简体   繁体   English

cmd和dockefile中的入口点之间的区别

[英]difference between cmd and entrypoint in dockefile

I am new to docker, has a simple question to the dockfile. 我是Docker的新手,对ockfile有一个简单的问题。 We can write entrypoint and CMD in dock file. 我们可以在停靠文件中写入入口点和CMD。 Seems that entrypoint is executed during creating container. 似乎在创建容器期间执行了入口点。 And CMD is executed during starting container. 并且在启动容器期间执行CMD。 Is this true? 这是真的?

Not exactly: 不完全是:

ENTRYPOINT configures a container that will run as an executable. ENTRYPOINT配置一个将作为可执行文件运行的容器。
So it is always executed (or the default /bin/sh -c is). 因此它总是被执行(或默认的/bin/sh -c是)。

ENTRYPOINT ["executable", "param1", "param2"] (exec form, preferred)
ENTRYPOINT command param1 param2 (shell form)

Command line arguments to docker run <image> will be appended after all elements in an exec form ENTRYPOINT , and will override all elements specified using CMD. ENTRYPOINT docker run <image>命令行参数将附加在exec形式ENTRYPOINT所有元素之后,并将覆盖使用CMD指定的所有元素。

The shell form prevents any CMD or run command line arguments from being used, but has the disadvantage that your ENTRYPOINT will be started as a subcommand of /bin/sh -c , which does not pass signals. shell形式可防止使用任何CMD或运行命令行参数,但具有以下缺点: ENTRYPOINT将作为/bin/sh -c的子命令启动,该子命令不传递信号。
This means that the executable will not be the container's PID 1 - and will not receive Unix signals - so your executable will not receive a SIGTERM from docker stop <container> . 这意味着可执行文件将不是容器的PID 1,并且不会接收Unix信号,因此您的可执行文件将不会从docker stop <container>接收到SIGTERM。

You can view CMD as parameters for the ENTRYPOINT . 您可以将CMD视为ENTRYPOINT参数。
if there is no entrypoint (the default command is " /bin/sh -c "), CMD can include an executable. 如果没有入口点(默认命令为“ /bin/sh -c ”),则CMD可以包含可执行文件。
If ENTRYPOINT already runs an executable, then the CMD arguments are parameters to this command (if docker run is used without additional parameters). 如果ENTRYPOINT已经运行了可执行文件,则CMD参数是该命令的参数(如果使用docker run而没有其他参数)。


With docker start , as mentioned in issue 1437 , the ENTRYPOINT is executed, but only with parameters from CMD (so CMD is used, but you cannot override it with parameters of your own on the command-line). 使用docker start ,如问题1437中所述 ,将执行ENTRYPOINT ,但仅使用CMD参数(因此使用CMD ,但不能在命令行上使用您自己的参数覆盖它)。
IF you want to use CMD, you need docker run , not docker start . 如果要使用CMD,则需要docker run ,而不是docker start

There actually is a recent PR in progress ( PR 19746 ) which allows for the docker start command to take an optional --cmd ( -c ) flag to specify the cmd to use instead of the default one from cmd/entrypoint. 实际上,最近有一个正在进行的PRPR 19746 ),它允许--cmd start命令使用可选的--cmd-c )标志来指定要使用的cmd,而不是cmd / entrypoint中的默认cmd。


The Official Dockerfile documentation now has a section " Understand how CMD and ENTRYPOINT interact ": Dockerfile官方文档现在具有“ 了解CMD和ENTRYPOINT如何交互 ”部分:

  • Dockerfile should specify at least one of CMD or ENTRYPOINT commands. Dockerfile应至少指定CMDENTRYPOINT命令之一。
  • ENTRYPOINT should be defined when using the container as an executable. 使用容器作为可执行文件时,应定义ENTRYPOINT
  • 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用作为ENTRYPOINT命令定义默认参数或在容器中执行自定义命令的方式。
  • CMD will be overridden when running the container with alternative arguments. 使用替代参数运行容器时, CMD将被覆盖。

That means, if your Dockerfile includes: 这意味着,如果您的Dockerfile包含:

  • No CMD : 没有CMD

    • if No ENTRYPOINT : error, not allowed 如果没有ENTRYPOINT :错误,则不允许
    • ENTRYPOINT exec_entry p1_entry means /bin/sh -c exec_entry p1_entry ENTRYPOINT exec_entry p1_entry表示/bin/sh -c exec_entry p1_entry
    • ENTRYPOINT ["exec_entry", "p1_entry"] means exec_entry p1_entry ENTRYPOINT ["exec_entry", "p1_entry"]表示exec_entry p1_entry
  • CMD ["exec_cmd", "p1_cmd"] (one command, one parameter) CMD ["exec_cmd", "p1_cmd"] (一个命令,一个参数)

    • if No ENTRYPOINT : exec_cmd p1_cmd , 如果没有ENTRYPOINTexec_cmd p1_cmd
    • ENTRYPOINT exec_entry p1_entry means /bin/sh -c exec_entry p1_entry exec_cmd p1_cmd ENTRYPOINT exec_entry p1_entry表示/bin/sh -c exec_entry p1_entry exec_cmd p1_cmd
    • ENTRYPOINT ["exec_entry", "p1_entry"] means exec_entry p1_entry exec_cmd p1_cmd ENTRYPOINT ["exec_entry", "p1_entry"]表示exec_entry p1_entry exec_cmd p1_cmd
  • CMD ["p1_cmd", "p2_cmd"]

    • if No ENTRYPOINT : p1_cmd p2_cmd 如果没有ENTRYPOINTp1_cmd p2_cmd
    • ENTRYPOINT exec_entry p1_entry means /bin/sh -c exec_entry p1_entry p1_cmd p2_cmd (good) ENTRYPOINT exec_entry p1_entry表示/bin/sh -c exec_entry p1_entry p1_cmd p2_cmd (好)
    • ENTRYPOINT [“exec_entry”, “p1_entry”] means exec_entry p1_entry p1_cmd p2_cmd ENTRYPOINT [“exec_entry”, “p1_entry”]表示exec_entry p1_entry p1_cmd p2_cmd
  • CMD exec_cmd p1_cmd : CMD exec_cmd p1_cmd

    • if No ENTRYPOINT : /bin/sh -c exec_cmd p1_cmd 如果没有ENTRYPOINT/bin/sh -c exec_cmd p1_cmd
    • ENTRYPOINT exec_entry p1_entry means /bin/sh -c exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd ENTRYPOINT exec_entry p1_entry表示/bin/sh -c exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd
    • ENTRYPOINT [“exec_entry”, “p1_entry”] means exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd ENTRYPOINT [“exec_entry”, “p1_entry”]表示exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM