Dockerfile命令

了解ARG和FROM之间的交互方式

ARG CODE_VERSION=latest  
FROM base:${CODE_VERSION} 

Docker其实也预定了一些ARG方便我们构建的时候使用代理

HTTP_PROXY
http_proxy
HTTPS_PROXY
https_proxy
FTP_PROXY
ftp_proxy
NO_PROXY
no_proxy

RUN 执行命令,使用其它shell环境,请使用第三种方式传入所需的shell

RUN /bin/bash -c 'source $HOME/.bashrc; echo $HOME'
RUN /bin/bash -c 'source $HOME/.bashrc;echo $HOME'
RUN ["/bin/bash", "-c", "echo hello"]
RUN echo "Hello world"

MAINTAINER 维护者(建议使用LABLE)

MAINTAINER os4top16
LABEL maintainer="os4top16@gmail.com"

LABEL 标签

父镜像(基础镜像)中包含的标签在新镜像中任然存在,创建重复的标签,则最近应用的值将会覆盖之前设置的值。可以使用docker inspect查看镜像中的标签

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."
LABEL multi.label1="value1" multi.label2="value2" other="value3"
LABEL multi.label1="value1" \
multi.label2="value2" \
other="value3"

EXPOSE 设置监听在指定的端口,默认tcp协议

要在运行容器时实际发布端口,请在docker run上使用-p标志发布并映射一个或多个端口,或使用-P标志发布所有公开的端口并将其映射到高阶端口(会随机指定端口)

EXPOSE 80/tcp
EXPOSE 80/udp
[root@localhost data]# docker run -itd --name 03 -P nginx
8f90cfdba786447a9445719a5ac1eed5272659c80664e9d1500a9951b8801ebb
[root@localhost data]# docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                              NAMES
8f90cfdba786        nginx                        "nginx -g 'daemon of…"   3 seconds ago       Up 2 seconds        0.0.0.0:32768->80/tcp              03

ENV 创建环境变量 两种形式(form)

  • ENV myName="John Doe" myDog=Rex The Dog myCat=fluffy
  • ENV myName John Doe
ENV myDog Rex The Dog
ENV myCat fluffy

ADD 类似于COPY,但是支持文件的下载和自动解压

  • ADD [--chown=:] ...
  • ADD [--chown=:] ["",... ""]
ADD hom* /mydir/ ADD hom?.txt /mydir/
ADD [`https://firebasestorage.googleapis.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-M0facU9lCTIfJSgNM-X%2F-M3iUnQMA0W2SW28aJz0%2F-M3icjxFY2__cCFQ_DIw%2Fopenresty.zip?alt=media&token=41f254a9-6527-4fe3-a099-3f1373802dd8`](https://firebasestorage.googleapis.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-M0facU9lCTIfJSgNM-X%2F-M3iUnQMA0W2SW28aJz0%2F-M3icjxFY2__cCFQ_DIw%2Fopenresty.zip?alt=media&token=41f254a9-6527-4fe3-a099-3f1373802dd8) `/mydir/`
ADD test relativeDir/ ADD test /absoluteDir/
ADD --chown=55:mygroup files* /somedir/
ADD --chown=bin files* /somedir/
ADD --chown=1 files* /somedir/
ADD --chown=10:11 files* /somedir/

COPY

  • COPY [--chown=:] ...
  • COPY [--chown=:] ["",... ""]
COPY test relativeDir/   
COPY test /absoluteDir/
  • COPY还能指定uid:gid,如果容器的rootfs里没有文件/etc/passwd和/etc/group文件只能使用数字不能使用组名
COPY --chown=55:mygroup files _/somedir/ 
COPY --chown=bin files_ /somedir/ 
COPY --chown=1 files _/somedir/ 
COPY --chown=10:11 files_ /somedir/

CMD容器启动命令

CMD echo $HOME
CMD [ "sh", "-c", "echo $HOME"]
CMD ["nginx", "-g", "daemon off;"]

CMD service nginx start 会被理解为 CMD [ "sh", "-c", "service nginx start"],因此主进程实际上是 sh。那么当 service nginx start 命令结束后,sh 也就结束了,sh 作为主进程退出了,自然就会令容器退出。

ENTRYPOINT 可执行文件

ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred) 
ENTRYPOINT command param1 param2 (shell form)
ENTRYPOINT ["top", "-b"]
$ docker run -it --rm --name test top -H
ENTRYPOINT exec top -b
$ docker run -it --rm --name test top

VOLUME 创建具有指定名称的安装点

VOLUME /myvol

USER

USER [:] or USER [:]

USER yundd

WORKDIR 设置RUN,CMD,ENTRYPOINT,COPY和ADD 工作目录

WORKDIR /path/to/workdir
WORKDIR /a WORKDIR b WORKDIR c RUN pwd 

ONBUILD 已构建

ONBUILD ADD . /app/src ONBUILD RUN /usr/local/bin/python-build --dir /app/src

HEALTHCHECK 健康检查

  • HEALTHCHECK [OPTIONS] CMD command (check container health by running a command inside the container)
  • HEALTHCHECK NONE (disable any healthcheck inherited from the base image)
--interval=DURATION (default: 30s) 间隔 
--timeout=DURATION (default: 30s) 超时时间 
--start-period=DURATION (default: 0s) 开始时间 --retries=N (default: 3) 重试
HEALTHCHECK --interval=5m --timeout=3s \
  CMD curl -f http://localhost/ || exit 1
[root@localhost openresty]# docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS                             PORTS                              NAMES
2481edb4bfd5        yundd:v4                     "/usr/bin/openresty …"   21 seconds ago      Up 19 seconds (health: starting)   0.0.0.0:800->800/tcp               openresty

SHELL

SHELL ["executable", "parameters"]

FROM microsoft/windowsservercore

# Executed as cmd /S /C echo default
RUN echo default

# Executed as cmd /S /C powershell -command Write-Host default
RUN powershell -command Write-Host default

# Executed as powershell -command Write-Host hello
SHELL ["powershell", "-command"]
RUN Write-Host hello

# Executed as cmd /S /C echo hello
SHELL ["cmd", "/S", "/C"]
RUN echo hello
posted @ 2020-04-02 17:52  duxiao  阅读(250)  评论(0编辑  收藏  举报