docker stop命令用来停止一个或多个正在运行的容器。帮助文档如下:
[root@localhost ~]# docker stop --help Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...] Stop one or more running containers Options: -t, --time int Seconds to wait for stop before killing it (default 10)
其中:
-t, --time int 停止前等待停止的秒数(默认值为10)
实例:根据容器ID停止正在运行的容器。如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 035d9d7e276e centos "/bin/bash" 8 minutes ago Up 3 minutes kind_bohr [root@localhost ~]# docker stop 035d9d7e276e 035d9d7e276e
我们也可以同时停止多个容器,如下:
[root@localhost ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ddfd18831621 centos "/bin/bash" 4 seconds ago Up 2 seconds gifted_lovelace c100b8e45fe8 ubuntu "/bin/bash" 21 seconds ago Up 20 seconds goofy_jepsen [root@localhost ~]# docker stop ddfd18831621 c100b8e45fe8 ddfd18831621 c100b8e45fe8
如果我们去停止已经停止的容器会发生什么呢?
[root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES ddfd18831621 centos "/bin/bash" About a minute ago Exited (137) 54 seconds ago gifted_lovelace [root@localhost ~]# docker stop ddfd18831621 ddfd18831621
上面实例中,我们去停止已经停止的容器,直接返回,并没有抛出错误。