对于新手, 我曾经说过, 在玩 docker 的时候, 尽量不要在自己电脑上 (Mac 或者 windows) 上直接安装 docker 和使用他, 而是找一个虚拟机.
人工智能 + 区块链的发展趋势及应用调研报告
对于新手, 我曾经说过, 在玩 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 ~]$ 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, 改成写 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 ~]$
但是有时候我们并不会这么幸运, 假如我们的 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 变成了一个, 这个僵尸 image 叫 dangling images, 这时候怎么办呢?
- [vagrant@localhost ~]$ docker image ls
- REPOSITORY TAG IMAGE ID CREATED SIZE
test1 latest 8c32028a8557 45 seconds ago 2.15GB
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 可以删除这两个容器, 从而释放空间. 但是有点麻烦对吧. 这时候我们可以试试一个命令
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 参数要小心使用.
好的, 希望本文能帮助到大家.
来源: http://stor.51cto.com/art/201805/573832.htm