Dockerfile image construction
1. Docker image layering
1.FROM 后面跟基础镜像
2.ADD run.sh/ 脚本
3.VOLUME 挂载共享空间 数据卷
4.CMD ["./run,sh"] 命令执行脚本
- Each instruction in the Dockerfile creates a new image layer
- The mirror layer will be cached and reused
- When the instructions of the Dockerfile are modified, the copied files are changed, or the variables specified when the mirror is built are different, the corresponding mirror layer cache will become invalid
- After a certain layer of mirroring cache is invalid, the mirroring layer cache after it will be invalid
- The image layer is immutable. If you add a file to a layer and then delete it in the next layer, the image will still contain the file
2. Create based on the existing image container
1.docker create -it 原镜像名 /bin/bash
2.docker commit -m “new” -a “chen” 已有容器id 新镜像名:标签
-m:说明信息
-a:作者信息
-p:生成过程中停止容器的运行
docker images | grep 标签
Three, create based on local template
1.导入本地镜像debian-7.0-x86-minimal.tar.gz
2.cat debian-7.0-x86-minimal.tar.gz | docker import - 镜像名:标签
3.docker images | grep 标签
Fourth, create based on Dockerfile
- Dockerfile is a file composed of a set of instructions
- The dockerfile structure is divided into four parts:
basic image information,
maintainer information,
image operation instructions
, and execute instructions when the container starts - Dockerfile supports one instruction per line, each instruction can carry multiple parameters, and supports the use of comments starting with "#"
1.Dockerfile operation instructions
instruction | meaning |
---|---|
FROM mirror | Specify the image on which the new image is based. The first instruction must be the FROM instruction, and a FROM instruction is required for each image created. |
MAINTAINER first name | Describe the maintainer information of the new image |
RUN command | Execute the command on the mirror based on it and submit it to the new mirror |
CMD ["program to be run", "parameter 1", "parameter 2"] | The command or script to be run when the instruction starts the container, the Dockerfile can only have one CMD command, if more than one is specified, only the last one can be executed |
EXPOSE port number | Specify the port to be opened when the new image is loaded into Docker (EXPOSE exposes the internal port of the container, which needs to be mapped to an external port) |
ENV environment variable variable value | Set the value of an environment variable, which will be used by RUN later |
ADD source file/directory target file/directory | Copy the source file to the target file (the difference from COPY is to decompress the local tar file into the mirror) |
COPY source file/directory target file/directory | Copy the file/directory on the local host to the target location, the source file/directory should be in the same directory as the Dockerfile |
VOLUME ["Directory"] | Create a mount point in the container (VOLUME is a directory in the host that is mounted to the container) |
USER username/UID | Specify the user when running the container |
WORKDIR path | Specify the working directory for the subsequent RUN, CMD, and ENTRYPOINT (WORKDIR is similar to cd, but only switches the directory once, and the subsequent RUN command can write the relative path) |
ONBUILD command | Specify the command to run when the generated image is used as a base image |
HEALTHCHECK | health examination |
-
The difference between CMD and ENTRYPOINT
-
The CMD instruction can specify the command to be executed by default when the container starts, but it can be overridden by the parameters of the docker run command.
-
The ENTRYPOINT instruction is similar to the CMD. It is also a command to be executed when the user specifies the container to start, but if there is also a CMD instruction in the dockerfile, the parameters in the CMD will be appended to the ENTRYPOINT instruction. If the docker run command carries a parameter at this time, this parameter will override the parameter of the CMD instruction and will also be appended to the ENTRYPOINT instruction.
-
In this way, when the container is started, the parameter part of the ENTRYPOINT instruction will be executed.
-
It can be seen that the ENTRYPOINT instruction has a higher priority.
-
Priority: ENTRYPOINT>CMD>docker run
-
For directories, the COPY and ADD commands have the same characteristics :
only copy the contents of the directory without including the directory itself. The
difference
COPY is just copying
ADD: the object of copying, decompression, and manipulation is not only a file, but also a URL