This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

preface

Speaking of Docker containers, there is a very important file Dockerfile, which is the configuration file used to define the image build process. Dockerfile contains commands and operations that need to be executed during image construction. When Dockerfile is executed in different environments, the image service of the corresponding environment will be generated, which can quickly realize container migration and cluster deployment. Because of this, Docker containers can be deployed and run across platforms.

The body of the

If we want to build our own service image, we can do it in two ways. The first way is to save the current running container directly, then rename the generated image file, usually using the Docker tag command, and finally push the renamed image to the public cloud repository or private repository. The second is to use the Dockerfile file introduced in this article to rebuild and then push to the corresponding repository.

In general, a Dockerfile is understood as a script file that executes instructions from the top down. When building the image, Docker will parse the commands in Dockerfile line by line. By observing the build log, we can also know that all commands in Dockerfile are executed in sequence.

Build instance log:

The current OS: Linux docker build - f Dockerfile. X86 - t registry.cn-beijing.aliyuncs.com/liuzhen007/bag-x86:1.6.6. Sending Build context to Docker Daemon 337.4MB Step 1/15: The FROM registry.cn-beijing.aliyuncs.com/liuzhen007/base-x86-ffmpeg:1.0 - > c91b1af20a82 Step 2/15: USER root ---> 827d7a1d2e52 Step 3/15 : ENV TZ=Asia/Shanghai ---> b1d66c3f4cc8 Step 4/15 : RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

 ---> c21e6a502a33

Step 5/15 : WORKDIR /root/bag/

 ---> c224c6317820

Step 6/15 : COPY ./bin/bag .

 ---> 59b4d013ce87

Step 7/15 : COPY ./bin/server.crt .

 ---> 36b92b7306f8

Step 8/15 : COPY ./bin/server.key .

 ---> 25da16fc2011

Step 9/15 : COPY ./config.example.yaml ./config.yaml

 ---> 44d1ae9f2686

Step 10/15 : RUN echo "/usr/local/lib/" >> /etc/ld.so.conf  && ldconfig

 ---> Running in bb907e7a1eb7

Step 11/15 : RUN mkdir /var/www && mkdir /var/www/html && mkdir /var/www/html/user-media &&  mkdir /var/www/html/module

 ---> Running in 7e1da4234327

Removing intermediate container 7e1da4234327

 ---> 4b4ca4e832f2

Step 12/15 : COPY ./data /var/www/html/user-media

 ---> 62a16954bafd

Step 13/15 : COPY ./docker/module /var/www/html/module

 ---> bb4e96ffdc57

Step 14/15 : COPY ./docker/moviemasher ./

 ---> 955a6248856e

Step 15/15 : CMD ["./bag"]

 ---> 7af71e575223

Successfully built 7af71e575223
Copy the code

The command analysis

FROM

It is not practical to build a new image completely from 0. The common practice is to rely on a base image and then add your own environment and code to the base image. The FROM command is used when declaring the underlying image.

The general command format is as follows:

FROM image AS name

Example Reference:

The From golang: 1.14.2 – alpine3.11 AS test

RUN

During image construction, you can run the following commands:

RUN command

Example Reference:

RUN go mod tidy

RUN go build main.go

COPY and ADD

When building a new image, we may need to import configuration files, program code, or execution scripts into the file system inside the image. The COPY and ADD commands are used to do this.

The general command format is as follows:

COPY [–chown=:] [“< source path 1>”… “< destination path >”]

ADD [–chown=:] [“< source path 1>”… “< destination path >”]

Example Reference:

COPY ./.netrc /app

COPY . /app/bag

CMD

Image-based containers that start a process when the container is started according to the commands specified when the image is built. And this command definition, is through the Dockerfile CMD command to achieve.

The general command format is as follows:

CMD [“< executable file or command >”,”param1″,”param2″,…

Example Reference:

CMD [“node”, “bin/www”]

At the end

Familiar with the command of Dockerfile is the premise of writing Dockerfile, this article only introduces a few of the most common commands, interested friends can go to in-depth understanding. Well, that’s all for tonight. Good night. I’m Liuzhen007, welcome to share more container knowledge.

Calendar Clocking (August Challenge)