在dockefile中cmd和entrypoint之间的区别 [英] difference between cmd and entrypoint in dockefile

查看:337
本文介绍了在dockefile中cmd和entrypoint之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Docker很新,对dockfile有一个简单的问题。我们可以在dock文件中写入entrypoint和CMD。似乎在创建容器期间执行entrypoint。并且CMD在启动容器期间执行。这是真的吗?

解决方案

不完全是:



ENTRYPOINT 配置一个容器,作为一个可执行文件运行。

所以执行 (或默认的 / bin / sh -c 是)。

  ENTRYPOINT [可执行文件,param1,param2](exec form,preferred)
ENTRYPOINT命令param1 param2(shell form)




命令行参数 docker运行< image> 将被附加在exec表单中的所有元素 ENTRYPOINT 之后,将覆盖使用CMD指定的所有元素。 / p>

shell表单防止使用任何 CMD 或运行命令行参数,但缺点是您的 ENTRYPOINT 将作为 / bin / sh -c 的子命令启动,该命令不通过信号。

这意味着可执行文件不会是容器的PID 1,并且不会收到Unix信号,因此您的可执行文件将不会从 docker stop< container>中收到SIGTERM。


您可以查看 CMD 作为 ENTRYPOINT

如果没有entrypoint(默认命令是 / bin / sh -c ), CMD 可以包含一个可执行文件。

如果 ENTRYPOINT 已经运行一个可执行文件,那么CMD参数就是这个命令的参数(如果 docker run 没有其他参数使用。






使用 docker start ,如是sue 1437 ,执行 ENTRYPOINT ,但只有参数来自 CMD (所以 CMD 被使用,但是您不能在命令行上用自己的参数覆盖它)。

如果要使用CMD,则需要 docker运行,而不是 docker start



实际上有一个最近的PR正在进行中( PR 19746 ),允许docker start命令采用可选的 - cmd -c )标志来指定要使用的cmd而不是cmd / entrypoint中的默认cmd。 p>




现在有一部分了解CMD和ENTRYPOINT的互动情况



  • Dockerfile应至少指定 CMD ENTRYPOINT 命令之一。在使用该容器作为可执行文件时,应该定义li>
  • ENTRYPOINT

  • 来定义 ENTRYPOINT 命令的默认参数或在容器中执行ad-hoc命令。在运行具有替代参数的容器时,li>
  • CMD 将被覆盖。


这意味着,如果您的Dockerfile包括:



  • CMD




    • 如果否 ENTRYPOINT :错误,不允许

    • ENTRYPOINT exec_entry p1_entry code> / bin / sh -c exec_entry p1_entry

    • ENTRYPOINT [exec_entry,p1_entry] 表示 exec_entry p1_entry


  • CMD [exec_cmd,p1_cmd] (一个命令,一个参数)




    • 如果否 ENTRYPOINT 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] 意味着 exec_entry p1_entry exec_cmd p1_cmd


  • CMD [p1_cmd ,p2_cmd]




    • 如果否 ENTRYPOINT p1_cmd p2_cmd

    • ENTRYPOINT exec_entry p1_entry c $ c> / bin / sh -c exec_entry p1_entry p1_cmd p2_cmd (good)

    • ENTRYPOINT [exec_entry,p1_entry ] 表示 exec_entry p1_entry p1_cmd p2_cmd


  • CMD exec_cmd p1_cmd




    • 如果否 ENTRYPOINT / bin / sh -c exec_cmd p1_cmd li>
    • ENTRYPOINT exec_entry p1_entry 表示 / bin / sh -c 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




I am new to docker, has a simple question to the dockfile. We can write entrypoint and CMD in dock file. Seems that entrypoint is executed during creating container. And CMD is executed during starting container. Is this true?

解决方案

Not exactly:

ENTRYPOINT configures a container that will run as an executable.
So it is always executed (or the default /bin/sh -c is).

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.

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.
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>.

You can view CMD as parameters for the ENTRYPOINT.
if there is no entrypoint (the default command is "/bin/sh -c"), CMD can include an executable.
If ENTRYPOINT already runs an executable, then the CMD arguments are parameters to this command (if docker run is used without additional parameters).


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).
IF you want to use CMD, you need docker run, not 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.


The Official Dockerfile documentation now has a section "Understand how CMD and ENTRYPOINT interact":

  • 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.

That means, if your Dockerfile includes:

  • No CMD:

    • if No ENTRYPOINT: error, not allowed
    • ENTRYPOINT exec_entry p1_entry means /bin/sh -c exec_entry p1_entry
    • ENTRYPOINT ["exec_entry", "p1_entry"] means exec_entry p1_entry
  • CMD ["exec_cmd", "p1_cmd"] (one command, one parameter)

    • if No ENTRYPOINT: exec_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"] means exec_entry p1_entry exec_cmd p1_cmd
  • CMD ["p1_cmd", "p2_cmd"]

    • if No ENTRYPOINT: p1_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"] means exec_entry p1_entry p1_cmd p2_cmd
  • CMD exec_cmd p1_cmd:

    • if No 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"] means exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd

这篇关于在dockefile中cmd和entrypoint之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆