Dockerfile 命令 - Wed, Feb 27, 2019
Dockerfile文件命令
Docker官方网站地址(https://docs.docker.com/v17.09/engine/reference/builder/#usage)
1. #
注释
# This is commit
# 这是注释
2. ENV
ENV <key> <value>
ENV <key>=<value> ...
FROM busybox
ENV foo /bar
WORKDIR ${foo} # WORKDIR /bar
ADD . $foo # ADD . /bar
COPY \$foo /quux # COPY $foo /quux
环境变量可以使用$variable_name
或者${variable_name}
。
特殊用法:
${variable:-word}
如果变量存在,它的值就会是变量的值;如果变量不存在,它的值会是word
${variable:+word}
如果变量存在,它的值是word
;如果变量不存在,它的值会是空字符串
可以使用转义字符\
,将$foo
变为字符串而不是变量
3. .dockerignore
Dockerfile进行构建镜像时,会将path下的所有文件复制到临时路径下,然后进行构建。尤其是前端有node_modules
文件夹,会非常花费时间。
所以需要使用.dockerignore
文件将构建镜像时不需要的文件忽略。用法类似.gitignore
文件
4. FROM
FROM <image> [AS <name>]
FROM <image>[:<tag>] [AS <name>]
FROM <image>[@<digest>] [AS <name>]
FROM
前面可以使用ARG
ARG CODE_VERSION=latest
FROM base:${CODE_VERSION}
CMD /code/run-app
FROM extras:${CODE_VERSION}
CMD /code/run-extras
但是,由于ARG
是在镜像构建之前设置的,所以在镜像构建中是无法获取ARG
变量值。
为了能够获取ARG
变量值,需要在镜像构建中重新声明ARG
变量。
ARG VERSION=latest
FROM busybox:$VERSION
ARG VERSION
RUN echo $VERSION > image_version
5. RUN
RUN
有两种用法:
RUN <command>
(shell格式,默认会被/bin/sh -c
(Linux) 或者cmd /S /C
(Windows))RUN ["executable", "param1", "param2"]
(exec 格式)
RUN
可以使用换行符来连接多个指令
RUN /bin/bash -c 'source $HOME/.bashrc; \
echo $HOME'
6. CMD
CMD
有三种用法:
CMD ["executable","param1","param2"]
(exec格式,首选)CMD ["param1","param2"]
(作为ENTRYPOINT
的参数)CMD command param1 param2
(shell格式)
一个Dockerfile文件只能有一个CMD
7. LABEL
LABEL <key>=<value> <key>=<value> <key>=<value> ...
LABEL
标签可以向镜像添加元数据
LABEL "com.example.vendor"="ACME Incorporated"
LABEL com.example.label-with-value="foo"
LABEL version="1.0"
LABEL description="This text illustrates \
that label-values can span multiple lines."
8. EXPOSE
EXPOSE <port> [<port>/<protocol>...]
EXPOSE
不是真正的暴露端口,而是用来说明容器里那些端口可以进行暴露。
如果要暴露端口,仍然需要使用-p
9. ADD
ADD
有两种用法:
ADD <src>... <dest>
ADD ["<src>",... "<dest>"]
(如果路径中包含空格)
ADD
支持通配符
ADD hom* /mydir/ # adds all files starting with "hom"
ADD hom?.txt /mydir/ # ? is replaced with any single character, e.g., "home.txt"
目标目录需要注意/
ADD test relativeDir/ # adds "test" to `WORKDIR`/relativeDir/
ADD test /absoluteDir/ # adds "test" to /absoluteDir/
10. COPY
COPY
有两种用法:
COPY <src>... <dest>
COPY ["<src>",... "<dest>"]
(如果路径中包含空格)
COPY
支持通配符
COPY hom* /mydir/ # adds all files starting with "hom"
COPY hom?.txt /mydir/ # ? is replaced with any single character, e.g., "home.txt"
目标目录需要注意/
COPY test relativeDir/ # adds "test" to `WORKDIR`/relativeDir/
COPY test /absoluteDir/ # adds "test" to /absoluteDir/
COPY
能够解压.tar.gz
,但是ADDD
不能。
11. ENTRYPOINT
ENTRYPOINT
有两种用法:
ENTRYPOINT ["executable", "param1", "param2"]
(exec form, preferred)ENTRYPOINT command param1 param2
(shell form)
12. VOLUME
VOLUME ["/data"]
VOLUME
提供镜像能够对host的挂载点。
13. USER
USER <user>[:<group>] or
USER <UID>[:<GID>]
14. WORKDIR
WORKDIR /path/to/workdir
设置RUN
, CMD
, ENTRYPOINT
, COPY
和 ADD
的工作目录
WORKDIR
可以有多个,多个工作目录是拼接的
WORKDIR /a
WORKDIR b
WORKDIR c
RUN pwd
pwd
是/a/b/c
15. ARG
ARG <name>[=<default value>]
Dockerfile中使用的变量,容器启动时的参数
16. Dockerfile例子
# Nginx
#
# VERSION 0.0.1
FROM ubuntu
LABEL Description="This image is used to start the foobar executable" Vendor="ACME Products" Version="1.0"
RUN apt-get update && apt-get install -y inotify-tools nginx apache2 openssh-server
# Firefox over VNC
#
# VERSION 0.3
FROM ubuntu
# Install vnc, xvfb in order to create a 'fake' display and firefox
RUN apt-get update && apt-get install -y x11vnc xvfb firefox
RUN mkdir ~/.vnc
# Setup a password
RUN x11vnc -storepasswd 1234 ~/.vnc/passwd
# Autostart firefox (might not be the best way, but it does the trick)
RUN bash -c 'echo "firefox" >> /.bashrc'
EXPOSE 5900
CMD ["x11vnc", "-forever", "-usepw", "-create"]
# Multiple images example
#
# VERSION 0.1
FROM ubuntu
RUN echo foo > bar
# Will output something like ===> 907ad6c2736f
FROM ubuntu
RUN echo moo > oink
# Will output something like ===> 695d7793cbe4
# You'll now have two images, 907ad6c2736f with /bar, and 695d7793cbe4 with
# /oink.