今天在重启重启的时候,执行
docker restart xxx
然后连接的时候发现连接失败,这个时候查看一下容器
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
一个容器都没有,很蛋疼,不是启动了么,怎么看不到,这个时候想起还有一个命令,显示所有的容器
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2e7d7ccc6171 mysql:latest "docker-entrypoint.s…" About an hour ago Exited (1) About an hour ago confident_grothendieck
3a3b46da138a php:7.3.10-fpm "docker-php-entrypoi…" 4 weeks ago Exited (255) 4 weeks ago 9000/tcp confident_minsky
1477a97400c1 php:7.3.10-fpm "docker-php-entrypoi…" 4 weeks ago Exited (255) 4 weeks ago 9000/tcp determined_carson
6e87be8bace1 php "docker-php-entrypoi…" 4 weeks ago Exited (0) 4 weeks ago agitated_lamarr
cd5dcbe38d83 php "docker-php-entrypoi…" 4 weeks ago Exited (255) 4 weeks ago vigilant_payne
1de17cea2667 nginx "nginx -g 'daemon of…" 4 weeks ago Exited (255) 4 weeks ago 0.0.0.0:8082->80/tcp jovial_visvesvaraya
额。一堆容器,很多都没有使用,那么就把他们删除掉吧,执行命令
docker rm 2e7d7ccc6171
2e7d7ccc6171
再看看列表
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3a3b46da138a php:7.3.10-fpm "docker-php-entrypoi…" 4 weeks ago Exited (255) 4 weeks ago 9000/tcp confident_minsky
1477a97400c1 php:7.3.10-fpm "docker-php-entrypoi…" 4 weeks ago Exited (255) 4 weeks ago 9000/tcp determined_carson
6e87be8bace1 php "docker-php-entrypoi…" 4 weeks ago Exited (0) 4 weeks ago agitated_lamarr
cd5dcbe38d83 php "docker-php-entrypoi…" 4 weeks ago Exited (255) 4 weeks ago vigilant_payne
1de17cea2667 nginx "nginx -g 'daemon of…" 4 weeks ago Exited (255) 4 weeks ago 0.0.0.0:8082->80/tcp jovial_visvesvaraya
删除掉了第一个了,但是这有这么多的容器,要怎么删除,我们试试删除php的容器
docker rm --force `docker ps -a | awk '/php/ { print $1 }'`
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1de17cea2667 nginx "nginx -g 'daemon of…" 4 weeks ago Exited (255) 4 weeks ago 0.0.0.0:8082->80/tcp jovial_visvesvaraya
成功,我们来试试删除所有的容器
docker rm --force `docker ps -a | awk '{print $1}'`
1de17cea2667
docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
成功删除
总结: docker删除容器主要使用: docker rm
命令,有些不能删除的,可以使用: docker rm --force
强制删除
然后对于指定条件的容器,可以在删除命令之后加上查找的条件, 例如上面的 docker ps -a | awk '{print $1}'
awk是查找命令并返回第一列的值,正好是CONTAINER ID,接上删除命令就是删除对应的容器id了。