RUN vs CMD vs ENTRYPOINT - 每天5分钟玩转 Docker 容器技术(17)
<p class="wihudong" style="margin: 0"><span style="font-size: 14px"><span style="color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px">RUN、CMD 和 ENTRYPOINT 这三个 Dockerfile 指令看上去很类似,很容易混淆。本节将通过实践详细讨论它们的区别。</span></span></p><p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">简单的说:</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">RUN 执行命令并创建新的镜像层,RUN 经常用于安装软件包。</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">CMD 设置容器启动后默认执行的命令及其参数,但 CMD 能够被 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)">docker run</code> 后面跟的命令行参数替换。</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">ENTRYPOINT 配置容器启动时运行的命令。</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">下面我们详细分析。</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left"><strong><span style="font-size: 24px">Shell 和 Exec 格式</span></strong></p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">我们可用两种方式指定 RUN、CMD 和 ENTRYPOINT 要运行的命令:Shell 格式和 Exec 格式,二者在使用上有细微的区别。</p>
<p style="text-align: left"><span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Shell 格式</span></p>
<p style="text-align: left"><span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"><instruction> <command></span></p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left"> <br>例如:<span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px"><br></span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">RUN apt-get install python3 </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">CMD echo "Hello world" </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENTRYPOINT echo "Hello world" </span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px"><br>当指令执行时,shell 格式底层会调用 /bin/sh -c <command></span>
<span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px">。</span><br>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
例如下面的 Dockerfile 片段:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENV name Cloud Man </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENTRYPOINT echo "Hello, $name" </span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<br>执行 docker run <image> 将输出:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Hello, Cloud Man</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
注意环境变量 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)">name</code> 已经被值 <code style="font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-size: 12.96px; padding: 2px 4px; color: rgba(0, 0, 0, 1); border-radius: 3px; background: rgba(234, 234, 235, 1)">Cloud Man</code> 替换。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
下面来看 Exec 格式。
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Exec 格式</span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"><instruction> ["executable", "param1", "param2", ...]</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
例如:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">RUN ["apt-get", "install", "python3"] </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">CMD ["/bin/echo", "Hello world"] </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENTRYPOINT ["/bin/echo", "Hello world"]</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
当指令执行时,会直接调用 <command>,不会被 shell 解析。<br style="text-align: left">例如下面的 Dockerfile 片段:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENV name Cloud Man </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENTRYPOINT ["/bin/echo", "Hello, $name"]</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
运行容器将输出:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Hello, $name</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
注意环境变量“name”没有被替换。<br style="text-align: left">如果希望使用环境变量,照如下修改
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENV name Cloud Man </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENTRYPOINT ["/bin/sh", "-c", "echo Hello, $name"]</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
运行容器将输出:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Hello, Cloud Man</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<strong style="color: rgba(0, 0, 0, 1)">CMD 和 ENTRYPOINT 推荐使用 Exec 格式</strong>,因为指令可读性更强,更容易理解。RUN 则两种格式都可以。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<strong><span style="font-size: 24px">RUN</span></strong>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
RUN 指令通常用于安装应用和软件包。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
RUN 在当前镜像的顶部执行命令,并通过创建新的镜像层。Dockerfile 中常常包含多个 RUN 指令。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
RUN 有两种格式:
</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="text-align: left">
Shell 格式:RUN
</p>
</li>
<li>
<p style="text-align: left">
Exec 格式:RUN ["executable", "param1", "param2"]
</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
下面是使用 RUN 安装多个包的例子:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">RUN apt-get update && apt-get install -y \ </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"> bzr \</span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"> cvs \</span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"> git \</span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"> mercurial \</span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)"> subversion</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
注意:<strong style="color: rgba(0, 0, 0, 1)">apt-get update 和 apt-get install 被放在一个 RUN 指令中执行</strong>,这样能够保证每次安装的是最新的包。如果 apt-get install 在单独的 RUN 中执行,则会使用 apt-get update 创建的镜像层,而这一层可能是很久以前缓存的。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<strong><span style="font-size: 24px">CMD</span></strong>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
CMD 指令允许用户指定容器的默认执行的命令。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
此命令会在容器启动且 docker run 没有指定其他命令时运行。
</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
如果 docker run 指定了其他命令,CMD 指定的默认命令将被忽略。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
如果 Dockerfile 中有多个 CMD 指令,只有最后一个 CMD 有效。
</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
CMD 有三种格式:
</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
Exec 格式:CMD ["executable","param1","param2"]<br style="text-align: left">这是 CMD 的推荐格式。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
CMD ["param1","param2"] 为 ENTRYPOINT 提供额外的参数,此时 ENTRYPOINT 必须使用 Exec 格式。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
Shell 格式:CMD command param1 param2
</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
Exec 和 Shell 格式前面已经介绍过了。<br style="text-align: left">第二种格式 CMD ["param1","param2"] 要与 Exec 格式 的 ENTRYPOINT 指令配合使用,其用途是为 ENTRYPOINT 设置默认的参数。我们将在后面讨论 ENTRYPOINT 时举例说明。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
下面看看 CMD 是如何工作的。Dockerfile 片段如下:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">CMD echo "Hello world"</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
运行容器 docker run -it 将输出:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Hello world</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
但当后面加上一个命令,比如 docker run -it /bin/bash,CMD 会被忽略掉,命令 bash 将被执行:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">root@10a32dc7d3d3:/#</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<strong><span style="font-size: 24px">ENTRYPOINT</span></strong>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
ENTRYPOINT 指令可让容器以应用程序或者服务的形式运行。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
ENTRYPOINT 看上去与 CMD 很像,它们都可以指定要执行的命令及其参数。不同的地方在于 ENTRYPOINT 不会被忽略,一定会被执行,即使运行 docker run 时指定了其他命令。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
ENTRYPOINT 有两种格式:
</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
Exec 格式:ENTRYPOINT ["executable", "param1", "param2"] 这是 ENTRYPOINT 的推荐格式。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
Shell 格式:ENTRYPOINT command param1 param2
</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
在为 ENTRYPOINT 选择格式时必须小心,因为这两种格式的效果差别很大。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<span style="font-size: 18px"><strong style="color: rgba(0, 0, 0, 1)">Exec 格式</strong></span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
ENTRYPOINT 的 Exec 格式用于设置要执行的命令及其参数,同时可通过 CMD 提供额外的参数。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
ENTRYPOINT 中的参数始终会被使用,而 CMD 的额外参数可以在容器启动时动态替换掉。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
比如下面的 Dockerfile 片段:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">ENTRYPOINT ["/bin/echo", "Hello"] </span>
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">CMD ["world"]</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
当容器通过 docker run -it 启动时,输出为:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Hello world</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
而如果通过 docker run -it CloudMan 启动,则输出为:
</p>
<p style="text-align: left">
<span style="font-size: 12px; background-color: rgba(216, 216, 216, 1)">Hello CloudMan</span>
</p>
<p> </p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<span style="font-size: 18px"><strong style="color: rgba(0, 0, 0, 1)">Shell 格式</strong></span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
ENTRYPOINT 的 Shell 格式会忽略任何 CMD 或 docker run 提供的参数。
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<strong><span style="font-size: 24px">最佳实践</span></strong>
</p>
<ol class=" list-paddingleft-2" style="margin-bottom: 1.5em; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; line-height: 20.5714px; white-space: normal">
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
使用 RUN 指令安装应用和软件包,构建镜像。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
如果 Docker 镜像的用途是运行应用程序或服务,比如运行一个 MySQL,应该优先使用 Exec 格式的 ENTRYPOINT 指令。CMD 可为 ENTRYPOINT 提供额外的默认参数,同时可利用 docker run 命令行替换默认参数。
</p>
</li>
<li>
<p style="margin-top: 0; margin-bottom: 15px; line-height: 1.6; text-align: left">
如果想为容器设置默认的启动命令,可使用 CMD 指令。用户可在 docker run 命令行中替换此默认命令。
</p>
</li>
</ol>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px">到这里,我们已经具备编写 Dockerfile 的能力了。如果大家还觉得没把握,推荐一个快速掌握 Dockerfile 的方法:<strong>去 Docker Hub 上参考那些官方镜像的 Dockerfile</strong>。<br></span><span style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px"><br>好了,我们已经学习完如何创建自己的 image,下一节讨论如何分发 image。</span>
</p>
<p style="margin-top: 0; margin-bottom: 1.5em; line-height: 1.6; color: rgba(56, 58, 66, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; white-space: normal; text-align: left">
<img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170518-1495115789663062240.jpg"><br>
</p><br><br>
来源:https://www.cnblogs.com/CloudMan6/p/6875834.html
頁:
[1]