在我的“系统学习Docker,践行DevOps理念”课程里,对于新手,我曾经说过,在玩docker的时候,尽量不要在自己电脑上(Mac或者windows)上直接安装docker和使用他,而是找一个虚拟机。Docker本身如果我们用的时间长了,会占用系统不少硬盘空间,特别是学习期间,今天拉一个image下来,明天又拉一个,今天建一个容器,明天建一个,久而久之,我们的电脑硬盘就吃紧了。如果是在虚拟机(vmware或者virtualbox),我们可以直接删除虚机,但是如果我们不想删除虚机,那如何清理docker所占的硬盘空间呢,本文我们一起来看看吧。
首先,我们先准备一台docker host,比如下面,我们通过df命令先看看系统当前的硬盘空间。
[vagrant@localhost ~]$ docker version
Client:
Version: 18.05.0-ce
API version: 1.37
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:14:54 2018
OS/Arch: linux/amd64
Experimental: false
Orchestrator: swarm
Server:
Engine:
Version: 18.05.0-ce
API version: 1.37 (minimum version 1.12)
Go version: go1.9.5
Git commit: f150324
Built: Wed May 9 22:18:36 2018
OS/Arch: linux/amd64
Experimental: false
[vagrant@localhost ~]$ df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/VolGroup00-LogVol00 38G 1.3G 37G 4% /
devtmpfs 236M 0 236M 0% /dev
tmpfs 245M 0 245M 0% /dev/shm
tmpfs 245M 4.5M 240M 2% /run
tmpfs 245M 0 245M 0% /sys/fs/cgroup
/dev/sda2 1014M 63M 952M 7% /boot
tmpfs 49M 0 49M 0% /run/user/1000
tmpfs 49M 0 49M 0% /run/user/0
[vagrant@localhost ~]$
系统现在用了1.3G空间,还有37G可用空间。
首先我们先介绍一个命令,叫 docker system df
,我们看到目前我们没有任何镜像,容器,存储和cache。
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 0 0 0B 0B
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
我们拉一个image看看
[vagrant@localhost ~]$ docker pull alpine
Using default tag: latest
latest: Pulling from library/alpine
ff3a5c916c92: Pull complete
Digest: sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0
Status: Downloaded newer image for alpine:latest
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 0 4.148MB 4.148MB (100%)
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
创建一个容器,退出,我们看到其实这个容器本身并不占空间
[vagrant@localhost ~]$ docker run -d alpine
8b7f9b1e11b85c6d2335b17ea2c303cf500f2a19cccd57864fb1eeceb4021a5d
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 1 1 4.148MB 0B (0%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
下面我们用Dockerfile基于alpine制作一个image,往这个image写入一个1G大小的文件,然后build完,我们看到系统空间多了1G,这个1G后面括号显示100%, 这个100%意思是,这个空间可以100%回收,怎么回收?把这个docker image删了就回收了。
[vagrant@localhost ~]$ more Dockerfile
FROM alpine
RUN dd if=/dev/zero of=1g3.img bs=1G count=1
[vagrant@localhost ~]$ docker build -t test1 .
Sending build context to Docker daemon 125.4kB
Step 1/2 : FROM alpine
---> 3fd9065eaf02
Step 2/2 : RUN dd if=/dev/zero of=1g3.img bs=1G count=1
---> Running in 6d9e95f54e26
1+0 records in
1+0 records out
Removing intermediate container 6d9e95f54e26
---> 4ebfe90ead11
Successfully built 4ebfe90ead11
Successfully tagged test1:latest
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 1 1.078GB 1.078GB (100%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
我们再修改下dockerfile,改成写2个1G的文件,然后重新build,我们看到系统的空间变成了之前的两倍,并没有变成3G,也就是它用了之前的image作为cache
[vagrant@localhost ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test1 latest 4ebfe90ead11 3 minutes ago 1.08GB
alpine latest 3fd9065eaf02 4 months ago 4.15MB
[vagrant@localhost ~]$ more Dockerfile
FROM alpine
RUN dd if=/dev/zero of=1g3.img bs=1G count=1
RUN dd if=/dev/zero of=1g3.img bs=1G count=1
[vagrant@localhost ~]$ docker build -t test1 .
Sending build context to Docker daemon 125.4kB
Step 1/3 : FROM alpine
---> 3fd9065eaf02
Step 2/3 : RUN dd if=/dev/zero of=1g3.img bs=1G count=1
---> Using cache
---> 4ebfe90ead11
Step 3/3 : RUN dd if=/dev/zero of=1g3.img bs=1G count=1
---> Running in 93929b2a75ce
1+0 records in
1+0 records out
Removing intermediate container 93929b2a75ce
---> 9fbb2427fc1d
Successfully built 9fbb2427fc1d
Successfully tagged test1:latest
[vagrant@localhost ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test1 latest 9fbb2427fc1d 5 seconds ago 2.15GB
alpine latest 3fd9065eaf02 4 months ago 4.15MB
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 1 2.152GB 2.152GB (100%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
但是有时候我们并不会这么幸运,假如我们的Dockerfile变成
[vagrant@localhost ~]$ more Dockerfile
FROM alpine
RUN echo test
RUN dd if=/dev/zero of=1g3.img bs=1G count=1
RUN dd if=/dev/zero of=1g3.img bs=1G count=1
RUN echo test 的位置导致docker build image时候不会去使用之前的cache,那么灾难就来了。我们的image变成了3个,我们的系统空间占用变成了4G,之前的test1变成了一个<none>, 这个僵尸image叫dangling images, 这时候怎么办呢?
[vagrant@localhost ~]$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
test1 latest 8c32028a8557 45 seconds ago 2.15GB
<none> <none> 9fbb2427fc1d 5 minutes ago 2.15GB
alpine latest 3fd9065eaf02 4 months ago 4.15MB
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 3 1 4.299GB 4.299GB (100%)
Containers 1 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
这时候,其实我们手动通过docker image rm可以删除这两个容器,从而释放空间。但是有点麻烦对吧。这时候我们可以试试一个命令
docker system prune
[vagrant@localhost ~]$ docker system prune
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Containers:
8b7f9b1e11b85c6d2335b17ea2c303cf500f2a19cccd57864fb1eeceb4021a5d
Deleted Images:
deleted: sha256:9fbb2427fc1dbaba37fd67a81f84970255e50325ea128aa06bcc60a138835ce8
deleted: sha256:63f58c3c4640f0ed9b1d917e2f01b0ca461929768712a4c8899cbf3b27f0d716
deleted: sha256:4ebfe90ead11af51f131372292709d590e4856bb6bf9855e1bd1e5b801920364
deleted: sha256:45f5bb8b1109d2e61b2aa4ab2d392582cbe89bba99d2e3a3d15192500ed5d22c
Total reclaimed space: 2.147GB
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 2 0 2.152GB 2.152GB (100%)
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
docker system prune会清理所有已停止的容器,没有被用的network,所有的僵尸image,还是build cache。能够迅速帮我们清理空间。
当然docker system prune 我们可以加一个 -a
参数,这个就厉害了,除了删除之前docker system prune能删除的东西以外,他还会删除所有没有容器使用的image,比如
[vagrant@localhost ~]$ docker system prune -a
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all images without at least one container associated to them
- all build cache
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: alpine:latest
untagged: alpine@sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0
untagged: test1:latest
deleted: sha256:8c32028a8557a1bdd9cc0bdba9b0bb6e9f3c52e139c62de166b24ac3b2abddab
deleted: sha256:cf8eec9e8e0dfdb48a628e284a8bce27b5d6968e94baacc16ca47ef9d667dc82
deleted: sha256:91e956dd9cf9f736e9b0ee7a7211e91b6858ad746d3b1cf7713e1492da575c04
deleted: sha256:e7264a1948b8edae8f94172c5034d5223e4bf045d1d7eb00b431402d52528aec
deleted: sha256:8bbc26e497f57eb0caef4a26a06333f32522ab0d8fae32637a5a85c2d477436e
deleted: sha256:3fd9065eaf02feaf94d68376da52541925650b81698c53c6824d92ff63f98353
deleted: sha256:cd7100a72410606589a54b932cabd804a17f9ae5b42a1882bd56d263e02b6215
Total reclaimed space: 2.152GB
[vagrant@localhost ~]$ docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 0 0 0B 0B
Containers 0 0 0B 0B
Local Volumes 0 0 0B 0B
Build Cache 0B 0B
[vagrant@localhost ~]$
test1这个好的image也被删了,因为没有容器使用它,所以 -a 参数要小心使用。
好的,希望本文能帮助到大家。