Docker_07 Dockerfile

Time:2021-2-22

$ docker inspect mysql

What is dockerfile

DockerfileIt’s calledDockerfileThe file contains some linux commands,DockerBuild the image by reading the command in the file.

Dockerfile file content structure

DockerfileIt is generally divided into four partsBasic image information, maintainer information, image operation instruction and container start-up execution instruction#byDockerfileComments in.

Run dockerfile

  • docker build -t image_name:tag_name .
    • You can also pass the-fParameter to specify the dockerfile file location
    • docker build -f /path/Dockerfile .

Command details

FORM

FORM: Specifies the underlying image, which must be the first command

Format:
  FROM <image>
  FROM <image>:<tag>
  FROM <image>@<digest>
Example:
  FROM centos:7.0

MAINTAINER

MAINTAINER: maintainer information

Format:
    MAINTAINER <name>
Example:
    MAINTAINER zhangsan

RUN

RUN: the command to execute when building a mirror

Format:
    Run < command > * exec execution*
Format:
    RUN ["executable", "param1", "param2"]
Example:
    RUN ["/bin/executable", "param1", "param2"]
    RUN yum install nginx

ADD and COPY

ADD: add local files to the container, tar type files will be automatically decompressed (network compressed resources will not be decompressed), you can access network resources, similar to WGet

Format:
    ADD <src>... <dest>
    Add ["< SRC >",... "< dest >"] is used to support paths containing spaces
Example:
    Add tes * / mydir / # add all files beginning with "tes"
    Add tes?. TXT / mydir / #? To replace a single character, for example:“ test.txt "
    Add test relativedir / # add "test" to 'workdir' / relativedir/
    Add test / absolutedir / # add "test" to / absolutedir/
Note: the first parameter refers to the host file path, and the second parameter refers to the container path

COPY: function similar to add, but will not automatically decompress the file, can not access network resources

CMD

CMDCall after building container, that is, when container starts.

Format:
    CMD ["executable", "Param1", "param2"] (executable file, priority)
    CMD ["Param1", "param2"] (if entrypoint is set, entrypoint is directly called to add parameters)
    CMD command Param1 param2
Example:
    CMD echo "This is a test." | wc -
    CMD ["/usr/bin/wc","--help"]
Note:
   CMD is different from run. CMD is used to specify the command to be executed when the container is started, while run is used to specify the command to be executed when the image is built.

ENTRYPOINT

ENTRYPOINT: configure the container to be executable. With the help of CMD, “application” can be omitted and only parameters can be used

Format:
    Entrypoint ["executable", "Param1", "param2"] (executable, priority)
    Entrypoint command Param1 param2 (shell internal command)
Example:
    FROM ubuntu
    ENTRYPOINT ["top", "-b"]
    CMD ["-c"]
Note:
   Entrypoint is very similar to CMD. The difference is that the command executed through docker run will not override entrypoint, and any parameters specified in docker run will be passed to entrypoint again as parameters. Only one entrypoint command is allowed in dockerfile. If more than one command is specified, the previous settings will be overridden and only the last entrypoint command will be executed.
       docker run -itd --name=nginx nginx echo 'hello word'

LABEL

LABEL: used to add metadata to the image

Format:
    LABEL <key>=<value> <key>=<value> <key>=<value> ...
Example:
  Label version = "1.0" description = "this is a nginx image"
Note:
  When using label to specify metadata, a label specification can specify one or more pieces of metadata. When multiple pieces of metadata are specified, different metadata are separated by spaces. It is recommended to specify all metadata by a label instruction to avoid generating too many intermediate images.

ENV

ENV: setting environment variables

Format:
    All contents after env < key > < value > # < key > will be regarded as components 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 there are spaces in < key >, you can use  to escape, or you can use  to mark; in addition, the backslash can also be used for continuation

Example:
    ENV myName John Doe
    ENV myDog Rex The Dog
    ENV myCat=fluffy

EXPOSE

EXPOSE: Specifies the port for external interaction

Format:
    EXPOSE <port> [<port>...]
Example:
    EXPOSE 80 443
    EXPOSE 8080
Note:
  Exit does not allow the port of the container to access the host. To make it accessible, you need to publish these ports through - P when docker run runs the container, or publish all the ports exported by expose through - P parameter

VOLUME

VOLUME: used to specify the persistence directory

Format:
    VOLUME ["/path/to/dir"]
Example:
    VOLUME ["/data"]
    VOLUME ["/var/www", "/var/log/apache2", "/etc/apache2"
Note:
  A volume can exist in a specified directory of one or more containers, which can bypass the federated file system and have the following functions:
1. Volume can be shared and reused among containers
2. A container does not have to share volumes with other containers
3. The modified volume will take effect immediately
4. The modification of the volume will not affect the image
5. The volume will exist until no container is using it

WORKDIR

WORKDIR: working directory, similar to the CD command

Format:
    WORKDIR /path/to/workdir
Example:
    Workdir / usr / local / (the working directory is / usr / local /)
    Workdir nginx (at this time, the working directory is / usr / local / nginx)
    Workdir SBIN (at this time, the working directory is / usr / local / nginx / SBIN)
Note:
  After setting the working directory through workdir, the following commands in dockerfile, such as run, CMD, entrypoint, add and copy, will be executed in this directory. When using docker run to run the container, the - W parameter can be used to override the working directory set at the time of construction.

USER

USER: Specifies the user name or uid when running the container, and the specified user will also be used in subsequent run. When using user to specify a user, you can use the user name, uid or GID, or a combination of both. When the service does not need administrator rights, you can specify the running user through this command. And you can create the required users before

Format:
  USER user
  USER user:group
  USER uid
  USER uid:gid
  USER user:gid
  USER uid:group
 Example:
      USER www
 Note:
  After user is used to specify the user, the following commands run, CMD and entrypoint in dockerfile will use the user. After the image is built, when the container is run through docker run, the - U parameter can be used to override the specified user.

ARG

ARG: used to specify the variables passed to the build runtime

Format:
    ARG <name>[=<default value>]
Example:
    ARG site
    ARG build_user=www

ONBUILD

ONBUILD: used to set the mirror trigger

Format:
  ONBUILD [INSTRUCTION]
Example:
  ONBUILD ADD . /app/src
  ONBUILD RUN /usr/local/bin/python-build --dir /app/src
Note:
  When the constructed image is used as the base image of other images, the trigger in the image will be triggered by the key

This work adoptsCC agreementReprint must indicate the author and the link of this article

Elder brother is going to be a CTO in the future. How can these technologies be successful?