Dockerfile Description:
customized image is actually customize each layer of the added configuration file. If we can modify each layer, installation, construction, operation commands are written in a script, use this script to build, customized image, then what can not duplicate the problem, mirroring build issues of transparency, the volume of the problem will solve. This script is Dockerfile. Dockerfile is a text file, within a section comprising instructions, each instruction to build a layer, and therefore the content of each instruction, is to describe how to construct the layer should be.
Written note items Dockerfile :
1, the command parameter, case insensitive instruction
2, the first row must be a non-comment FROM instruction
3, must be prepared Dockerfile in a directory, the directory is called the working directory (WORKSPACE)
. 4, the command file Dockerfile the first letter must be capitalized
5, mirrored the use of the file must be placed in the working directory or a subdirectory under the working directory, can not be placed in the parent directory
6, you can not put .dockeringnore to specify the file to the mirror through the hidden files, line is a file, you can use wildcards
7, based on dockerfile make a mirror, in essence, it is based on an existing mirror to make a new image
dockerfile Detailed instructions:
1, FROM
effect: Specify a base image. In addition to the notes, dockerfile first instruction.
format:
FROM docker.io/image:tags (FROM registry / image name (warehouse): Label (version number))
example:
FROM docker.io/http:v1 FROM nginx:latest
2, LABEL
role: set some metadata (specify author information)
Format:
LABEL author = ken (LABEL information)
Example:
LABEL author=ken
3, ENV
role: set the environment variable
format:
ENV key=value key=value (ENV key=value)
Example:
ENV workdir=/var/www/html
Note: The
variable is defined by the ENV can be passed into the container, however, when the container is created, if manually specify the value of a variable, then this value will overwrite any original image value
4, WORKDIR
effect: the equivalent of cd command (Statement working directory) to perform
format:
WORKDIR working directory
example:
The WORKDIR / var / WWW / HTML the WORKDIR $ Workdir (ENV environment variable) the WORKDIR / usr / local / apache2 /
5, RUN
effect: the mirror based on a command to be executed when building the container
a first stage, performed when building the mirror: Phase
Format:
the RUN command && command
Example:
RUN yum -y install httpd && systemctl enable httpd
6.EXPOSE
Role: Statement exposed port
format:
EXPOSE PORT/[PROTOCOL]
Example:
EXPOSE 80 8080 3306
注意:
如果需要外部访问,还需要启动容器时增加-p或者-P参数进行分配
7、COPY
作用:复制dockerfile工作目录下的文件移动至镜像中
格式:
复制单个:COPY <src> <dest> 复制多个:COPY [<src> <src> <src>… <dest>]
示例:
COPY passwd /etc/
注意:
1、源文件路径用相对路径,目标一般用绝对路径
2、也可以通配符
3、源文件必须在工作目录或者工作目录的子目录中
4、目标路径可以不存在,会自动创建
5、如果源文件是一个目录,会自动递归复制目录下的文件到目标位置,但是目录自身不会复制
6、如果复制多个文件,或者源文件中用了通配符,那么目标路径必须以 / 为结尾
8、ADD
作用:也是复制和COPY类似
格式:
ADD SRC DEST ADD [“<src>” “<src>” “<src>” “<dest>”]
例子:
ADD ken.tar.gz /etc/
注意:
ADD复制时会直接将tar包解压;也可以从网络下载文件到镜像,下载的tar无法解压。
9、VOLUME
作用:声明挂载点
格式:
VOLUME DEST (容器目录)
例子:
VOLUME /var/www/html
10.ONBUILD
作用:触发器 (当别人在使用你的镜像制作新的镜像的时候会触发一个动作)
格式:
ONBUILD 动作 ONBUILD 其他指令
例子:
ONBUILD COPY ken /app/ ONBUILD ADD https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/adwaita-qt-common-1.0-1.el7.x86_64.rpm /date
注意:
指定的命令在构建镜像时并不执行,用来实现当基于这个这个镜像做新镜像的时候要执行的命令
11.CMD
作用:执行指令,PID为1的指令
阶段:第二阶段,也就是将镜像构成成容器的时候执行
格式:
CMD 指令
CMD <命令> 相当于执行的是/bin/sh -c 命令,也相当于执行exec来运行命
CMD [“<命令>”, “<参数>”, “<参数>”]
CMD [“<参数>”, “<参数>”] <<< 需要借助于ENTRYPOINT指令
例子:
CMD nginx -g daemon off; CMD ["nginx","-g","daemon off"]
注意:
可以在启动容器的时候用指定的命令替换掉镜像所要执行的命令,
CMD指定容器启动是执行的命令,每个Dockerfile只能有一条CMD命令,
如果指定了多条,只有最后一条会被执行,
如果你在启动容器的时候也指定的命令,那么会覆盖Dockerfile构建的镜像里面的CMD命令。
12.ENTRYPOINT
作用:执行指令,PID为1的指令
格式:
ENTRYPOINT 指令 ENTRYPOINT [“执行命令”,”参数1″,”参数2″…]
例子:
ENTRYPOINT nginx -g daemon off; ENTRYPOINT ["nginx","-g","daemon off]
13. ARG
作用:定义变量,这个变量是用在第一阶段(构建镜像——build)
格式:
ARG 变量名=变量值
ARG示例:
ARG name=ken
注意:
Dockerfile中ENV 和 ARG的区别
在指定docker build 过程中传参数,要用ARG
在执行docker run的过程中传参数,要用ENV
ARG构建参数和 ENV 的效果一样,都是设置环境变量。所不同的是, ARG 所设置的构建环境的
环境变量,在将来容器运行时是不会存在这些环境变量的
14. USER
作用:指定运行容器时的用户名和UID,后续的RUN指令也会使用这里指定的用户
格式:
USER <UID>|<USERNAME>
USER示例:
user ken
注意;
该用户必须存在于容器的用户空间中(容器的文件系统的中的/etc/passwd中)
15. HEALTHCHECK
作用:docker daemon检查docker容器是否正常,如果异常会将该容器stop
将容器stop的条件
1)主进程停止了
2)主进程工作在了后台
格式:
HEALTHCHECK [options] CMD options –interval=#s|m 指定健康检查的时间间隔(例如:30s,30m) –timeout=#s|m 指定等待响应的超时时间 –start-period=#s|m 指定容器启动多久以后才可以做监控检查 –retries=# 指定重试次数 返回值 0:success
示例:
HEALTHCHECK --interval=5m --timeout=1s --retries=3 CMD curl http://10.220.5.138/ken.html || exit 1
16. SHELL
作用:可以用来指定系统中默认的shell类型
格式:
SHELL [“/bin/sh”, “-c”] (linux系统中)
SHELL示例:
SHELL ["/bin/sh","-c"]