批量清理多种类型的资源

docker system prune

  1. # docker system prune
  2. WARNING! This will remove:
  3. - all stopped containers
  4. - all networks not used by at least one container
  5. - all dangling images
  6. - all dangling build cache
  7. Are you sure you want to continue? [y/N] y
  8. Deleted Containers:
  9. 0e5590aa2b7375420a3bfb4346d7a6a298cbe74661b3f7255cf6757073fb1edb
  10. Total reclaimed space: 0B

可以清理containers,networks,image,cache

单独清理镜像

docker image prune 可以清理掉无tag的镜像

  1. # docker image prune
  2. WARNING! This will remove all dangling images.
  3. Are you sure you want to continue? [y/N]
  4. Total reclaimed space: 0B

docker image prune -a 可以清理掉无容器使用的镜像

  1. # docker image prune -a
  2. WARNING! This will remove all images without at least one container associated to them.
  3. Are you sure you want to continue? [y/N] y
  4. Deleted Images:
  5. untagged: nginx:1.14.2
  6. untagged: nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
  7. deleted: sha256:295c7be079025306c4f1d65997fcf7adb411c88f139ad1d34b537164aa060369
  8. deleted: sha256:19606512dfe192788a55d7c1efb9ec02041b4e318587632f755c5112f927e0e3
  9. deleted: sha256:0b83495b3ad3db8663870c3babeba503a35740537a5e25acdf61ce6a8bdad06f
  10. deleted: sha256:5dacd731af1b0386ead06c8b1feff9f65d9e0bdfec032d2cd0bc03690698feda
  11. untagged: busybox:latest
  12. untagged: busybox:v1
  13. untagged: busybox@sha256:0f354ec1728d9ff32edcd7d1b8bbdfc798277ad36120dc3dc683be44524c8b60
  14. deleted: sha256:69593048aa3acfee0f75f20b77acb549de2472063053f6730c4091b53f2dfb02
  15. deleted: sha256:5b8c72934dfc08c7d2bd707e93197550f06c0751023dabb3a045b723c5e7b373
  16. Total reclaimed space: 110.4MB

docker image prune -a —filter “until=24h” 可以清理掉24h之前的镜像

清理容器

docker container prune 可以清理到已经停止的容器

  1. # docker container prune
  2. WARNING! This will remove all stopped containers.
  3. Are you sure you want to continue? [y/N] y
  4. Deleted Containers:
  5. 1ae261ad41affaf06974835572bf21adc91c390fb4328d1bea82e5cd0e3d6940
  6. Total reclaimed space: 0B

默认情况下,所有停止状态的容器会被删除。可以使用 —filter 标志来限制范围。
例如docker container prune --filter "until=24h"

清理网络

docker network prune 可以清理docker 网络创建的iptables规则、桥接网络设备和路由条目