FROM

Specify the base image used to build the image. FROM must be the first instruction in the non-comment line in the Dockerfile. If there is no specified image locally, it will automatically pull the image from the Docker public library. Example:

1
FROM ubuntu:14.04 #Inherit ubuntu:14.04

MAINTAINER

Specify creator information

1
MAINTAINER wanzi "iwz2099@163.com"

ENV

Set environment variables, which will be used by subsequent RUN instructions and maintained when the container is running. ENV # can only set one variable ENV = … # allows multiple variables to be set at once Example:

1
2
ENV LANG en_US.UTF-8
ENV LANG=en_US.UTF-8 LC_ALL=en_US.UTF-8

RUN

RUN or RUN [“executable”, “param1”, “param2”]. Example:

1
2
RUN yum -y install bind-utils
RUN ["/bin/bash", "-c", "yum -y install bind-utils"]

The former will run the command in the shell terminal, i.e. /bin/sh -c yum -y install bind-utils; the latter will use exec to execute. Specifying the use of other terminals can be achieved through the second method. Each RUN instruction will execute the specified command based on the current image and submit it as a new image. Subsequent RUNs are based on the image submitted by the previous RUN. The image is layered and can be created through any historical submission point of an image, similar to the version control of source code.

When the command is long, you can use \ to wrap the line.

COPY:

COPY Example:

1
COPY script/ /build/script/

Copy the local host’s (relative path to the directory where Dockerfile is located) to the container’s . When using the local directory as the source directory, it is recommended to use COPY.

ADD

ADD Example:

1
2
ADD https://www.baidu.com/index.html /var/www/html/
ADD ubuntu-xenial-core-cloudimg-amd64-root.tar.gz /

This command will copy the specified to the in the container. can be a relative path to the directory where the Dockerfile is located; it can also be a URL (automatically downloaded and copied to the container); it can also be a tar file (automatically unzipped to a directory).

VOLUME

1
2
VOLUME [ "/data" ]
VOLUME [ "/var/lib/redis", "/var/log/redis" ]

Declare a data volume, which can be used for mounting. [] contains the path. Create a mount point that can be mounted from the local host or other containers. It is generally used to store databases and data that needs to be maintained.

USER

USER A UID set when the image is running, the user when the RUN command is executed

CMD

Supports three formats CMD [“executable”,“param1”,“param2”] Use exec to execute, the recommended method; CMD command param1 param2 is executed in /bin/sh and provided to applications that need to interact; CMD [“param1”,“param2”] Provides the default parameters for ENTRYPOINT; Specify the command executed when starting the container. Each Dockerfile can only have one CMD command. If multiple commands are specified, only the last one will be executed.

WORKDIR

Specify the working directory of RUN, CMD and ENTRYPOINT commands Example:

1
WORKDIR /opt/nodeapp

ONBUILD

ONBUILD [INSTRUCTION] Configure the operation instructions executed when the created image is used as the base image of other newly created images.

For example: Dockerfile uses the following content to create the image image-A.

1
2
ONBUILD ADD . /app/src
ONBUILD RUN /usr/local/bin/python-build --dir /app/src

For example: If you create a new image based on image-A, when you use FROM image-A to specify the base image in the new Dockerfile, the ONBUILD instruction content will be automatically executed, which is equivalent to adding two instructions later.

1
2
3
4
FROM image-A
#Automatically run the following
ADD . /app/src
RUN /usr/local/bin/python-build --dir /app/src

For images using the ONBUILD instruction, it is recommended to indicate it in the tag, such as ruby:1.9-onbuild.

ENTRYPOINT

Configure the command executed after the container is started, and it cannot be overwritten by the parameters provided by docker run, while CMD can be overwritten. If you need to overwrite, you can use the docker run –entrypoint option. There can only be one ENTRYPOINT in each Dockerfile. When multiple ENTRYPOINTs are specified, only the last one takes effect. Two formats are supported ENTRYPOINT [ “nodejs”, “server.js” ] ENTRYPOINT command param1 param2 (executed in shell)

EXPOSE

EXPOSE Tell the server the port that the container listens on at runtime. When starting the container, you need to pass -P. The Docker host will automatically assign a port to forward to the specified port. Example:

1
EXPOSE 3000

Notes

  • Keep it as simple as possible and do not install redundant packages
  • Write a .dockerignore file to exclude some directories and files. The syntax is similar to .gitignore
  • Try to choose the image provided by Docker as the basic version to reduce the image size.
  • The instructions in the first few lines of Dockerfile should be fixed. Frequent changes are not recommended to effectively use the cache.
  • Use ‘' to connect multiple RUN commands, which is easy to understand and maintain.
  • COPY and ADD should use the former first
  • Use the -t tag to build the image, which is conducive to managing the newly created image.
  • Do not map public ports in Dockerfile.
  • Run locally before pushing to ensure that the built image is correct.