Dockerfile features and their workflow
Docker can create an image by automatically reading the Dockerfile file, Dockerfile is a text document type, where all the actions commands that the user can make at the command line can be defined here, combining these commands and generating a directly available image. With Docker build, you can automate your build by executing multiple instructions in succession. This article describes the command parameters that can be executed in dockerfile.
The Docker build command can build an image from a dockerfile or a text, and the build text can be a local path or a git URL path. "Docker build." Indicates that the current directory is used as the context to build (warning, do not execute this command at the root, this will transfer the context information of your hard drive to the Docker process, think about the consequences), by default, if you do not specify with the-F option, a file named Dockerfiler will be loaded. You can then use the-t option to specify the tag tag for the build image and the warehouse name, for example (multiple parameters can be added):
Docker build-t shykes/myapp:1.0.2-t shykes/myapp:latest.
The Docker build operation is done by the Docker daemon, not the CLI (command-line operation). The order of construction is that the context information is first sent to the process, and then the Docker daemon executes the instructions inside the dockerfile into the new mirror, and the context information is automatically cleared after the end. To improve Docker performance, you can also add a ". Dockerignore" file to the context directory. It is important to note that each instruction in Dockerfile is completely independent, that is, you cannot expect the "RUN cd/tmp" command to set the working environment of the next instruction under the "/tmp" directory.
Docker uses mirrored caches (the default) as much as possible to improve its performance. Of course, you can also use "--no-cache=true" to not build with the cache. Once the build is complete, it can be uploaded to the repository at the specified registry via the push command.
Dockerfile writing style:
first, the basic grammar
# Comment
Instruction arguments
"Instruction" is case-insensitive, but for the sake of distinction, uppercase is good. Dockerfile each line supports only one instruction, the first non-comment line in the starting position of the dockerfile needs to use the "from" directive to specify the source image to use for the build, and the start of the instruction area should have a comment line with "#" to illustrate the task content of the current region.
two, variable substitution
In Dockerfile, variable declarations can be expressed as $variable_name or ${variable_name}, which is primarily used to distinguish between variables without spaces, such as ${foo}_bar.
Variable declarations in Dockerfile also support a small number of bash modifiers, such as:
${variable_name:+word}: If it is already assigned and not empty, the value of $variable_name is used, and if not, an empty string is returned
${variable_name:-word}: If you have already assigned a value, use the value of $variable_name, and if not, use Word
Word can be any string that contains additional environment variables
You can also use the "\" symbol to escape, for example "\ $foo" means that the content is "$foo"
Environment variables can be used in the following directives:
Environment variable Support replacement
Three, Dockerignore file
when Docker executes a command (before sending context information to a process), if there is a ". Dockerignore" file, the file or directory defined in it does not add or copy instructions to the image. For example:
*/temp* #如,/aa/template.txt
*/*/temp* #如,/aa/bb/template.conf
Temp? #如,/TEMPAA or/TEMPBB
*.md
! Readme.md #取反, these two lines are used in combination to indicate that all files ending in. md except README.MD are included
Use the match format to refer to the Go language style
Dockerfile instruction Description:
From
Role: The directive constructs the source image, which must be in the first non-annotated instruction
Syntax: from <image> or from <image>:<tag> or from <image>@<digest>, and the latter two (labels and summaries) are optional, if they do not match, The error is returned
Maintainer
RUN
Function: Executes the command in the referenced image, submits the result, and is used to dockerfile the subsequent operation
Syntax: Run has two ways of writing
Examples:
Run/bin/bash-c "Source $HOME/.BASHRC; echo $HOME "#跨行操作时可以添加反斜杠
RUN ["/bin/bash", "-C", "echo Hello"]
Precautions:
-
cmd
-
role: The main purpose of CMD is to provide some default parameters for the execution container, which can include an executable file, or a system command. The cmd instruction can only exist once in dockerfile, and if there are more than one, only the last
-
syntax: CMD has three notation
-
cmd ["Executable", "param1", "param2"]
-
cmd ["param1", "param2"] #此种写法需要配置ENTRYPOINT指令结合使用, if two are used independently, only the last CMD or entrypoint directive
-
is executed. Span style= "font-family: ' Microsoft Yahei '; background-color:inherit;" >cmd command param1 param2 #shell风格
-
label
-
function: The label instruction is used to add some metadata to the image, the label is a key-value pair type data, one image can have one or more labels, but too many tags cause a low-energy image to be generated. When multiple labels are specified, try to display them in one layer instead of using multiple label directives. You can use Docker inspect to view the label information for the image
-
syntax: LABEL <key>=<value> <key>=<value> <key>=<value> <key>= <value> ...
-
expose
-
function: Specifies that the container is mapped to the port of the host, and that the IP of the Docker container is randomly generated, so the external access needs to be accessed through the host port mapping, and expose specifies the port to which it maps to the host, referring to the-p or-p option in man Docker-run
-
syntax: EXPOSE <port > [<port> ...]
-
env
-
role: Set environment variables, where variables can be referenced in Dockerfile
-
syntax: Env has two ways of writing
-
add
-
function: The add instruction can add a source (which can be a file, directory, or URL) to a path inside a container, and you can add a file using a matching rule. If it is a URL, it will be downloaded to the container target path
-
syntax: Add has two ways of writing
-
copy
-
copy is copying the source file or directory into the specified path within the container, and if you do not carry the Dest parameter, a complete missing directory is created
-
-
copy <src> <dest>
-
copy ["<src>",... <dest>] #这种形式一般用于包含空格的路径
EntryPoint
Function: The use of the directive is divided into two situations, one is to use it alone, and the other is used in combination with CMD. When used in conjunction, the entrypoint instruction can only execute commands, but cannot specify parameter parts; the cmd instruction is not a complete executable command, but only a parameter part. For example, the following cases:
From Ubuntu
CMD ["-L"]
entrypoint ["/usr/bin/ls"]
VOLUME
-
function: The volume instruction can create a mount point for the container to use, thus persisting the data within the container (the container will disappear after the data is closed). Volumes can be created through Docker create, or they can be created directly using Docker run, which can be deleted only when no container is connected, and volumes created through Docker run are shared with other containers. For example (for more information about volume, please refer to the link: https://docs.docker.com/engine/userguide/containers/dockervolumes/# Mount-a-host-directory-as-a-data-volume):
-
USER
Function: Used to specify user name or UID, which will run when run, CMD, and entrypoint instructions are used.
Syntax: USER daemon
Workdir
function: The workdir instruction is used to set the working directory of the Run, CMD, and entrypoint instructions
Syntax: Workdir/path/to/workdir
ARG
-
onbuild
-
function: Onbuild The specified instruction does not execute when the image is built, only executes
-
syntax: onbuild [instruction]
Stopsignal
Function: Stopsignal used to set the exit status code, can be a normal system signal, such as Sigkill, can also be a matching number, the host can close the container through this signal
Syntax: stopsignal Signal
This article is from the "Letter" blog, please be sure to keep this source http://leon0long.blog.51cto.com/3011404/1768324
Docker Configuration Guide (iii): Dockerfile (i)