Docker basic operation uses --dockerfile

Chen Bucheng I 2022-06-24 07:17:50 阅读数:812

dockerbasicoperationusesdockerfile

5、 ... and .Dockerfile file

Learn how to use image After file , The next question is how to generate image file ? If you want to promote your software , I'm bound to make it myself image file .

And that's where it comes in Dockerfile file . It is a text file used to configure image.Docker Generate binary from this file image file .

1. Now I will koa-demos Project as an example , How to write the introduction Dockerfile file , Enable users to Docker Running inside the container Koa frame , Please download it first as preparation Source code . git clone https://github.com/ruanyf/koa-demos.gitcd koa-demos

2. To write Dockerfile file , First, in the root of the project , Create a new text file .dockerignore, Write the following Content .

The code indicates that these three paths should be excluded , Don't pack in image file . If you have no path to exclude , This file can not be created . vim .dockerignore

  1. .git
  2. node_modules
  3. npm-debug.log

3. then , Under the root directory of the project , Create a new text file Dockerfile, Write the following Content .

vim Dockerfile

  1. FROM node:8.4
  2. COPY ./app
  3. WORKDIR /app
  4. RUN npm install --registry=https://registry.npm.taobao.org
  5. EXPOSE 3000

There are five lines of code above , The meaning is as follows

FROM node:8.4: The image Documents inherit official node image, A colon indicates a label , Here the label is 8.4, namely 8.4 Version of node.
COPY . /app: All files in the current directory ( except .dockerignore The path of exclusion ), All copy into image Of documents /app Catalog .
WORKDIR /app: Specify the next work path as /app.
RUN npm install: stay /app Under the table of contents , function npm install Command installation depends on . Be careful , All dependencies after installation , Will be packaged into image file .
EXPOSE 3000: The container 3000 Port exposed , Allow external connections to this port .

4. establish image file With Dockerfile After file , You can use docker image build Command to create image The file . docker image build -t koa-demo .

In the above code ,-t Parameters are used to specify image Name of file , You can also specify the label later with a colon . If you don't specify , The default label is latest. The last point means Dockerfile The path of the file , The above example is the current path , So it's a point .

If the operation is successful , You can see the newly generated image file koa-demo 了 . docker image ls

5. Container generation docker container run Command from image File generation container docker container run -p 8000:3000 -it koa-demo /bin/bash

The parameters of the above command are as follows :

-p Parameters : Container of 3000 The port maps to the native 8000 port .
-it Parameters : Container of Shell Map to the current Shell, Then you type the command in the local window , It will pass into the container
koa-demo:0.0.1:image Name of file ( If there's a label , Labels are also required , The default is latest label )
/bin/bash: After container startup , The first internal command to execute . Here is the start Bash, Ensure that users can use Shell

If everything goes well , After running the above command , A command line prompt is returned [email protected]6d80f4aaf1e:/app#

That means you're already in the container , The prompt returned is the prompt inside the container Shell Prompt . Execute the following command . [email protected]:/app# node demos/01.js

6. At this time ,Koa The framework is already running . Open the browser on this computer , visit ”, It's because of this demo No write route .

In this case ,Node The process is running in Docker In the virtual environment of the container , The file system and network interface that the process touches are virtual , Isolated from the native file system and network interface , Therefore, you need to define the port mapping between the container and the physical machine (map).

Now? , On the command line of the container , Press down Ctrl + c stop it Node process , Then press Ctrl + d( Or input exit) Exit the container . Besides , It can also be used. docker container kill Terminate container operation .

In another terminal window of this machine , Find out the container ID docker container ls

Stops the specified container from running docker container kill [containerID]

7. After the container stops running , It won't go away , Delete the container file with the following command

Find out the container ID docker container ls --all

Deletes the specified container file docker container rm [containerID]

You can also use docker container run Ordered –rm Parameters , Automatically delete the container file after the container stops running . docker container run --rm -p 8000:3000 -it koa-demo /bin/bash

copyright:author[Chen Bucheng I],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/175/20210702150335073s.html