docker 技巧

  1. 1. 删除 none tag 镜像
  2. 2. 删除所有镜像
  3. 3. 删除所有容器
  4. 4. 删除所有停止运行的容器
  5. 5. 删除所有 dangling (unused) 的镜像
  6. 6. xargs with –no-run-if-empty is even better as it does cleanly handle the case when there is nothing to be removed.

删除 none tag 镜像

1
docker rmi $(docker images | grep "^<none>" | awk '{print $3}')
1
docker rmi $(docker images -q --filter "dangling=true")

删除所有镜像

1
docker images -q | xargs docker rmi

删除所有容器

1
docker ps -q -a | xargs docker rm

删除所有停止运行的容器

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

删除所有 dangling (unused) 的镜像

1
docker rmi $( docker images -q -f dangling=true)

xargs with –no-run-if-empty is even better as it does cleanly handle the case when there is nothing to be removed.

1
2
3
4
# Delete all stopped containers
docker ps -q -f status=exited | xargs --no-run-if-empty docker rm
# Delete all dangling (unused) images
docker images -q -f dangling=true | xargs --no-run-if-empty docker rmi