Docker - cleanup

查看磁盘使用情况

sudo du -sh /var/lib/docker
  • -s --summarize
  • -h --human-readable
df -h

查看docker磁盘使用情况

dev@iZ2ze8mda8vb5igpwhsvggZ:~$ docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              4                   4                   1.969GB             69.21MB (3%)
Containers          4                   4                   52.08MB             0B (0%)
Local Volumes       1                   1                   0B                  0B
Build Cache         0                   0                   0B                  0B
dev@iZ2ze8mda8vb5igpwhsvggZ:~$ docker system df -v
Images space usage:

REPOSITORY                                         TAG                 IMAGE ID            CREATED             SIZE                SHARED SIZE         UNIQUE SIZE         CONTAINERS
1nj0zren.mirror.aliyuncs.com/bitnami/phabricator   2020.29.0           fc21dec6c2be        46 hours ago        1.044GB             0B                  1.044GB             0
hub-inner.xingfuture.com/nacos/nacos-server        latest              707084b162a6        9 days ago          909.7MB             0B                  909.7MB             1
hub-inner.xingfuture.com/library/zookeeper         3.6.1               2bb455f2234b        10 days ago         252.3MB             69.21MB             183.1MB             1
hub-inner.xingfuture.com/library/postgres          12.3                4b52913b0a3a        10 days ago         313MB               69.21MB             243.8MB             1
hub-inner.xingfuture.com/library/mysql             5.7.31              8679ced16d20        10 days ago         448.5MB             69.21MB             379.3MB             1
nginx                                              1.19.1              8cf1bfb43ff5        11 days ago         132.5MB             69.21MB             63.27MB             2
hub-inner.xingfuture.com/concourse/concourse       6.4.0               a1e690d81652        2 weeks ago         967.5MB             0B                  967.5MB             1
sonatype/nexus3                                    latest              0aedb49b54b0        2 weeks ago         625.6MB             0B                  625.6MB             3
hub-inner.xingfuture.com/wurstmeister/kafka        2.12-2.5.0          f1905dce9659        2 months ago        431.3MB             0B                  431.3MB             1

Containers space usage:

CONTAINER ID        IMAGE                                                    COMMAND                  LOCAL VOLUMES       SIZE                CREATED             STATUS                      NAMES
114841091f29        hub-inner.xingfuture.com/concourse/concourse:6.4.0       "dumb-init /usr/loca…"   0                   52.4MB              3 minutes ago       Up 3 minutes                concourse_concourse_1
af125b8f933a        hub-inner.xingfuture.com/library/postgres:12.3           "docker-entrypoint.s…"   1                   48B                 3 minutes ago       Up 3 minutes                concourse_concourse-db_1
baa56e0e8b24        nginx:1.19.1                                             "/docker-entrypoint.…"   0                   0B                  9 hours ago         Up 9 hours                  fookey_nginx.1.cp1v7mrq8kzvn6xioi6xi88p0
21a520d8976c        sonatype/nexus3:latest                                   "sh -c ${SONATYPE_DI…"   0                   32.9kB              12 hours ago        Up 12 hours                 fookey_nexus.1.vn4a74nu6zxlqnabruwoe5fan
75e8c564e4ea        sonatype/nexus3:latest                                   "sh -c ${SONATYPE_DI…"   0                   93B                 12 hours ago        Exited (0) 12 hours ago     fookey_nexus.1.981grj8l5byd1dnry6m7cznyd
6abb9996d7f2        sonatype/nexus3:latest                                   "sh -c ${SONATYPE_DI…"   0                   93B                 12 hours ago        Exited (255) 12 hours ago   fookey_nexus.1.dm1vyvggbrjvwk2ssx582b7eu
d585a6d52fc0        hub-inner.xingfuture.com/nacos/nacos-server:latest       "bin/docker-startup.…"   0                   70.9kB              12 hours ago        Up 12 hours                 nacos
6cf0cfe2192f        hub-inner.xingfuture.com/library/mysql:5.7.31            "docker-entrypoint.s…"   0                   2B                  22 hours ago        Up 22 hours                 slave_mysql-slave_1
3450baab3d29        nginx:1.19.1                                             "/docker-entrypoint.…"   0                   1.83kB              47 hours ago        Exited (0) 24 hours ago     nginx_nginx_1
1cd9fba884b7        hub-inner.xingfuture.com/wurstmeister/kafka:2.12-2.5.0   "start-kafka.sh"         0                   4.65MB              5 days ago          Up 24 hours                 kafka1
636a3f5cff04        hub-inner.xingfuture.com/library/zookeeper:3.6.1         "/docker-entrypoint.…"   1                   38.4kB              5 days ago          Up 24 hours                 zoo1

Local Volumes space usage:

VOLUME NAME                                                        LINKS               SIZE
d6bb26afccf7c8b919811db910512275d03b74d728214ad42bbdeed4c0cee992   1                   0B
0955c2d46e4830c2992fd1b4f103191c739b7acd13f902f45a7cbd248565a795   0                   0B
44e9bbabf73a966c9fddd4225c5905a3d969344bb365a84a954b8e04941be96e   0                   0B
4f9ca0de315057f10ab42164e0e39adaab2288126e0c400471b33b09873bfcab   0                   0B
8d4bc3bb0403e6c8ebb180948130c0b5105407c6cffbb16a643fe82ab7d9d248   0                   0B
dfb84d2ad9c07337d0c5adc0f42b81ab8d8f6fe7f6e9068e32f791330289415b   0                   0B
e338970e92e72b459193ffbb44802aecb6ed5f7460fd00d64ffba25c793e1a83   0                   0B
ffcd39a66f3d4a5d20a6deb81a3689f7bbe0b0707936e53bb0fc4514e3c5c302   1                   0B

Build cache usage: 0B

CACHE ID            CACHE TYPE          SIZE                CREATED             LAST USED           USAGE               SHARED

清理磁盘,删除关闭的容器、无用的数据卷和网络,以及 dangling 镜像 ( 即无 tag 的镜像 )

dev@iZ2ze8mda8vb5igpwhsvggZ:~$ docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache

Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

docker system prune -a 彻底清理磁盘,,可以将没有容器使用 Docker 镜像都删掉。注意,这两个命令会把你暂时关闭的容器,以及暂时没有用到的 Docker 镜像都删掉了.....所以使用之前一定要想清楚。我没用过,因为会清理没有开启的 Docker 镜像。


清理容器

docker container prune

等价于

docker rm $(docker ps -aq)

清理所有悬挂( )镜像

docker image prune
# or
docker rmi $(docker images -qf "dangling=true")

清理所有无用数据卷

docker volume prune

由于 prune 操作是批量删除类的危险操作,所以会有一次确认。 如果不想输入 y 来确认,可以添加 -f 操作。慎用!

清理停止的容器

docker rm -lv CONTAINER
  • -l 是清理 link,
  • -v 是清理 volume。

这里的 CONTAINER 是容器的 name 或 ID,可以是一个或多个。

参数列表

Name, shorthandDefaultDescription
–force, -ffalseForce the removal of a running container (uses SIGKILL)
–link, -lfalseRemove the specified link
–volumes, -vfalseRemove the volumes associated with the container

https://note.qidong.name/2017/06/26/docker-clean/