容器常用操作
stop/start/restart 容器
通过 docker stop
可以停止运行的容器。
1 [email protected]:~# docker ps 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 3 days 80/tcp myhttp 4 fa7407d1b483 ubuntu "/bin/bash -c ‘while…" 3 days ago Up 3 days jolly_kirch 5 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 6 [email protected]:~# 7 [email protected]:~# docker stop fa7407d1b483 8 fa7407d1b483 9 [email protected]:~# docker ps 10 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 3 days 80/tcp myhttp 12 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 13 [email protected]:~#
容器在 docker host 中实际上是一个进程,docker stop
命令本质上是向该进程发送一个 SIGTERM 信号。
如果想快速停止容器,可使用 docker kill
命令,其作用是向容器进程发送 SIGKILL 信号。
1 [email protected]:~# docker ps 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 3 days 80/tcp myhttp 4 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 5 [email protected]:~# 6 [email protected]:~# docker kill myhttp 7 myhttp 8 [email protected]:~# 9 [email protected]:~# docker ps 10 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 12 [email protected]:~#
对于处于停止状态的容器,可以通过 docker start
重新启动。
docker start
会保留容器的第一次启动时的所有参数。
1 [email protected]:~# docker start myhttp 2 myhttp 3 [email protected]:~# docker ps 4 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 3 seconds 80/tcp myhttp 6 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 7 [email protected]:~#
docker restart
可以重启容器,其作用就是依次执行 docker stop
和docker start
。
容器可能会因某种错误而停止运行。
对于服务类容器,通常希望在这种情况下容器能够自动重启。
启动容器时设置 --restart
就可以达到这个效果。
1 [email protected]:~# docker run -d --restart=always httpd 2 599ffc950752e832275be369ce6f8c6cffff526e8241c760b971db11b46df818 3 [email protected]:~#
--restart=always
意味着无论容器因何种原因退出(包括正常退出),就立即重启。
该参数的形式还可以是 --restart=on-failure:3
,意思是如果启动进程退出代码非0,则重启容器,最多重启3次。
pause/unpause 容器
需要让容器暂停工作一段时间,比如要对容器的文件系统打个快照,或者 dcoker host 需要使用 CPU,这时可以执行 docker pause
。
1 [email protected]:~# docker ps 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 5 minutes 80/tcp myhttp 4 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 5 [email protected]:~# docker pause myhttp 6 myhttp 7 [email protected]:~# docker ps 8 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 5 minutes (Paused) 80/tcp myhttp 10 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 11 [email protected]:~#
处于暂停状态的容器不会占用 CPU 资源,直到通过 docker unpause
恢复运行。
1 [email protected]:~# docker unpause myhttp 2 myhttp 3 [email protected]:~# docker ps 4 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 6 minutes 80/tcp myhttp 6 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 7 [email protected]:~#
删除容器
使用 docker 一段时间后,host 上可能会有大量已经退出了的容器。
1 [email protected]:~# docker ps -a 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 599ffc950752 httpd "httpd-foreground" 5 minutes ago Exited (137) 3 minutes ago epic_bohr 4 07de97f97680 busybox "sh" 8 hours ago Exited (0) 8 hours ago lucid_franklin 5 c44dc8090486 ubuntu "/bin/bash -c ‘while…" 2 days ago Exited (137) 2 days ago blissful_joliot 6 d7534aebaf54 ubuntu "/bin/bash -c ‘while…" 2 days ago Exited (137) 2 days ago nifty_wright 7 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 7 minutes 80/tcp myhttp 8 d32b4710507d ubuntu "/bin/bash -c ‘while…" 3 days ago Exited (137) 3 days ago trusting_panini 9 fa7407d1b483 ubuntu "/bin/bash -c ‘while…" 3 days ago Exited (137) 9 minutes ago jolly_kirch 10 8b312e89208b ubuntu "pwd" 3 days ago Exited (0) 3 days ago frosty_williams 11 1b4e997bcfd3 ubuntu "pwd" 3 days ago Exited (0) 3 days ago sad_brahmagupta 12 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 13 [email protected]:~#
这些容器依然会占用 host 的文件系统资源,如果确认不会再重启此类容器,可以通过 docker rm
删除。
1 [email protected]:~# docker rm 599ffc950752 2 599ffc950752 3 [email protected]:~#
docker rm
一次可以指定多个容器,如果希望批量删除所有已经退出的容器,可以执行如下命令:
docker rm -v $(docker ps -aq -f status=exited)
1 [email protected]:~# docker rm -v $(docker ps -aq -f status=exited) 2 07de97f97680 3 c44dc8090486 4 d7534aebaf54 5 d32b4710507d 6 fa7407d1b483 7 8b312e89208b 8 1b4e997bcfd3 9 [email protected]:~# docker ps -a 10 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 11 8b62217b8d1d httpd "httpd-foreground" 3 days ago Up 9 minutes 80/tcp myhttp 12 f27eb6348026 registry:2 "/entrypoint.sh /etc…" 4 days ago Up 4 days 0.0.0.0:5000->5000/tcp vigilant_swartz 13 [email protected]:~#
docker rm
是删除容器docker rmi
是删除镜像
--------------------引用来自-------------------
https://mp.weixin.qq.com/s?__biz=MzIwMTM5MjUwMg==&mid=2653587651&idx=1&sn=787c294f16f32a7343e202254c8241c0&chksm=8d3080daba4709cc8e90a33bc93bdf6abdc8e9d6c122cc65256d5bc7d945eba1814d1a5db573&scene=21#wechat_redirect
原文地址:https://www.cnblogs.com/gsophy/p/10287777.html