A Dockerfile is a text file used to build an image. The text content contains the instructions and instructions required to build the image.
Use Dockerfile to customize the image
Here is how to run the Dockerfile file to customize an image. The instructions in the Dockerfile file will be explained in the next section. Here you only need to know the process of building an image.
1, the following to customize a nginx mirror (built inside the mirror there will be a/usr/share/nginx/HTML/index. The HTML file)
In an empty directory, create a new file named Dockerfile and add the following contents to the file:
[Bash shell]
Plain text view
Copy the code
?
1
2
FROM nginx
RUN echo 'This is a locally built nginx image' > /usr/share/nginx/html/index .html
The functions of the FROM and RUN directives
FROM: Custom images are based on FROM images, here nginx is the basic image required for customization. Subsequent operations are based on Nginx.
RUN: is used to execute the command line command that follows. There are two formats:
Shell formats:
[Bash shell]
Plain text view
Copy the code
?
1
2
RUN < command line command >
# < command line command > is equivalent to shell commands that operate on terminals.
The exec formats:
[Bash shell]
Plain text view
Copy the code
?
1
2
3
RUN [ "Executable file" . Parameters of "1" . "Parameter 2" ]
# such as:
# RUN ["./test.php", "dev", "offline"] = RUN./test.php dev offline
Pay attention toEach execution of the: Dockerfile directive creates a new layer on top of the docker. So too many meaningless layers will cause the image to expand too much. Such as:
As shown in the preceding example, the command is joined with &&. After this execution, only 1 layer image is created.
Start building the image
In the directory where the Dockerfile file is stored, perform the build action.
In the following example, build an nginx:test (image name: image tag) from a Dockerfile in the directory.
noteThe last. Represents the context path for this execution, which is described in the next section.
[Bash shell]
Plain text view
Copy the code
?
1
$ docker build -t nginx: test .
If the preceding information is displayed, the building is successful.
Context path
In the previous section, we mentioned that the last. Is a context path. So what is a context path?
[Bash shell]
Plain text view
Copy the code
?
1
$ docker build -t nginx: test .
Context path, refers to the docker image building, sometimes want to use the local file (such as copy), docker build command after knowing this path, will package all contents in the path.
parsing: Because docker runs in C/S mode. Our machine is C, and the Docker engine is S. The actual construction process is completed under the Docker engine, so we cannot use our local files at this time. This needs to package the files under the specified directory of our local computer together and provide them to the Docker engine for use.
If the last parameter is not specified, the default context path is where the Dockerfile is located.
Pay attention toDo not put useless files in the context path, because they will be packaged together and sent to the Docker engine. If there are too many files, the process will be slow.
Instruction,COPY
Copy directive that copies a file or directory from the context directory to a specified path in the container.
[–chown=<user>:<group>]: Optional parameters. The user changes the owner and owner group of the file copied to the container.
< source path >: Source file or source directory. In this case, it can be a wildcard expression. The wildcard rule must meet the Filepath. Such as:
[Bash shell]
Plain text view
Copy the code
?
1
2
COPY hom* /mydir/
COPY hom? .txt /mydir/
< target path >: Specifies a path in the container. This path does not need to be created in advance. If no path exists, it will be created automatically.
ADD
The format of the ADD command is the same as that of COPY (COPY is officially recommended for the same requirements). Functions are similar, with the following differences:
Advantages of ADD: When the < source > is executed as a tar file in gzip, bzip2, and xz formats, it is automatically copied and decompressed to the < destination path >.
Disadvantages of ADD: You cannot copy tar files without decompressing them. Invalidates the image build cache, which may slow the image build. You can determine whether to use it based on whether automatic decompression is required.
CMD
Similar to the RUN directive, used to RUN a program, but at different points in time:
CMD runs at docker run time.
RUN is in the Docker build.
role: specifies the default program to run for the started container. The container ends when the program runs. Programs specified by CMD directives can be overwritten by programs specified to run in the Docker run command line argument.
Pay attention toIf multiple CMD directives exist in a Dockerfile, only the last one takes effect.
CMD [ "<param1>" . "<param2>" . ] The ENTRYPOINT directive provides a default parameter for the program specified by the ENTRYPOINT directive
The second format is recommended because the execution process is relatively clear. The first format is actually automatically converted to the second format as it runs, and the default executable is sh.
ENTRYPOINT
Similar to CMD directives, but not overridden by directives specified by the docker Run command line arguments, and these command line arguments are sent as arguments to the program specified by the ENTRYPOINT directive.
However, if a Docker run is run with the — entryPoint option, the argument to this option can override the program specified by the EntryPoint directive as the program to be run.
advantages: You can specify the parameters required by ENTRYPOINT when executing docker run.
Pay attention to: If there are multiple ENTRYPOINT directives in a Dockerfile, only the last one takes effect.
It can be used with the CMD command: CMD is usually used as a variable parameter. In this case, CMD is used as a parameter to ENTRYPOINT, as described in the following example.
Example:
Assuming that the nginx:test image has been built from Dockerfile:
[Bash shell]
Plain text view
Copy the code
?
1
2
3
4
FROM nginx
ENTRYPOINT [ "nginx" . "-c" ] # set the cords
CMD [ "/etc/nginx/nginx.conf" ] # varargs
1. Run without passing parameters
[Bash shell]
Plain text view
Copy the code
?
1
$ docker run nginx: test
By default, the container runs the following command to start the main process.
[Bash shell]
Plain text view
Copy the code
?
1
nginx -c /etc/nginx/nginx .conf
2. Run the parameter transfer
[Bash shell]
Plain text view
Copy the code
?
1
$ docker run nginx: test -c /etc/nginx/new .conf
By default, the container will run the following command to start the main process (/etc/nginx/new.conf: assuming the file already exists in the container).
[Bash shell]
Plain text view
Copy the code
?
1
nginx -c /etc/nginx/new .conf
ENV
Set the environment variable and define the environment variable so that it can be used in subsequent instructions.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
2
ENV <key> <value>
ENV <key1>=<value1> <key2>=<value2>...
The following example sets NODE_VERSION = 7.2.0, which can be referenced by $NODE_VERSION in subsequent directives:
[Bash shell]
Plain text view
Copy the code
?
1
2
3
4
ENV NODE_VERSION 7.2.0
RUN curl -SLO "https://nodejs.org/dist/v$NODE_VERSION/node-v$NODE_VERSION-linux-x64.tar.xz" \
Build parameters, and ENV to. But the scope is different. ARG environment variables are only valid in Dockerfile, that is, only in the docker build process, the built image does not exist in this environment variable.
The build command docker build can be overridden with –build-arg < parameter name >=< value >.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
ARG < parameter name >[=< default value >]
VOLUME
Define anonymous data volumes. If you forget to mount the data volume when starting the container, it will be automatically mounted to the anonymous volume.
Function:
Avoid the loss of important data due to container restart, which can be fatal.
Avoid growing containers.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
2
VOLUME [ "< 1 > path" . "< 2 > path" . ]
VOLUME < path >
When starting the container Docker run, we can change the mount point with the -v argument.
EXPOSE
Just declare the port.
Function:
Help mirror users understand the daemon port of the mirror service to facilitate the configuration of mappings.
When random port mapping is used at runtime, that is, when Docker run -p, the ports for EXPOSE are automatically mapped randomly.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
EXPOSE < Port 1> [< Port 2>...]
WORKDIR
Specify the working directory. The working directory specified with WORKDIR is present at each layer of the build image. WORKDIR Specifies the working directory, which must be created in advance.
Each RUN command in docker build is a new layer. Only directories created by WORKDIR will always exist.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
WORKDIR < working directory path >
USER
The command is used to specify the user and user group that execute the subsequent command. The user and user group must exist in advance.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
USER < username >[:< USER group >]
HEALTHCHECK
Specifies a program or directive that monitors the running status of the Docker container service.
Format:
[Bash shell]
Plain text view
Copy the code
?
1
2
3
4
HEALTHCHECK [options] CMD < command > : Sets the command to check the container health
HEALTHCHECK NONE: use this line to mask the underlying mirror's HEALTHCHECK if it has one
Used to delay the execution of build commands. The ONBUILD command in Dockerfile will not be executed during the build process (assuming the image is test-build). When a new Dockerfile is built using the previously built image FROM test-build, the command ONBUILD specified in the Dockerfile of test-build will be executed.