Very detailed tutorial on Dockerfile.
FROM
基础 image 的来源。
-
FROM <repository>:[tag]FROM busybox # will pull latest tag by default FROM busybox:latest # 默认使用hub.docker.com 仓库image FROM registry.cn-shenzhen.aliyuncs.com/abo/devops:v1 -
FROM <repository>@<digest>FROM alpine@sha256:d7342993700f8cd7aba8496c2d0e57be0666e80b4c441925fc6f9361fa81d10e # docker pull 通过digest方式 docker pull alpine@sha256:d7342993700f8cd7aba8496c2d0e57be0666e80b4c441925fc6f9361fa81d10e
MAINTAINER
添加维护者等信息。
-
MAINTAINER <information>注意:
MAINTAINER语法已被新的LABEL替代MAINTAINER "Peter<westlife@aliyun.com>"
LABEL
作用和 MAINTAINER 一样。
语法格式: LABEL <key>=<value> <key>=<value> ...
LABEL author="Peter<westlife@aliyun.com>"
COPY
复制文件或目录。
-
COPY <src> <dest>COPY test.tar.gz /data/web/ -
COPY ["<src>", ... "<dest>"]COPY ["test.tar.gz", "/data/web/"] -
注意事项
src支持通配符dest建议使用绝对路径- 路径中不能存在空白字符,常用第二种格式
src是目录时,仅复制其下层目录和文件,不复制src目录本身- 指定多个
src或使用通配符时,dest必须是同一个,且以/结尾
ADD
复制添加文件或目录。
-
ADD <src> <dest>ADD test.tar.gz index.html /data/web/ ADD http://aliyun.com/test.tar.gz /data/web ADD http://aliyun.com/test.tar.gz /data/web/注:压缩包会自动解压(仅本地);支持 URL 作为
src -
ADD ["<src>", ... "<dest>"]ADD ["http://5.5.5.100:8000/software/linux/nginx-1.18.0.tar.gz", "index.html", "/data/web/"] ADD ["test.tar.gz", "index.html", "/data/web/"]
VOLUME
在 image 中创建挂载点,用于挂载 docker host 上的卷或其他容器的卷。
-
VOLUME <mountpoint>VOLUME /data/mydata/ -
VOLUME ["<mountpoint>"]VOLUME ["/data/mydata/"]
注意:挂载点路径如果存在文件,
docker run后会将文件复制到新的挂载卷中
EXPOSE
容器运行时需要暴露的端口。
语法格式: EXPOSE <port>/<[tcp|udp]>
EXPOSE 80/tcp 2222/udp
ENV
定义环境变量,可被 ADD、COPY 等指令调用。
-
ENV <key> <value>ENV HTTP_ROOT /usr/local/nginx/html/ -
ENV <key>=<value>ENV HTTP_ROOT="/usr/local/nginx/html/" \ WORKDIR="/data/web/" \ PORT=80调用方式:
$HTTP_ROOT或${HTTP_ROOT}
RUN
在 docker build 过程中运行的命令。
-
RUN <command>RUN mkdir -p /data/web -
RUN ["<command>", "<参数1>", "<参数n>"]ENV HTTP_ROOT="/data/test/" RUN ["/bin/bash", "-c", "mkdir -p $HTTP_ROOT"]
CMD
docker run 过程中容器启动时执行的命令。
注意:
CMD指定的命令可被docker run后面的命令覆盖。RUN是构建时命令,CMD是运行时命令。
-
CMD <完整的命令以及参数>CMD /bin/httpd -f -h $HTTP_ROOT -
CMD ["<command>", "<参数1>", "<参数n>"]CMD ["/bin/httpd", "-f", "-h", "/data/web/html/"]
ENTRYPOINT
类似 CMD,但其指定的命令不可被 docker run 后面的命令覆盖。
-
ENTRYPOINT ["<command>"]ENTRYPOINT ["/bin/httpd"] CMD ["-f", "-h", "/data/web/html"] -
示例
FROM nginx:1.18-alpine LABEL author="Jason Hu" ENV NGX_ROOT="/data/nginx/html/" ADD entrypoint.sh /bin/ ADD index.html $NGX_ROOT RUN chmod +x /bin/entrypoint.sh ENTRYPOINT ["/bin/entrypoint.sh"] CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
HEALTHCHECK
用于容器的健康检查。
语法格式: HEALTHCHECK [options] CMD <command>
-
options 选项:
--interval=<value>(默认 30s)--timeout=<value>(默认 30s)--start-period=<value>(默认 0s)--retries=<value>(默认 3)
-
返回值:
0— healthy (健康)1— unhealthy (不健康)2— reserved (保留)
HEALTHCHECK --interval=5m --timeout=3s --start-period=5s \
CMD curl -f http://localhost/ || exit 1
SHELL
指定默认 shell 来运行命令。
语法格式: SHELL ["<command>", "<参数>"]
Linux 默认:["/bin/sh", "-c"] | Windows 默认:["CMD", "/S", "/C"]
STOPSIGNAL
容器接收的 Unix 信号来退出。
语法格式: STOPSIGNAL <signal>
ARG
在 docker build 过程中定义的构建变量。
语法格式: ARG <key>=<value>
ARG author=scofield
LABEL maintainer=${author}
使用:docker build --build-arg author=westlife -t test:v10 .
ONBUILD
定义一个触发器,当本 image 被用作其他 Dockerfile 的基础镜像时执行。
语法格式: ONBUILD <Dockerfile指令>
ONBUILD ADD http://nginx.org/download/nginx-1.19.4.tar.gz /data/nginx/