Docker容器:Dockerfile实践
Dockerfile
Dockerfile用来创建一个自定义的image,包含了用户指定的软件依赖等。当前目录下包含Dockerfile,使用命令build来创建新的image,并命名
docker build -t edwardsbean/centos6-jdk1.7 .
关键字
- FROM:
基于哪个镜像
- ARG:
FROM instructions support variables that are declared by any ARG instructions that occur before the first FROM.
ARG CODE_VERSION=latest
FROM base:${CODE_VERSION}
CMD /code/run-app
FROM extras:${CODE_VERSION}
CMD /code/run-extras
- RUN:
安装软件用
两种形式:
RUN
- MAINTAINER:
镜像创建者
- CMD:
container启动时执行的命令,但是一个Dockerfile中只能有一条CMD命令,多条则只执行最后一条CMD. CMD主要用于container时启动指定的服务,当docker run command的命令匹配到CMD command时,会替换CMD执行的命令。如:
三种形式:
CMD [“executable”,”param1”,”param2”] (exec form, this is the preferred form)
CMD [“param1”,”param2”] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)
FROM ubuntu
CMD ["/usr/bin/wc","--help"]
- LABEL
LABEL
- EXPOSE
EXPOSE
- ENV
ENV
- ADD
ADD [–chown=
ADD [–chown=
- COPY
COPY [–chown=
COPY [–chown=
- ENTRYPOINT
The exec form:
ENTRYPOINT ["executable", "param1", "param2"]
The shell form:
ENTRYPOINT command param1 param2
例如:
FROM ubuntu
ENTRYPOINT ["top", "-b"]
CMD ["-c"]
- CMD 和 ENTRYPOINT
Both CMD and ENTRYPOINT instructions define what command gets executed when running a container. There are few rules that describe their co-operation.
Dockerfile should specify at least one of CMD or ENTRYPOINT commands.
ENTRYPOINT should be defined when using the container as an executable.
CMD should be used as a way of defining default arguments for an ENTRYPOINT command or for executing an ad-hoc command in a container.
CMD will be overridden when running the container with alternative arguments.
- VOLUME
VOLUME [“/data”]
- USER
USER <user>[:<group>]
USER <UID>[:<GID>]
- WORKDIR
WORKDIR /path/to/workdir
- ONBUILD
ONBUILD
- STOPSIGNAL
STOPSIGNAL signal
- HEALTHCHECK
HEALTHCHECK --interval=5m --timeout=3s CMD curl -f http://localhost/ || exit 1
- 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
refs
https://docs.docker.com/engine/reference