容器常用操作 - 每天5分钟玩转 Docker 容器技术(25)
<p class="wihudong" style="margin: 0"><span style="font-size: 16px"><span style="background-color: rgba(250, 250, 250, 1); font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-size: 14.4px; color: rgba(56, 58, 66, 1)">前面讨论了如何运行容器,本节学习容器的其他常用操作。</span></span></p><h3 style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-weight: 500; line-height: 1.2; margin-top: 1.5em; margin-bottom: 0.5em; font-size: 1.5em; white-space: normal; background-color: rgba(250, 250, 250, 1); text-align: left"><strong>stop/start/restart 容器</strong></h3>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">docker stop</span></code> 可以停止运行的容器。</p>
<p><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736028283080683.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left">容器在 docker host 中实际上是一个进程,<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)"><span style="background-color: rgba(216, 216, 216, 1)">docker stop</span></code> 命令本质上是向该进程发送一个 SIGTERM 信号。如果想快速停止容器,可使用 <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)"><span style="background-color: rgba(216, 216, 216, 1)">docker kill</span></code> 命令,其作用是向容器进程发送 SIGKILL 信号。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736028567067456.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">docker start</span></code> 重新启动。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736028763084912.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">docker start</span></code> 会保留容器的第一次启动时的所有参数。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">docker restart</span></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)"><span style="background-color: rgba(216, 216, 216, 1)">docker stop</span></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)"><span style="background-color: rgba(216, 216, 216, 1)">docker start</span></code>。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">--restart</span></code> 就可以达到这个效果。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736029081053547.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">--restart=always</span></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)"><span style="background-color: rgba(216, 216, 216, 1)">--restart=on-failure:3</span></code>,意思是如果启动进程退出代码非0,则重启容器,最多重启3次。</p>
<h3 style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-weight: 500; line-height: 1.2; margin-top: 1.5em; margin-bottom: 0.5em; font-size: 1.5em; white-space: normal; background-color: rgba(250, 250, 250, 1); text-align: left"><strong>pause/unpause 容器</strong></h3>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left">有时我们只是希望暂时让容器暂停工作一段时间,比如要对容器的文件系统打个快照,或者 dcoker host 需要使用 CPU,这时可以执行 <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)"><span style="background-color: rgba(216, 216, 216, 1)">docker pause</span></code>。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736029243067386.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left">处于暂停状态的容器不会占用 CPU 资源,直到通过 <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)"><span style="background-color: rgba(216, 216, 216, 1)">docker unpause</span></code> 恢复运行。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736029453020313.jpg"></p>
<h3 style="font-family: BlinkMacSystemFont, "Lucida Grande", "Segoe UI", Ubuntu, Cantarell, sans-serif; font-weight: 500; line-height: 1.2; margin-top: 1.5em; margin-bottom: 0.5em; font-size: 1.5em; white-space: normal; background-color: rgba(250, 250, 250, 1); text-align: left"><strong>删除容器</strong></h3>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left">使用 docker 一段时间后,host 上可能会有大量已经退出了的容器。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736029639011452.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left">这些容器依然会占用 host 的文件系统资源,如果确认不会再重启此类容器,可以通过 <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)"><span style="background-color: rgba(216, 216, 216, 1)">docker rm</span></code> 删除。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736029931046689.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">docker rm</span></code> 一次可以指定多个容器,如果希望批量删除所有已经退出的容器,可以执行如下命令:</p>
<p style="text-align: left"><input class="hidden-input" style="font: inherit inherit inherit inherit inherit / inherit inherit; margin: 0; padding: 0; border-width: 0; position: absolute; z-index: -1; top: 0; left: 0; opacity: 0; width: 2px; transform: translateZ(0); height: 19px" tabindex="-1" type="text" data-react-skip-selection-restoration="true"></p>
<p style="text-align: left"><span style="background-color: rgba(216, 216, 216, 1)">docker rm -v $(docker ps -aq -f status=exited)</span></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"><img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736030054020183.jpg"></p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); 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)"><span style="background-color: rgba(216, 216, 216, 1)">docker rm</span></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)"><span style="background-color: rgba(216, 216, 216, 1)">docker rmi</span></code> 是删除镜像。</p>
<p style="margin-top: 0; margin-bottom: 15px; 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; background-color: rgba(250, 250, 250, 1); text-align: left"> 一下学了这么多操作,很有必要做个总结。下一节我们会用一张图来描述容器的状态机。<br>
</p>
<p>
<img src="http://7xo6kd.com1.z0.glb.clouddn.com/upload-ueditor-image-20170606-1496736030315055131.jpg"><br>
</p><br><br>
来源:https://www.cnblogs.com/CloudMan6/p/6952115.html
頁:
[1]