Docker——我们的硬盘空间去哪了?

存储 存储设备
对于新手,我曾经说过,在玩docker的时候,尽量不要在自己电脑上(Mac或者windows)上直接安装docker和使用他,而是找一个虚拟机。

对于新手,我曾经说过,在玩docker的时候,尽量不要在自己电脑上(Mac或者windows)上直接安装docker和使用他,而是找一个虚拟机。Docker本身如果我们用的时间长了,会占用系统不少硬盘空间,特别是学习期间,今天拉一个image下来,明天又拉一个,今天建一个容器,明天建一个,久而久之,我们的电脑硬盘就吃紧了。如果是在虚拟机(vmware或者virtualbox),我们可以直接删除虚机,但是如果我们不想删除虚机,那如何清理docker所占的硬盘空间呢,本文我们一起来看看吧。

[[229894]]

首先,我们先准备一台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.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.

系统现在用了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 ~]$ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

我们拉一个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 ~]$ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

创建一个容器,退出,我们看到其实这个容器本身并不占空间

[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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

下面我们用Dockerfile基于alpine制作一个image,往这个image写入一个1G大小的文件,然后build完,我们看到系统空间多了1G,这个1G后面括号显示100%, 这个100%意思是,这个空间可以100%回收,怎么回收?把这个docker image删了就回收了。

[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 ~]$ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.

我们再修改下dockerfile,改成写2个1G的文件,然后重新build,我们看到系统的空间变成了之前的两倍,并没有变成3G,也就是它用了之前的image作为cache

[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 ~]$  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.

但是有时候我们并不会这么幸运,假如我们的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 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

RUN echo test 的位置导致docker build image时候不会去使用之前的cache,那么灾难就来了。我们的image变成了3个,我们的系统空间占用变成了4G,之前的test1变成了一个, 这个僵尸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 ~]$ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

这时候,其实我们手动通过docker image rm可以删除这两个容器,从而释放空间。但是有点麻烦对吧。这时候我们可以试试一个命令

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 ~]$ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

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 ~]$ 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.

test1这个好的image也被删了,因为没有容器使用它,所以 -a 参数要小心使用。

好的,希望本文能帮助到大家。

 

责任编辑:武晓燕 来源: 死磕网络攻城狮
相关推荐

2021-08-30 15:44:13

数据中心云端工作负载

2013-01-30 13:40:42

Windows 7系统硬盘

2011-09-19 16:03:01

双系统vista

2009-08-22 21:09:02

改变预分配硬盘空间

2015-08-10 10:07:59

Windows 10硬盘清理

2009-09-08 08:20:00

Windows 7节省硬盘空间

2009-09-07 09:10:24

Windows 7占用空间

2021-09-27 14:33:01

Windows 11Windows微软

2021-09-20 11:41:56

Windows 11硬盘空间占用微软

2010-03-24 11:48:19

tubro Linux

2009-08-18 09:19:12

Windows 7占用空间Windows 7体积

2021-01-06 10:50:27

程序员35岁互联网

2021-09-13 05:18:36

硬盘应用WizTree

2019-01-10 08:47:11

Windows 10硬盘磁盘

2013-04-15 15:07:43

清理日志Linux系统

2015-07-08 10:20:51

2021-10-27 23:32:06

Windows 11Windows微软

2018-01-11 15:36:23

命令磁盘空间Docker

2021-12-10 10:21:35

云技术云计算混合云

2021-04-09 09:55:55

DockerGoLinux
点赞
收藏

51CTO技术栈公众号