網站首頁 編程語言 正文
場景:使用docker引擎服務時間久了,會發現磁盤空間越來越大,現在要刪除關于docker相關不用的數據來釋放磁盤空間
先看下docker system命令
docker system 目前擁有四個子命令,分別是:
docker system df
docker system events
docker system info
docker system prune
docker system 其中最重要的一個命令就是 docker system prune 命令,清理沒有使用的數據,包括鏡像數據,已經停止的容器
查看 docker system 幫助
[root@localhost ~]# docker system --help
Usage: docker system COMMAND
Manage Docker
Options:
--help Print usage
Commands:
df Show docker disk usage
events Get real time events from the server
info Display system-wide information
prune Remove unused data
Run 'docker system COMMAND --help' for more information on a command.
docker system df提供Docker整體磁盤使用率的概況,包括鏡像、容器和(本地)volume。所以我們現在隨時都可以查看Docker使用了多少資源。
[root@localhost ~]# docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 10 6 2.652GB 1.953GB (73%)
Containers 6 6 6.922MB 0B (0%)
Local Volumes 0 0 0B 0B
docker system prune如果之前的命令展示出 docker 已經占用了太多空間,我們會開始清理。有一個包辦一切的命令:
[root@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
Total reclaimed space: 0B
根據警告信息可知,這個命令會刪除所有關閉的容器以及dangling鏡像。示例中,含有3個1GB隨機文件的鏡像的名稱被占用了,名稱為:,為dangling鏡像,因此會被刪除。同時,所有的中間鏡像也會被刪除。
更進一步,使用-a選項可以做深度清理。這時我們會看到更加嚴重的WARNING信息:
$ docker system prune -a
WARNING! This will remove:
- all stopped containers
- all volumes not used by at least one container
- all networks not used by at least one container
- all images without at least one container associated to them
Are you sure you want to continue? [y/N] y
Deleted Images:
untagged: test:latest
deleted: sha256:c515ebfa2...
deleted: sha256:07302c011...
deleted: sha256:37c0c6474...
deleted: sha256:5cc2b6bc4...
deleted: sha256:b283b9c35...
deleted: sha256:8a8b9bd8b...
untagged: alpine:latest
untagged: alpine@sha256:58e1a1bb75db1...
deleted: sha256:4a415e366...
deleted: sha256:23b9c7b43...
Total reclaimed space: 2.151GB
這個命令將清理整個系統,并且只會保留真正在使用的鏡像,容器,數據卷以及網絡,因此需要格外謹慎。比如,我們不能在生產環境中運行prune -a命令,因為一些備用鏡像(用于備份,回滾等)有時候需要用到,如果這些鏡像被刪除了,則運行容器時需要重新下載。
此時,所有未綁定容器的鏡像將會被刪除。由于第一次prune命令刪除了所有容器,因此所有鏡像(它們沒有綁定任何容器)都會被刪除。
如何清理none對象
Docker 采用保守的方法來清理未使用的對象(通常稱為“垃圾回收”),例如鏡像、容器、卷和網絡:
除非明確要求 Docker 這樣做,否則通常不會刪除這些對象。這可能會導致 Docker 使用額外的磁盤空間。
對于每種類型的對象,Docker 都提供了一條 prune 命令。
另外,可以使用 docker system prune一次清理多種類型的對象。本主題講解如何使用這些 prune 修剪命令
修剪鏡像
清理none鏡像(虛懸鏡像)
命令: docker image prune
默認情況下,docker image prune 命令只會清理 虛無鏡像(沒被標記且沒被其它任何鏡像引用的鏡像)
root@instance-o70no2nw:~# docker image prune
WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
清理無容器使用的鏡像
命令: docker image prune -a
默認情況下,系統會提示是否繼續。要繞過提示,請使用 -f 或 --force 標志。
可以使用 --filter 標志使用過濾表達式來限制修剪哪些鏡像。例如,只考慮 24 小時前創建的鏡像:
$ docker image prune -a --filter "until=24h"
修剪容器
停止容器后不會自動刪除這個容器,除非在啟動容器的時候指定了 –rm 標志。使用 docker ps -a 命令查看 Docker 主機上包含停止的容器在內的所有容器。你可能會對存在這么多容器感到驚訝,尤其是在開發環境。停止狀態的容器的可寫層仍然占用磁盤空間。要清理掉這些,可以使用 docker?container?prune 命令:
$ docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
默認情況下,系統會提示是否繼續。要繞過提示,請使用 -f 或 --force 標志。
默認情況下,所有停止狀態的容器會被刪除。可以使用 --filter 標志來限制范圍。例如,下面的命令只會刪除 24 小時之前創建的停止狀態的容器:
修剪卷
卷可以被一個或多個容器使用,并占用 Docker 主機上的空間。卷永遠不會被自動刪除,因為這么做會破壞數據。
$ docker volume prune
WARNING! This will remove all volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
修剪網絡
Docker 網絡不會占用太多磁盤空間,但是它們會創建 iptables 規則,橋接網絡設備和路由表條目。要清理這些東西,可以使用 docker network prune 來清理沒有被容器未使用的網絡。
$ docker network prune
修剪一切
docker system prune 命令是修剪鏡像、容器和網絡的快捷方式。在 Docker 17.06.0 及以前版本中,還好修剪卷。在 Docker 17.06.1 及更高版本中必須為 docker system prune 命令明確指定 --volumes 標志才會修剪卷。
$ 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
如果使用 Docker 17.06.1 或更高版本,同時也想修剪卷,使用 --volumes 標志。
$ docker system prune --volumes
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all volumes not used by at least one container
- all dangling images
- all build cache
Are you sure you want to continue? [y/N] y
通常我們定時任務刪除即可
比如我這邊場景是鏡像占用磁盤空間很大,那我定時個刪除鏡像的任務
每天凌晨1點,刪除72小時之外所有沒有被使用的鏡像:
[root@develop-server]# crontab -e
0 1 * * * docker image prune -a --force --filter "until=72h"
docker system info (docker info)
這個命令的縮寫docker info相信大家都很熟悉
[root@localhost ~]# docker system info
Containers: 6
Running: 6
Paused: 0
Stopped: 0
Images: 49
Server Version: 17.06.2-ce
Storage Driver: overlay
Backing Filesystem: xfs
Supports d_type: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 6e23458c129b551d5c9871e5174f6b1b7f6d1170
runc version: 810190ceaa507aa2727d7ae6f4790c76ec150bd2
init version: 949e6fa
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-514.26.2.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 24
Total Memory: 31.21GiB
Name: localhost.localdomain
ID: YTL2:6RWX:IZK6:X4XC:XKMO:WVXD:LXPR:E5GN:GEJB:WIUX:L5YH:PDFB
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
127.0.0.0/8
Registry Mirrors:
http://9zkjjecg.mirror.aliyuncs.com/
https://docker.mirrors.ustc.edu.cn/
Live Restore Enabled: false
詳細的解釋
原文鏈接:https://www.cnblogs.com/chentiao/p/16801576.html
相關推薦
- 2024-01-31 linux部署項目, 報數據庫連接不上錯誤
- 2023-05-06 C++右值引用與移動構造函數基礎與應用詳解_C 語言
- 2022-08-01 GO項目配置與使用的方法步驟_Golang
- 2022-08-12 python利用scatter繪畫散點圖_python
- 2022-06-12 3種Python?實現酷炫進度條的實用方法_python
- 2022-04-01 mybatis if 并且判斷列表是否為空
- 2022-09-22 git-lfs 離線安裝
- 2023-01-08 Android?Application的使用全面解析_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支