entrypoint和cmd用在dockerfile中,它们都能够指定容器启动时运行的命令,在应用时,通常用entrypoint指定命令,cmd指定命令参数。

exec和shell

entrypoint和cmd的写法:exec模式、shell模式

exec模式:[]的写法,举荐应用

FROM ubuntuCMD [ "top" ]

容器运行时,top命令是1号过程。

shell模式:/bin/sh -c "cmd"的写法

FROM ubuntuCMD top

容器运行时,sh是1号过程,top是sh创立的子过程。

典型用法:CMD

简略的能够在CMD中指定:命令和参数

FROM ubuntuEXPOSE 8080CMD [ "nginx", "-s", "reload" ]

典型用法:EntryPoint + CMD (举荐)

通用的办法,在EntryPoint中指定命令,在CMD中指定参数

# prometheus通过CMD指定运行参数USER       nobodyEXPOSE     9090VOLUME     [ "/prometheus" ]WORKDIR    /prometheusENTRYPOINT [ "/bin/prometheus" ]        #exec写法:指定命令CMD        [ "--config.file=/etc/prometheus/prometheus.yml", \             "--storage.tsdb.path=/prometheus", \             "--web.console.libraries=/usr/share/prometheus/console_libraries", \             "--web.console.templates=/usr/share/prometheus/consoles" ]        #exec写法:指定参数

典型用法:kubernetes

在pod的spec中,能够通过command参数笼罩镜像中的EntryPoint参数:

# kubectl explain pod.spec.containers.commandDESCRIPTION:     Entrypoint array. Not executed within a shell. The docker image's     ENTRYPOINT is used if this is not provided. 

在pod的spec中,能够通过args参数笼罩镜像中的CMD参数:

# kubectl explain pod.spec.containers.argsDESCRIPTION:     Arguments to the entrypoint. The docker image's CMD is used if this is not     provided. 

所以,为了业务利用的image能够在kubernetes上运行,举荐在镜像的dockerfile中:entrypoint指定命令,cmd指令参数,以便在运行时能够灵便笼罩。

比方Prometheus的container在kubernetes中运行时,应用args笼罩镜像中的CMD参数:

......spec:  containers:  - args:    - --web.console.templates=/etc/prometheus/consoles    - --web.console.libraries=/etc/prometheus/console_libraries    - --config.file=/etc/prometheus/config_out/prometheus.env.yaml    - --storage.tsdb.path=/prometheus    - --storage.tsdb.retention.time=24h    - --web.enable-lifecycle    - --storage.tsdb.no-lockfile    - --web.route-prefix=/    image: 178.104.162.39:443/dev/huayun/amd64/prometheus:v2.20.0    imagePullPolicy: IfNotPresent