Docker 与 K8S学习笔记(五)—— 容器的操作(下篇)

上一篇我们学习了容器的启动和常用的进入容器的方式,今天我们来看看如何控制容器起停以及容器删除操作。

 一、stop、kill、start和restart

stop、kill命令都可以停止运行的容器,二者不同之处在于,stop是先给容器中的进程发送SIGTERM信号,一段时间后在发送SIGKILL信号,这样可以让进程在退出前做一些善后工作,这样显得更加优雅,而kill命令则是直接杀掉容器中的进程。

$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS         PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Up 6 minutes             quizzical_hermann
:~$ sudo docker stop 82b049e20195
82b049e20195
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS                     PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Exited (0) 2 seconds ago             quizzical_hermann

大家可以看到当执行stop命令时,容器状态变为Exited。start命令就是让处于Exited状态的进程重新启动,它能够保留容器第一次启动时执行的参数:

$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS                     PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Exited (0) 2 seconds ago             quizzical_hermann
$ sudo docker start 82b049e20195
82b049e20195
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS         PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Up 2 seconds             quizzical_hermann

restart命令是重启容器,本质上就是限制性stop在执行start命令,我们除了手动重启之外,在启动容器时可以通过设置--restart参数,确保我们容器当出现错误而停止时能够自动重启容器。

restart参数配置说明如下:

  • no:默认策略,在容器退出时不重启容器

  • on-failure:在容器非正常退出时(退出状态非0),才会重启容器

  • on-failure:3:在容器非正常退出时重启容器,最多重启3次

  • always:在容器退出时总是重启容器

  • unless-stopped:在容器退出时总是重启容器,但是不考虑在Docker守护进程启动时就已经停止了的容器

$ sudo docker run -d --restart=always  ubuntu:18.04 pwd
fb149d6e2bc1f07dece3410bfc62f5bcaf08f69bfef0bb65eaa205c2c9408bd3
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED         STATUS                        PORTS     NAMES
fb149d6e2bc1   ubuntu:18.04   "pwd"      8 seconds ago   Restarting (0) 1 second ago             heuristic_robinson
82b049e20195   ubuntu:18.04   "top -b"   4 days ago      Up 4 minutes                            quizzical_hermann
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                                  PORTS     NAMES
fb149d6e2bc1   ubuntu:18.04   "pwd"      11 seconds ago   Restarting (0) Less than a second ago             heuristic_robinson
82b049e20195   ubuntu:18.04   "top -b"   4 days ago       Up 4 minutes                                      quizzical_hermann
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                         PORTS     NAMES
fb149d6e2bc1   ubuntu:18.04   "pwd"      12 seconds ago   Restarting (0) 2 seconds ago             heuristic_robinson
82b049e20195   ubuntu:18.04   "top -b"   4 days ago       Up 4 minutes                             quizzical_hermann
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                         PORTS     NAMES
fb149d6e2bc1   ubuntu:18.04   "pwd"      14 seconds ago   Restarting (0) 3 seconds ago             heuristic_robinson
82b049e20195   ubuntu:18.04   "top -b"   4 days ago       Up 4 minutes                             quizzical_hermann

大家可以看到,当设置--restart=always时,只要容器退出就会自动重启。

二、pause和unpause操作

有时候我们可能只想让容器暂停一会儿以便于做一些简单维护性操作,这时候如果直接使用stop和start操作成本就太高了,这时我们可以使用pause和unpause命令,pause命令会暂停容器,此时容器不再使用CPU,只有当执行unpause命令时,容器才恢复运行:

$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS         PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Up 5 minutes             quizzical_hermann
$ sudo docker pause 82b049e20195
82b049e20195
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS                  PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Up 6 minutes (Paused)             quizzical_hermann
$ sudo docker unpause 82b049e20195
82b049e20195
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS         PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Up 6 minutes             quizzical_hermann

三、容器删除

当我们想要删除一个或多个容器时,可以使用rm命令,此命令参数不仅可以直接传递一个或多个容器ID,也可以通过传递表达式删除指定的某一类容器。

$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                      PORTS     NAMES
96c8a885dd0d   ubuntu:18.04   "pwd"      7 seconds ago    Exited (0) 6 seconds ago              silly_bell
4f43afe0f4b9   ubuntu:18.04   "pwd"      8 seconds ago    Exited (0) 7 seconds ago              recursing_mirzakhani
c66c61850c2c   ubuntu:18.04   "pwd"      9 seconds ago    Exited (0) 8 seconds ago              naughty_kapitsa
386d785528fd   ubuntu:18.04   "pwd"      10 seconds ago   Exited (0) 9 seconds ago              elated_chandrasekhar
91aec302789d   ubuntu:18.04   "pwd"      11 seconds ago   Exited (0) 10 seconds ago             modest_cori
038ac31cf1cf   ubuntu:18.04   "pwd"      11 seconds ago   Exited (0) 10 seconds ago             recursing_northcutt
e3fbc064330e   ubuntu:18.04   "pwd"      12 seconds ago   Exited (0) 11 seconds ago             compassionate_mccarthy
46f8b71c40e8   ubuntu:18.04   "pwd"      14 seconds ago   Exited (0) 13 seconds ago             vibrant_swartz
82b049e20195   ubuntu:18.04   "top -b"   4 days ago       Up 10 minutes                         quizzical_hermann
$ sudo docker rm 96c8a885dd0d
96c8a885dd0d
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                      PORTS     NAMES
4f43afe0f4b9   ubuntu:18.04   "pwd"      29 seconds ago   Exited (0) 28 seconds ago             recursing_mirzakhani
c66c61850c2c   ubuntu:18.04   "pwd"      30 seconds ago   Exited (0) 29 seconds ago             naughty_kapitsa
386d785528fd   ubuntu:18.04   "pwd"      31 seconds ago   Exited (0) 30 seconds ago             elated_chandrasekhar
91aec302789d   ubuntu:18.04   "pwd"      32 seconds ago   Exited (0) 31 seconds ago             modest_cori
038ac31cf1cf   ubuntu:18.04   "pwd"      32 seconds ago   Exited (0) 31 seconds ago             recursing_northcutt
e3fbc064330e   ubuntu:18.04   "pwd"      33 seconds ago   Exited (0) 32 seconds ago             compassionate_mccarthy
46f8b71c40e8   ubuntu:18.04   "pwd"      35 seconds ago   Exited (0) 34 seconds ago             vibrant_swartz
82b049e20195   ubuntu:18.04   "top -b"   4 days ago       Up 11 minutes                         quizzical_hermann
$ sudo docker rm 4f43afe0f4b9 c66c61850c2c
4f43afe0f4b9
c66c61850c2c
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED          STATUS                      PORTS     NAMES
386d785528fd   ubuntu:18.04   "pwd"      53 seconds ago   Exited (0) 52 seconds ago             elated_chandrasekhar
91aec302789d   ubuntu:18.04   "pwd"      54 seconds ago   Exited (0) 53 seconds ago             modest_cori
038ac31cf1cf   ubuntu:18.04   "pwd"      54 seconds ago   Exited (0) 54 seconds ago             recursing_northcutt
e3fbc064330e   ubuntu:18.04   "pwd"      55 seconds ago   Exited (0) 54 seconds ago             compassionate_mccarthy
46f8b71c40e8   ubuntu:18.04   "pwd"      57 seconds ago   Exited (0) 56 seconds ago             vibrant_swartz
82b049e20195   ubuntu:18.04   "top -b"   4 days ago       Up 11 minutes                         quizzical_hermann
$ sudo docker rm -v $(sudo docker ps -aq -f status=exited)
386d785528fd
91aec302789d
038ac31cf1cf
e3fbc064330e
46f8b71c40e8
$ sudo docker ps -a
CONTAINER ID   IMAGE          COMMAND    CREATED      STATUS          PORTS     NAMES
82b049e20195   ubuntu:18.04   "top -b"   4 days ago   Up 13 minutes             quizzical_hermann

当然如果要删除所有退出的容器,也可以直接使用sudo docker rm $(sudo docker ps -a -q),虽然这个命令是要删除所有容器,但是运行中的容器一般是无法直接删除的,所以就可以确保只删除Exited的容器。

posted @ 2021-10-06 14:49  阿拉懒神灯  阅读(371)  评论(0编辑  收藏  举报