What is a Dockerfile?

A Dockerfile is a text file used to build an image. The text content contains the instructions and instructions required to build the image.

Using Dockerfile custom 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:

FROM nginx
RUN echo 'This is a locally built nginx image' > /usr/share/nginx/html/index.html
Copy the code



The functions of the FROM and RUN directives

FROM: Custom images are based on images FROM, and nginx is the base image required for customization. Subsequent operations are based on Nginx. RUN: Used to execute the command line commands that follow. There are two formats: shell format:

RUN < command line command ># < command line command > is equivalent to shell commands that operate on terminals.
Copy the code

The exec formats:

RUN ["Executable file".Parameters of "1"."Parameter 2"]
# such as:
# RUN ["./test.php", "dev", "offline"] = RUN./test.php dev offline
Copy the code

Note: Each 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:

FROM centos
RUN yum install wget
RUN wget -O redis.tar.gz "http://download.redis.io/releases/redis-5.0.3.tar.gz"RUN tar -xvf redis.tar.gz The preceding command creates a layer 3 image. FROM centos RUN yum install wget \ && wget -o redis.tar.gz"http://download.redis.io/releases/redis-5.0.3.tar.gz" \
    && tar -xvf redis.tar.gz
Copy the code

As shown in the preceding example, the command is joined with &&. After this execution, only 1 layer image is created.

Start Build the image. In the directory where the Dockerfile file is stored, perform the build operation.

In the following example, build a nginx:v3 (image name: image tag) from a Dockerfile in the directory.

Note: The last. Represents the context path for this execution, which is described in the next section.

Details of other commands

1: ENV

Format: ENV or ENV =…

The ENV directive declares environment variables for containers created by the image. And in Dockerfile, the ENV directive declares environment variables that are interpreted and used by subsequent directives (ENV, ADD, COPY, WORKDIR, EXPOSE, VOLUME, USER).

Other instructions use environment variables in the format var I ab L E name or variable_name or variablename or {variable_name}. A variable can be escaped if it is preceded by a slash \. Such as $foo or ${foo} will be converted to foo and foo and foo and {foo} instead of the value held by the environment variable. In addition, the ONBUILD directive does not support environment replacement.

2: COPY

Format: COPY

The COPY command copies the file or directory pointed to and adds it to a new image. The path of the copied file or directory in the image is. You can specify multiple sources, but they must be relative paths in the context root. Can’t just be like COPY… /something /something. In addition, wildcards can be used to point to all files or directories that match wildcards. For example, COPY home* /mydir/ adds all files starting with “hom” to the directory /mydir/.

It can be a file or directory, but it must be an absolute path in the target image or relative to WORKDIR (WORKDIR is the path specified by the WORKDIR directive in Dockerfile and used to set working directories for other directives). If it ends with a backslash /, it points to a directory. Otherwise point to the file. In the same way. If it is a file, the contents will be written to. Otherwise, the contents of the pointed file or directory will be copied and added to the directory. When multiple sources are specified, it must be a directory. If not, a directory that does not exist in the path will be created.

3: the ADD

Format: the ADD

The ADD command is similar to the COPY command in that it can COPY local files to an image, but the ADD command also supports other functions. It can be a URL pointing to a network file. If it points to a directory, the URL must be the full path. In this case, the network filename filename can be obtained, and the file will be copied and added to /. Such as the ADD example.com/config.prop… / creates a file /config.property.

You can also point to a local compressed archive that will be extracted when copied to the container, such as ADD sxample.tar.xz /. However, if the file in the URL is an archive file, it will not be extracted.

Although the ADD and COPY directives have similar functions, COPY is generally recommended because it supports only local files and is more transparent than ADD.

4: EXPOSE

EXPOSE [/…]

The EXPOSE directive tells Docker that the container listens for a specified network port at run time. You can specify whether the port listens on TCP or UDP. If no protocol is specified, the default is TCP. This directive simply declares what port the container intends to use, and does not automatically map ports on the host. It can be specified at runtime via docker-p.

EXPOSE 80/ TCP EXPOSE 80/ UDP Copy code

5: the USER

Format: USER [:<group] or USER [:]

The USER directive sets the USER name and USER group(optional). The RUN,CMD, and ENTRYPOINT directives that follow it are executed with the set user.

6: WORKDIR

Format: WORKDIR /path/to/ WORKDIR

The WORKDIR directive sets up the working directory where the RUN, CMD, ENTRYPOINT, COPY, and ADD directives follow. If the working directory does not exist, one will be created automatically. The WORKDIR directive can be used multiple times in a Dockerfile. If a relative path is provided, it will be relative to the path of the previous WORKDIR directive. For example,

WORKDIR /a WORKDIR b WORKDIR c RUN PWD Copy the code. The output is /a/b/c

7: the RUN

Format 1: RUN (shell format) Format 2: RUN [” executable “, “param1”, “param2”] (exec format, recommended)

The RUN command creates a container based on the image created by the previous command, runs the command inside the container, and submits the container as a new image when the command is finished. The new image will be used by the next instruction in the Dockerfile.

The two formats of the RUN directive represent the two ways in which the command can be RUN in a container. When the shell format is used, the command is run through /bin/sh -c. When using the exec format, commands are run directly and the container does not call shell programs, that is, there are no shell programs in the container. Parameters in exec format are parsed by Docker as JSON arrays, so double quotes are required instead of single quotes. Because the exec format is not executed in the shell, the parameters of the environment variable are not replaced.

For example, RUN [” echo “, “HOME”] command, HOME”] command, HOME”] command, HOME does not do variable replacement. If you want to RUN a shell program, the instructions can be RUN [” /bin/bash”, “-c “,” echo “, “$HOME”].

8: CMD

CMD directives come in three formats.

Format 1: CMD (shell format) format 2: CMD [” executable “, “param1”, “param2″] (exec format, recommended) CMD [” param1 “, “param2”] (provides parameters for the ENTRYPOINT directive)

CMD directives provide the container’s runtime defaults, which can be directives or parameters. A Dockerfile can have multiple CMD directives, but only the last one is valid. The CMD [” param1 “, “param2”] format is used when the CMD command is combined with the ENTRYPOINT command. The parameters in the CMD command are added to the ENTRYPOING command. Using the shell and exec formats, commands RUN in the container the same way as the RUN directive.

The difference is that the RUN command executes commands while building the image and generates a new image; The CMD command does not execute any commands when building the image, but defaults to the CMD command as the first command to execute when the container starts. If the user specified command parameters when running the Docker run command on the command line interface, the commands in the CMD command will be overwritten.

9: ENTRYPOINT

The ENTRYPOINT directive comes in two formats.

Format 1: ENTRYPOINT (shell format) Format 2: ENTRYPOINT [” Executable “, “param1”, “param2”] (exec format, recommended format)

The ENTRYPOINT directive is similar to the CMD directive in that it allows the container to execute the same command each time it is started, but there are differences. A Dockerfile can have multiple ENTRYPOINT directives, but only the last ENTRYPOINT directive is valid.

When using Shell format, the ENTRYPOINT directive ignores any CMD and docker run arguments and runs in bin/sh -c. This means that the ENTRYPOINT instruction process is a child of bin/ sh-c, its PID in the container will not be 1, and it will not accept Unix signals. That is, when docker stop is used, the command process cannot receive the SIGTERM signal.

The exec format is recommended, in which the command arguments passed by the Docker run overwrite the contents of the CMD directive and append to the parameters of the ENTRYPOINT directive. CMD can be a parameter or an instruction, while ENTRYPOINT can only be a command. In addition, the run command parameters provided by the Docker run command can override CMD, but not ENTRYPOINT.