Due to recent work needs, began to contact container related content, these days need to Devops platform using DockerFile to build project mirror, using Docker deployment a set of SIT environment, reprint this article, record DockerFile writing rules!

1. Compile the image

  1. Compile the mirror

    Dockerfile similar to Makfile, users use the docker build can compile image, use this command to set the compilation of the mirror can be used by the number of CPU, memory size, file PATH, such as grammar: docker build [OPTIONS] PATH | | – the URL

The format is name: TAG -f Dockerfile name. The default value is PATH/Dockerfile. Example: docker build -f ~/ php.dockerfile. Note: PATH is the working directory for compiling images, and the Docker Daemon scans all files in PATH at the start of compiling, which can be added to the compilation directory

Docker Daemon reads instructions sequentially from Dockerfile, generates a temporary container, and executes instructions in the container. After the container is successfully compiled, it will be submitted as an image layer to be added to the final image. In order to speed up the compilation process, Docker Daemon uses a caching mechanism. If a desired intermediate image is found in the cache, it is used directly without generating a temporary container (you can choose not to use the cache at compile time with the — no-cache option)

  1. Dockerignore file

Before compiling, the Docker Daemon reads the. Dockerignore file in the compile directory and ignores the files and directories in it, where wildcards (? * represents a single character, * represents zero or any character), when using wildcard characters, there are always several exceptions, this can be used! + file name, Docker Daemon will read! The following file

*/temp* Ignores files and directories that start with temp in the subdirectory below PATH, for example, PAHT/A/temp. TXT */*/temp* Ignores files and directories that start with temp in the subdirectory below PATH, for example, PATH/A/B/temp.txt *.md! Readme. md Ignores all MD files except readme.mdCopy the code

Dockerfile instruction

Dockerfile is composed of multiple instructions, each instruction in the compilation of the image to execute the corresponding program to complete some functions, consists of instructions + parameters, separated by commas, # as the beginning of the comment, although the instruction is not case-sensitive, but the general instruction is larger, parameters use lowercase

Instruction: the FROM functional description: set the base image syntax: FROM < image > [: < tag > | @ < digest >] tip: Images are generated FROM a base image (operating system or other images). You can add multiple FROM directives to a Dockerfile to generate multiple images at once. Note: If you omit the tag option, latest images will be used

Command: MAINTAINER Function description: Configures an image. Author Syntax: MAINTAINER < name>

Syntax: RUN < command> RUN [” executable “, “param1”, “param2”] The RUN command generates a container in which the script is executed, and the container uses the current image. When the script is completed, the Docker Daemon submits the container to an intermediate image for subsequent instructions to supplement: The first mode of RUN command is shell mode. Use /bin/sh -c < command> to RUN the script, which can be divided into multiple lines with \. The second mode is exec mode, which is used when /bin/sh is not in the image or another shell is to be used. It does not invoke shell commands

Example: RUN source $HOME/.bashrc; \ echo $HOME RUN ["/bin/bash ", "c", "echo" hello] RUN [" sh ", "-" c, "echo", "$HOME"] read call the shell environment variables using the second wayCopy the code

Syntax: CMD [” executable “, “param1”, “param2″] CMD [” param1 “, “param2”] CMD < command> Note: Only one CMD command can be used in a Dockerfile. If more than one CMD command is written, the last one takes effect

Docker inspect: LABEL < key>=< value> < key>=< value>… Note: Each command generates an image layer. Docker images can only have 127 layers. If they exceed the Docker Daemon, errors will be reported, such as LABEL.. =.. < pretend there is a newline > LABEL.. =.. This can be combined to reduce the number of mirror layers by separating them with a space. Similarly, you can use the hyphen \ to split a script into multi-line mirrors that inherit tags from the underlying mirror or override tags with the same name if they exist

Instruction: EXPOSE function description: set the mirror exposed port, record when the container started to listen to which ports syntax: EXPOSE < port> < port>… Extension: Mirror exposed ports can be viewed with Docker Inspect for hints: When the container is started, The Docker Daemon scans the exposed ports in the image. If -p is added, the Docker Daemon exports all exposed ports in the image and assigns a random host port to each exposed port (exposed ports are container listening ports and host ports are external container access ports). Note: EXPOSE only exposes ports and does not export ports. When you start the container, you need to use -p /-p to export ports. In this case, you can access services provided by the container externally

ENV < key>=< value>… | < key > < value > note: environment variables in the build cycle, the first way to set up multiple environment variable, the second method only set an environment variable prompt: Use variables by variable name or {variable name} or variable name or variable name, use mode variable name can use {variable name} can use variable name when using variable name can use {variable name :-default} ${variable name :+cover} Set the default value or override value ENV set variable values are always the same throughout the compilation process

Command: ADD Function description: Copy files to the image syntax: ADD < SRC >… “< < dest > | [SRC >”,… “< dest >”] note: When SRC is a file or directory, Docker Daemon looks for these files or directories from the compiled directory, and dest is the absolute path in the image or the path relative to WORKDIR: If SRC is a directory, copy all contents of the directory, including the metadata of the file system, but not the directory itself. If SRC is a compressed file and the compression mode is gzip,bzip2, or xz, the directive unzips it to a directory. The dest command automatically creates dest and the missing parent directory

COPY a file to an image syntax: COPY < SRC >… “< < dest > | [SRC >”,… “< dest >”] hint: instruction is similar to the logic and the ADD, same Docker Daemon will from the build directory for a file or directory, dest to mirror the absolute path or relative to the path of the WORKDIR

ENTRYPOINT set the ENTRYPOINT syntax of the container: ENTRYPOINT [” executable “, “param1”, “param2”] ENTRYPOINT command param1 param2 The entry program is the program executed when the container starts. The last command in docker Run is passed as a parameter to the entry program. The entry program has two formats: If a Dockerfile has multiple entryPoints, only the last ENTRYPOINT instruction will take effect. If a script is used as an ENTRYPOINT, To ensure that the last program of the script can receive the semaphore, you can use exec or gosu at the end of the script to start the commands passed into the script. Note: In order to ensure that the container can accept the semaphore sent by docker Stop, it needs to start the program through exec. If the exec command is not added, two processes will appear when the container is started, and the container cannot exit normally with the Docker stop command (it cannot accept SIGTERM signal). After the timeout, the Docker stop sends SIGKILL to force the container to stop. Example: FROM Ubuntu < line wrap > ENTRYPOINT exec top -b

Run the following command to set the mount point of a container: VOLUME [” /data “] VOLUME /data1 /data2 When the container is started, the Docker Daemon creates mount points and initializes them with data from the image. You can mount host directories or data volume containers to these mount points

Set the USER name or UID of RUN CMD ENTRYPOINT

RUN CMD ENTRYPOINT ADD COPY WORKDIR < Path> If the working directory does not exist, then the Docker Daemon automatically creates a WORKDIR that can be called from multiple places in the Dockerfile. If it is followed by a relative location, WORKDIR /A WORKDIR B WORKDIR C, the final path is /A/B/C.

ONBUILD [INSTRUCTION] : generate a child image from this image. During the compilation process of the child image, the ONBUILD INSTRUCTION in the parent image will be executed first. All the compilation instructions can become hook instructions

Set the semaphore syntax that Docker Daemon sends to a container when the container exits. A semaphore can be a number or a semaphore name, such as 9 or SIGKILL. The numeric description of a semaphore is described briefly in Linux system administration

Added: ONBUILD process

During compilation, all ONBUILD images are read and recorded, and all ONBUILD instructions are recorded in the ONBUILD keyword of the configuration file of the image when no instructions are executed during the current compilation process. When the image is generated, all ONBUILD instructions are recorded in the ONBUILD keyword of the configuration file of the image. When executing the FROM command, the child image reads the ONBUILD instructions in the base image and executes them in sequence. Compilation is interrupted if the execution fails. When all onbuilds are successfully executed, the ONBUILD command in the child image will not inherit the ONBUILD command in the base image

Add: Differences between CMD ENTRYPOINT and RUN

The RUN command is used to set the scripts and programs to be executed during image compilation. After image compilation is complete, the life cycle of the RUN command ends

When the container is started, you can set the startup item through CMD and ENTRYPOINT. CMD is the default startup command of the container. If you add command at the end of the Docker run command, the startup program set by CMD in the image will be replaced. ENRTYPOINT is called an entry program and cannot be replaced by the command at the end of the Docker run command. Instead, the command is passed to ENTRYPOINT as a string

FROM Ubuntu ENTRYPOINT ["ps"] // Run the docker run --rm test command to start the container and print the ps output. // Run the docker run --rm test -ef command to start the container and print the ps -ef outputCopy the code

In docker Run, you can replace the entry program in the image with -entryPoint. In Dockerfile, there should be at least one CMD or EntryPoint directive. If both CMD and EntryPoint are defined, CMD will be passed to EntryPoint as a parameter

FROM Ubuntu ENTRYPOINT ["ps"] CMD ["-ef"] // Run the docker run --rm test command to start the container and print the ps -ef outputCopy the code

From: blog.csdn.net/qq_29999343…