自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Docker——我們的硬盤空間去哪了?

存儲 存儲設(shè)備
對于新手,我曾經(jīng)說過,在玩docker的時候,盡量不要在自己電腦上(Mac或者windows)上直接安裝docker和使用他,而是找一個虛擬機。

對于新手,我曾經(jīng)說過,在玩docker的時候,盡量不要在自己電腦上(Mac或者windows)上直接安裝docker和使用他,而是找一個虛擬機。Docker本身如果我們用的時間長了,會占用系統(tǒng)不少硬盤空間,特別是學(xué)習(xí)期間,今天拉一個image下來,明天又拉一個,今天建一個容器,明天建一個,久而久之,我們的電腦硬盤就吃緊了。如果是在虛擬機(vmware或者virtualbox),我們可以直接刪除虛機,但是如果我們不想刪除虛機,那如何清理docker所占的硬盤空間呢,本文我們一起來看看吧。

[[229894]]

首先,我們先準(zhǔn)備一臺docker host,比如下面,我們通過df命令先看看系統(tǒng)當(dāng)前的硬盤空間。

  1. [vagrant@localhost ~]$ docker version 
  2. Client: 
  3. Version:      18.05.0-ce 
  4. API version:  1.37 
  5. Go version:   go1.9.5 
  6. Git commit:   f150324 
  7. Built:        Wed May  9 22:14:54 2018 
  8. OS/Arch:      linux/amd64 
  9. Experimental: false 
  10. Orchestrator: swarm 
  11.  
  12. Server: 
  13. Engine: 
  14.  Version:      18.05.0-ce 
  15.  API version:  1.37 (minimum version 1.12) 
  16.  Go version:   go1.9.5 
  17.  Git commit:   f150324 
  18.  Built:        Wed May  9 22:18:36 2018 
  19.  OS/Arch:      linux/amd64 
  20.  Experimental: false 
  21. [vagrant@localhost ~]$ df -h 
  22. Filesystem                       Size  Used Avail Use% Mounted on 
  23. /dev/mapper/VolGroup00-LogVol00   38G  1.3G   37G   4% / 
  24. devtmpfs                         236M     0  236M   0% /dev 
  25. tmpfs                            245M     0  245M   0% /dev/shm 
  26. tmpfs                            245M  4.5M  240M   2% /run 
  27. tmpfs                            245M     0  245M   0% /sys/fs/cgroup 
  28. /dev/sda2                       1014M   63M  952M   7% /boot 
  29. tmpfs                             49M     0   49M   0% /run/user/1000 
  30. tmpfs                             49M     0   49M   0% /run/user/0 
  31. [vagrant@localhost ~]$ 

系統(tǒng)現(xiàn)在用了1.3G空間,還有37G可用空間。

首先我們先介紹一個命令,叫 docker system df ,我們看到目前我們沒有任何鏡像,容器,存儲和cache。

  1. [vagrant@localhost ~]$ docker system df 
  2. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  3. Images              0                   0                   0B                  0B 
  4. Containers          0                   0                   0B                  0B 
  5. Local Volumes       0                   0                   0B                  0B 
  6. Build Cache                                                 0B                  0B 
  7. [vagrant@localhost ~]$ 

我們拉一個image看看

  1. [vagrant@localhost ~]$ docker pull alpine 
  2. Using default tag: latest 
  3. latest: Pulling from library/alpine 
  4. ff3a5c916c92: Pull complete 
  5. Digest: sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0 
  6. Status: Downloaded newer image for alpine:latest 
  7. [vagrant@localhost ~]$ docker system df 
  8. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  9. Images              1                   0                   4.148MB             4.148MB (100%) 
  10. Containers          0                   0                   0B                  0B 
  11. Local Volumes       0                   0                   0B                  0B 
  12. Build Cache                                                 0B                  0B 
  13. [vagrant@localhost ~]$ 

創(chuàng)建一個容器,退出,我們看到其實這個容器本身并不占空間

  1. [vagrant@localhost ~]$ docker run -d alpine 
  2. 8b7f9b1e11b85c6d2335b17ea2c303cf500f2a19cccd57864fb1eeceb4021a5d 
  3. [vagrant@localhost ~]$ docker system df 
  4. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  5. Images              1                   1                   4.148MB             0B (0%) 
  6. Containers          1                   0                   0B                  0B 
  7. Local Volumes       0                   0                   0B                  0B 
  8. Build Cache                                                 0B                  0B 

下面我們用Dockerfile基于alpine制作一個image,往這個image寫入一個1G大小的文件,然后build完,我們看到系統(tǒng)空間多了1G,這個1G后面括號顯示100%, 這個100%意思是,這個空間可以100%回收,怎么回收?把這個docker image刪了就回收了。

  1. [vagrant@localhost ~]$ docker image ls 
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE 
  3. test1               latest              4ebfe90ead11        3 minutes ago       1.08GB 
  4. alpine              latest              3fd9065eaf02        4 months ago        4.15MB 
  5. [vagrant@localhost ~]$ more Dockerfile 
  6. FROM alpine 
  7. RUN dd if=/dev/zero of=1g3.img bs=1G count=1 
  8. RUN dd if=/dev/zero of=1g3.img bs=1G count=1 
  9. [vagrant@localhost ~]$ docker build -t test1 . 
  10. Sending build context to Docker daemon  125.4kB 
  11. Step 1/3 : FROM alpine 
  12. ---> 3fd9065eaf02 
  13. Step 2/3 : RUN dd if=/dev/zero of=1g3.img bs=1G count=1 
  14. ---> Using cache 
  15. ---> 4ebfe90ead11 
  16. Step 3/3 : RUN dd if=/dev/zero of=1g3.img bs=1G count=1 
  17. ---> Running in 93929b2a75ce 
  18. 1+0 records in 
  19. 1+0 records out 
  20. Removing intermediate container 93929b2a75ce 
  21. ---> 9fbb2427fc1d 
  22. Successfully built 9fbb2427fc1d 
  23. Successfully tagged test1:latest 
  24. [vagrant@localhost ~]$ docker image ls 
  25. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE 
  26. test1               latest              9fbb2427fc1d        5 seconds ago       2.15GB 
  27. alpine              latest              3fd9065eaf02        4 months ago        4.15MB 
  28. [vagrant@localhost ~]$ docker system df 
  29. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  30. Images              2                   1                   2.152GB             2.152GB (100%) 
  31. Containers          1                   0                   0B                  0B 
  32. Local Volumes       0                   0                   0B                  0B 
  33. Build Cache                                                 0B                  0B 
  34. [vagrant@localhost ~]$ 

我們再修改下dockerfile,改成寫2個1G的文件,然后重新build,我們看到系統(tǒng)的空間變成了之前的兩倍,并沒有變成3G,也就是它用了之前的image作為cache

  1. [vagrant@localhost ~]$ more Dockerfile  
  2. FROM alpine  
  3. RUN dd if=/dev/zero of=1g3.img bs=1G count=1  
  4.   
  5. [vagrant@localhost ~]$ docker build -t test1 .  
  6. Sending build context to Docker daemon  125.4kB  
  7. Step 1/2 : FROM alpine  
  8. ---> 3fd9065eaf02  
  9. Step 2/2 : RUN dd if=/dev/zero of=1g3.img bs=1G count=1  
  10. ---> Running in 6d9e95f54e26  
  11. 1+0 records in  
  12. 1+0 records out  
  13. Removing intermediate container 6d9e95f54e26  
  14. ---> 4ebfe90ead11  
  15. Successfully built 4ebfe90ead11  
  16. Successfully tagged test1:latest  
  17. [vagrant@localhost ~]$ docker system df  
  18. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE  
  19. Images              2                   1                   1.078GB             1.078GB (100%)  
  20. Containers          1                   0                   0B                  0B  
  21. Local Volumes       0                   0                   0B                  0B  
  22. Build Cache                                                 0B                  0B  
  23. [vagrant@localhost ~]$  

但是有時候我們并不會這么幸運,假如我們的Dockerfile變成

  1. [vagrant@localhost ~]$ more Dockerfile 
  2. FROM alpine 
  3. RUN echo test 
  4. RUN dd if=/dev/zero of=1g3.img bs=1G count=1 
  5. RUN dd if=/dev/zero of=1g3.img bs=1G count=1 

RUN echo test 的位置導(dǎo)致docker build image時候不會去使用之前的cache,那么災(zāi)難就來了。我們的image變成了3個,我們的系統(tǒng)空間占用變成了4G,之前的test1變成了一個, 這個僵尸image叫dangling images, 這時候怎么辦呢?

  1. [vagrant@localhost ~]$ docker image ls 
  2. REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE 
  3. test1               latest              8c32028a8557        45 seconds ago      2.15GB 
  4. <none>              <none>              9fbb2427fc1d        5 minutes ago       2.15GB 
  5. alpine              latest              3fd9065eaf02        4 months ago        4.15MB 
  6. [vagrant@localhost ~]$ docker system df 
  7. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  8. Images              3                   1                   4.299GB             4.299GB (100%) 
  9. Containers          1                   0                   0B                  0B 
  10. Local Volumes       0                   0                   0B                  0B 
  11. Build Cache                                                 0B                  0B 
  12. [vagrant@localhost ~]$ 

這時候,其實我們手動通過docker image rm可以刪除這兩個容器,從而釋放空間。但是有點麻煩對吧。這時候我們可以試試一個命令

  1. vagrant@localhost ~]$ 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 build cache 
  7. Are you sure you want to continue? [y/N] y 
  8. Deleted Containers: 
  9. 8b7f9b1e11b85c6d2335b17ea2c303cf500f2a19cccd57864fb1eeceb4021a5d 
  10.  
  11. Deleted Images: 
  12. deleted: sha256:9fbb2427fc1dbaba37fd67a81f84970255e50325ea128aa06bcc60a138835ce8 
  13. deleted: sha256:63f58c3c4640f0ed9b1d917e2f01b0ca461929768712a4c8899cbf3b27f0d716 
  14. deleted: sha256:4ebfe90ead11af51f131372292709d590e4856bb6bf9855e1bd1e5b801920364 
  15. deleted: sha256:45f5bb8b1109d2e61b2aa4ab2d392582cbe89bba99d2e3a3d15192500ed5d22c 
  16.  
  17. Total reclaimed space: 2.147GB 
  18. [vagrant@localhost ~]$ docker system df 
  19. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  20. Images              2                   0                   2.152GB             2.152GB (100%) 
  21. Containers          0                   0                   0B                  0B 
  22. Local Volumes       0                   0                   0B                  0B 
  23. Build Cache                                                 0B                  0B 
  24. [vagrant@localhost ~]$ 

docker system prune會清理所有已停止的容器,沒有被用的network,所有的僵尸image,還是build cache。能夠迅速幫我們清理空間。

當(dāng)然docker system prune 我們可以加一個 -a 參數(shù),這個就厲害了,除了刪除之前docker system prune能刪除的東西以外,他還會刪除所有沒有容器使用的image,比如

  1. [vagrant@localhost ~]$ docker system prune -a 
  2. WARNING! This will remove: 
  3.        - all stopped containers 
  4.        - all networks not used by at least one container 
  5.        - all images without at least one container associated to them 
  6.        - all build cache 
  7. Are you sure you want to continue? [y/N] y 
  8. Deleted Images: 
  9. untagged: alpine:latest 
  10. untagged: alpine@sha256:7df6db5aa61ae9480f52f0b3a06a140ab98d427f86d8d5de0bedab9b8df6b1c0 
  11. untagged: test1:latest 
  12. deleted: sha256:8c32028a8557a1bdd9cc0bdba9b0bb6e9f3c52e139c62de166b24ac3b2abddab 
  13. deleted: sha256:cf8eec9e8e0dfdb48a628e284a8bce27b5d6968e94baacc16ca47ef9d667dc82 
  14. deleted: sha256:91e956dd9cf9f736e9b0ee7a7211e91b6858ad746d3b1cf7713e1492da575c04 
  15. deleted: sha256:e7264a1948b8edae8f94172c5034d5223e4bf045d1d7eb00b431402d52528aec 
  16. deleted: sha256:8bbc26e497f57eb0caef4a26a06333f32522ab0d8fae32637a5a85c2d477436e 
  17. deleted: sha256:3fd9065eaf02feaf94d68376da52541925650b81698c53c6824d92ff63f98353 
  18. deleted: sha256:cd7100a72410606589a54b932cabd804a17f9ae5b42a1882bd56d263e02b6215 
  19.  
  20. Total reclaimed space: 2.152GB 
  21. [vagrant@localhost ~]$ docker system df 
  22. TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE 
  23. Images              0                   0                   0B                  0B 
  24. Containers          0                   0                   0B                  0B 
  25. Local Volumes       0                   0                   0B                  0B 
  26. Build Cache                                                 0B                  0B 
  27. [vagrant@localhost ~]$ 

test1這個好的image也被刪了,因為沒有容器使用它,所以 -a 參數(shù)要小心使用。

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

 

責(zé)任編輯:武曉燕 來源: 死磕網(wǎng)絡(luò)攻城獅
相關(guān)推薦

2021-08-30 15:44:13

數(shù)據(jù)中心云端工作負載

2009-08-22 21:09:02

改變預(yù)分配硬盤空間

2011-09-19 16:03:01

雙系統(tǒng)vista

2013-01-30 13:40:42

Windows 7系統(tǒng)硬盤

2015-08-10 10:07:59

Windows 10硬盤清理

2009-09-08 08:20:00

Windows 7節(jié)省硬盤空間

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歲互聯(lián)網(wǎng)

2013-04-15 15:07:43

清理日志Linux系統(tǒng)

2015-07-08 10:20:51

2021-09-13 05:18:36

硬盤應(yīng)用WizTree

2019-01-10 08:47:11

Windows 10硬盤磁盤

2021-10-27 23:32:06

Windows 11Windows微軟

2018-01-11 15:36:23

命令磁盤空間Docker

2021-12-10 10:21:35

云技術(shù)云計算混合云

2021-04-09 09:55:55

DockerGoLinux
點贊
收藏

51CTO技術(shù)棧公眾號