Dockerfile syntax details
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:
|
|
MAINTAINER
Specify creator information
|
|
ENV
Set environment variables, which will be used by subsequent RUN instructions and maintained when the container is running.
ENV
RUN
RUN
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
|
|
Copy the local host’s
ADD
ADD
This command will copy the specified
VOLUME
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
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:
|
|
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.
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.
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
|
|
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.