本想将stop、start、restart、kill、pause、unpause这几个命令一起打包学习,但使用stop的过程中发现了一些可深入探讨的课题,因此这次只说docker stop。
命令:
docker container stop
描述:
停止一个或多个运行中的容器。容器内的主进程将会收到 SIGTERM 信号,然后在一段宽限期之后,如果进程还没有退出,将会收到 SIGKILL 信号。可以通过在容器的 Dockerfile 中使用 STOPSIGNAL 指令,或者在运行容器时使用 docker run
的 --stop-signal
选项来更改首先发送的信号。
用法:
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
别名:
docker stop(docker的一些命令可以简写,docker stop就等同于docker container stop)
选项:
选项 | 描述 |
---|---|
-s, –signal | 给容器发送的信号 |
-t, –time | 杀死容器之前等待的秒数 |
示例1:docker stop
上图所示,我有一个正在运行的redis容器,名为my-redis。使用如下命令,即可停止容器的运行。
docker stop my-redis
下图所示,执行上述命令后,docker ps已经没有运行中的容器了,docker ps -a查看所有容器,看到my-redis容器处于Exited状态。
示例2:–signal
这次的例子使用一个wordpress的容器测试选项–signal。
[root@iZ2zeajxywdhwhnlvuhp3iZ ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1f7ca360b5a wordpress "docker-entrypoint.s…" 8 hours ago Up 8 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp data-wordpress-1
在上面的描述一节,我们知道stop某个容器,docker默认会发送SIGTERM信号,那么我们改成发送SIGQUIT信号试试。
为了获得监控信号的接收情况,我们需要使用命令
docker events
不过这个命令会阻塞住终端的运行,想停止容器,需要再创建一个终端。为了叙述方便,使用docker events的终端是A,使用docker stop的终端为B。在终端B,输入命令
docker stop --signal SIGQUIT my-redis
这时终端B的容器退出,终端A显示信息如下:
2024-04-29T05:45:14.747386522+08:00 container kill b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1, signal=3)
2024-04-29T05:45:24.768588735+08:00 container kill b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1, signal=9)
2024-04-29T05:45:24.832128654+08:00 network disconnect d89db7879999ffcd4d3d9a522a60dbc8d3189e1890dd9cb8361dab888187b000 (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, name=data_default, type=bridge)
2024-04-29T05:45:24.839610684+08:00 volume unmount data_wordpress (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, driver=local)
2024-04-29T05:45:24.839752187+08:00 container stop b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1)
2024-04-29T05:45:24.845060663+08:00 container die b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, execDuration=27387, exitCode=137, image=wordpress, name=data-wordpress-1)
由上面的日志可以看到,2024-04-29T05:45:14时,signal=3,也就是我们改的信号SIGQUIT。在2024-04-29T05:45:24时,也就是10秒以后,signal=9,也就是SIGKILL信号。10秒是docker内部默认的等待时间,如果想更改,就用到下面要查看的选项–time了。
示例3:–time
终端B重新启动data-wordpress-1这个容器,这次加上–time选项(-t)。
docker stop -s SIGQUIT -t 5 data-wordpress-1
终端A结果如下:
2024-04-29T06:11:14.449516697+08:00 container kill b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1, signal=3)
2024-04-29T06:11:19.462382500+08:00 container kill b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1, signal=9)
2024-04-29T06:11:19.526282223+08:00 network disconnect d89db7879999ffcd4d3d9a522a60dbc8d3189e1890dd9cb8361dab888187b000 (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, name=data_default, type=bridge)
2024-04-29T06:11:19.533842144+08:00 volume unmount data_wordpress (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, driver=local)
2024-04-29T06:11:19.533899148+08:00 container stop b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1)
2024-04-29T06:11:19.538894894+08:00 container die b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, execDuration=91, exitCode=137, image=wordpress, name=data-wordpress-1)
由上面的日志可以看到,2024-04-29T06:11:14时,signal=3。在2024-04-29T06:11:19时,也就是5秒以后,signal=9。这说明我们的设置成功了。
备注1:信号序号
关于信号,linux的终端上,输入kill -l即可显示所有信号和它的序号:
[root@iZ2zeajxywdhwhnlvuhp3iZ ~]# kill -l
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM
16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP
21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ
26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR
31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3
38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8
43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7
58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2
63) SIGRTMAX-1 64) SIGRTMAX
备注2:疑惑
在上述测试时,我没有指定信号为SIGQUIT,只是指定了时间。如下所示:
docker stop --time 5 data-wordpress-1
奇怪的是,本想设定5秒停止容器,但容器立刻停止了(不到1秒)。这个时间改为60000也是立刻停止。这是为什么呢?
继续使用docker events查看,
2024-04-29T06:36:24.377835674+08:00 container kill b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1, signal=28)
2024-04-29T06:36:25.439919847+08:00 network disconnect d89db7879999ffcd4d3d9a522a60dbc8d3189e1890dd9cb8361dab888187b000 (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, name=data_default, type=bridge)
2024-04-29T06:36:25.447593528+08:00 volume unmount data_wordpress (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, driver=local)
2024-04-29T06:36:25.447748217+08:00 container stop b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1)
2024-04-29T06:36:25.453749391+08:00 container die b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, execDuration=46, exitCode=0, image=wordpress, name=data-wordpress-1)
2024-04-29T06:46:11.860864095+08:00 network connect d89db7879999ffcd4d3d9a522a60dbc8d3189e1890dd9cb8361dab888187b000 (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, name=data_default, type=bridge)
2024-04-29T06:46:11.863281740+08:00 volume mount data_wordpress (container=b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5, destination=/var/www/html, driver=local, propagation=, read/write=true)
2024-04-29T06:46:11.964655828+08:00 container start b1f7ca360b5a2f8a662c2ba701d5faec52cb3650f680b4533d5620072b6f0ac5 (com.docker.compose.config-hash=90a10fd46e90b54c2d51c64c572177eb63d9e2d7eb0aa74fd296de399637b7e5, com.docker.compose.container-number=1, com.docker.compose.depends_on=, com.docker.compose.image=sha256:d653f5f8a60226be8830c1467fd39e67fdcc4882aeb224ff30fffdb225bf8fb0, com.docker.compose.oneoff=False, com.docker.compose.project=data, com.docker.compose.project.config_files=/data/docker-compose.yaml, com.docker.compose.project.working_dir=/data, com.docker.compose.service=wordpress, com.docker.compose.version=2.25.0, image=wordpress, name=data-wordpress-1)
这次,wordpress容器从2024-04-29T06:36:24到2024-04-29T06:36:25,不到1秒的时间停止了,而停止信号居然不是SIGTERM(15),而是SIGWINCH(28)。
我想到使用inspect查看这个容器,看看它没有没默认配置什么的。命令如下:
docker inspect data-wordpress-1
结果进行了简化,可以看出,在Config下,有一个StopSignal,这个停止信号指定了SIGWINCH:
"Config": {
...
"StopSignal": "SIGWINCH"
}
也就是说,停止信号是在容器镜像编排的时候设定好的。这就解决了停止信号的疑惑。
那么再来看看时间–time为啥没起作用。上述官方描述,一段宽限期之后,如果进程还没有退出,将会收到SIGKILL信号。从上述例子中可以看出,这段宽限期默认为10秒。也即是说,–time可以改变的就是这个宽限期。
那么,如果容器本身正确处理了停止信号SIGTERM或是由镜像编排时,指定的StopSignal,那么只要完成停止,就不会进入宽限期指定的时间了。
1 thought on “【Docker学习】docker stop深入研究”