一次删除所有 docker 个实例

Delete all docker instances at once

我是码头工人和其他概念的新手。我有很多未使用的码头工人 运行,我想删除所有这些码头工人。现在我手动杀死它。我们可以马上做吗?

  • docker rm $(docker ps -a -q)

This command deletes all stopped containers. The command docker ps -a -q above returns all existing container IDs and passes them to the docker rm command which deletes them. Running containers are not deleted.

或者,试试这个。

  • docker system prune -a

Remove all unused images not just dangling ones. also it will remove all build cache

正如@Emon 提到的,您可以选择 docker 修剪。但请记住,修剪系统将删除所有已停止的容器和所有 未使用的图像。 由于您已标记 shell,我还要添加此 shell 脚本。

echo -e "******* Start *******"
echo -e "\n=====> Stopping & Cleaning up UNUSED containers.."
for i in `docker ps -a | grep -v CONTAINER | awk '{print }'`; do docker stop $i ; docker rm $i ; done

echo -e "[CLEARED]\n"
docker ps -a

echo -e "\n=====> Removing UNUSED container images.."

for i in `docker images | grep none | awk '{print }'`; do docker rmi $i ; done
echo -e "[CLEARED]\n"
docker images

这个脚本可以根据您的需要进行相应的更改,例如如果您只想删除容器甚至图像。

这也可以通过

轻松完成

docker ps -a | grep "pattern" | awk '{print }' | xargs docker rm

如果只想单独删除退出的容器,可以使用它

`docker rm $(docker ps -a -f status=exited -q)`