DockerFile instruction of Docker series

What is DockerFile

Dockerfile is a text file used to build an image. The text content contains instructions and instructions required to build an image one by one. That is, a command parameter script.
Since this is used to build an image, please explain the steps of building an image. It mainly includes the following steps:
1. Write a dockerfile file
2.docker build build image
3.docker run uses image
4.docker push publishing image

DockerFile construction process

Basic knowledge

Each reserved keyword (instruction) must be capitalized
Execute from top to bottom
#Indicates a comment
Each instruction creates and commits a new mirror layer
dockerfile is development oriented.
dockerfile is generally divided into four parts: basic image information, maintainer information, image operation instructions and instructions executed when the container is started

Build command

The overall command structure can be illustrated with such a diagram. Each instruction will be described later.

FROM

The FROM instruction specifies that the basic image must be the first command.

"format"
FROM <IMAGE>(:tag/@digest)

digest is equivalent to a hash value. When you pull the image, you can take it with you to prevent the image from being maliciously tampered with

MAINTAINER

MAINTAINER stores information about the builder and MAINTAINER
Usually save your name and email address

MAINTAINER <NAME>

RUN

The RUN instruction describes the commands that need to be RUN during the construction of the image.

RUN It is used to execute commands in the mirror container. It has the following two command execution modes:
shell implement
 Format:
    RUN <command>
exec implement
 Format:
    RUN ["executable", "param1", "param2"]
Example:
    RUN ["executable", "param1", "param2"]
    RUN apk update
    RUN ["/etc/execfile", "arg1", "arg1"]
Note:  RUN The intermediate image created by the instruction will be cached and used in the next build. If you do not want to use these cache images, you can specify them at build time--no-cache Parameters, such as: docker build --no-cache

ADD

ADD <src>... <dest>
Example:
    ADD hom* /mydir/          # Add all files starting with "hom"
    ADD hom?.txt /mydir/      # ?  Replace a single character, for example: "home.txt"
    ADD test relativeDir/     # Add "test" to 'workdir' / relativedir/
    ADD test /absoluteDir/    # Add "test" to / absoluteDir/

WORKDIR

Set the working directory of the mirror

WORKDIR /path

VOLUME

Set mounted directory

VOLUME /path

EXPOSE

Set open ports

EXPOSRE port [port...]
#You can also set the protocol types allowed by the port, such as export 8080 / TCP

RUN

Commands to run after starting the container

CMD

Specify the commands to be executed when the container starts

Format:
    CMD ["executable","param1","param2"] (Executable document, priority)
    CMD ["param1","param2"] (Set ENTRYPOINT,Directly call ENTRYPOINT Add parameter)
    CMD command param1 param2 (implement shell Internal command)
Example:
    CMD echo "This is a test." | wc -
    CMD ["/usr/bin/wc","--help"]Note:   CMD differ RUN,CMD Used to specify the command to be executed when the container starts, and RUN Specifies the command to be executed when the image is built.

ENTRYPOINT

This is very similar to CMD, but it is different. Only the last CMD instruction will take effect, while the commands in the ENTRYPOINT instruction can be superimposed.

Format:
    ENTRYPOINT ["executable", "param1", "param2"] (Executable file, first)
    ENTRYPOINT command param1 param2 (shell Internal command)
Example:
    FROM ubuntu
    ENTRYPOINT ["top", "-b"]
    CMD ["-c"]Note:   ENTRYPOINT And CMD Very similar, the difference is through docker run The command executed will not be overwritten ENTRYPOINT,and docker run Any parameter specified in the command will be passed to again as a parameter ENTRYPOINT. Dockerfile Only one is allowed in ENTRYPOINT When the last command is executed, only the previous settings are overwritten ENTRYPOINT Instructions.

ONBUILD

Add commands here. When the current image is used as the basic image of other images, the instructions in it will be transferred and executed

COPY

Similar to add, copy the file to the image

ENV

Setting environment variables

Format:
    ENV <key> <value>  #Everything after < key > will be regarded as part of its < value >, so only one variable can be set at a time
    ENV <key>=<value> ...  #Multiple variables can be set, and each variable is a key value pair of "< key > = < value >". If < key > contains spaces, you can use \ to escape or mark with ""; In addition, backslashes can also be used for continuation
 Example:
    ENV myName John Doe
    ENV myDog Rex The Dog
    ENV myCat=fluffy

LABEL

Used to add image meta information to the image

Format:
    LABEL <key>=<value> <key>=<value> <key>=<value> ...
Example:
  LABEL version="1.0" description="This is a Web The server" by="IT record"
Note:
  use LABEL When specifying metadata, a LABEL Specify that one or more metadata can be specified. When multiple metadata are specified, different metadata are separated by spaces. It is recommended to pass all metadata through one LABEL Directive to avoid generating too many intermediate images.

DOCKERFILE test

There are not many basic commands that have been introduced earlier; Then use the above commands to build a centos as a test.
First, create a new file in the test machine for writing. The file path I use is / home/dockerfile.
ifconfig, clear and vim commands are added in the framework process. Use vim to edit the file. After editing, check the contents as follows.

Write a DOCKERFILE file

Build image

The command to build an image is docker build. The syntax was introduced earlier. Here, the following code is used for construction:

docker build -f dockerfilecentos -t mycentos:0.1 .

After execution, skip directly to the end, showing success, indicating that the construction is completed.

It can be viewed in the system

The first mycentos here is the image just built

Mirror test

Test the built image. Compared with the original centos, it can be found that at this time, mycentos: 0.1 has the ability to execute vim, ifconfig and clear instructions.

Added by Cynthia Blue on Thu, 03 Mar 2022 23:04:24 +0200