Dockerfile 命令

  1. 1. #注释
  2. 2. ENV
  3. 3. .dockerignore
  4. 4. FROM
  5. 5. RUN
  6. 6. CMD
  7. 7. LABEL
  8. 8. EXPOSE
  9. 9. ADD
  10. 10. COPY
  11. 11. ENTRYPOINT
  12. 12. VOLUME
  13. 13. USER
  14. 14. WORKDIR
  15. 15. ARG
  16. 16. Dockerfile例子

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, COPYADD的工作目录

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.

转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 wind.kaisa@gmail.com

文章标题:Dockerfile 命令

本文作者:kaisawind

发布时间:2019-02-27, 08:36:49

最后更新:2020-07-21, 14:35:46

原始链接:https://kaisawind.gitee.io/2019/02/27/2019-02-27-docker/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

目录
×

喜欢就点赞,疼爱就打赏