docker|docker prune命令可定时清理不常用数据的实现

目录

  • docker system df
  • docker system prune
  • 如何清理none对象
    • 修剪镜像
    • 清理无容器使用的镜像
    • 修剪容器
    • 修剪卷
    • 修剪网络
    • 修剪一切
场景:使用docker引擎服务时间久了,会发现磁盘空间越来越大,现在要删除关于docker相关不用的数据来释放磁盘空间
先看下docker system命令

docker system 目前拥有四个子命令,分别是:
docker system dfdocker system eventsdocker system infodocker system prune

docker system 其中最重要的一个命令就是 docker system prune 命令,清理没有使用的数据,包括镜像数据,已经停止的容器
查看 docker system 帮助
[root@localhost ~]# docker system --helpUsage:docker system COMMANDManage DockerOptions:--helpPrint usageCommands:dfShow docker disk usageeventsGet real time events from the serverinfoDisplay system-wide informationpruneRemove unused dataRun 'docker system COMMAND --help' for more information on a command.


docker system df
提供Docker整体磁盘使用率的概况,包括镜像、容器和(本地)volume。所以我们现在随时都可以查看Docker使用了多少资源。
[root@localhost ~]# docker system dfTYPETOTALACTIVESIZERECLAIMABLEImages1062.652GB1.953GB (73%)Containers666.922MB0B (0%)Local Volumes000B0B


docker system prune
如果之前的命令展示出 docker 已经占用了太多空间,我们会开始清理。有一个包办一切的命令:
[root@localhost ~]# docker system pruneWARNING! 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] yTotal reclaimed space: 0B

根据警告信息可知,这个命令会删除所有关闭的容器以及dangling镜像。示例中,含有3个1GB随机文件的镜像的名称被占用了,名称为:,为dangling镜像,因此会被删除。同时,所有的中间镜像也会被删除。
更进一步,使用-a选项可以做深度清理。这时我们会看到更加严重的WARNING信息:
$ docker system prune -aWARNING! 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 themAre you sure you want to continue? [y/N] yDeleted Images:untagged: test:latestdeleted: sha256:c515ebfa2...deleted: sha256:07302c011...deleted: sha256:37c0c6474...deleted: sha256:5cc2b6bc4...deleted: sha256:b283b9c35...deleted: sha256:8a8b9bd8b...untagged: alpine:latestuntagged: 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 pruneWARNING! This will remove all dangling images.Are you sure you want to continue? [y/N] yTotal 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 pruneWARNING! This will remove all stopped containers.Are you sure you want to continue? [y/N] y

默认情况下,系统会提示是否继续。要绕过提示,请使用 -f 或 --force 标志。
默认情况下,所有停止状态的容器会被删除。可以使用 --filter 标志来限制范围。例如,下面的命令只会删除 24 小时之前创建的停止状态的容器:

修剪卷

卷可以被一个或多个容器使用,并占用 Docker 主机上的空间。卷永远不会被自动删除,因为这么做会破坏数据。
$ docker volume pruneWARNING! 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 pruneWARNING! This will remove:- all stopped containers- all networks not used by at least one container- all dangling images- all build cacheAre you sure you want to continue? [y/N] y

如果使用 Docker 17.06.1 或更高版本,同时也想修剪卷,使用 --volumes 标志。
$ docker system prune --volumesWARNING! 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 cacheAre you sure you want to continue? [y/N] y

通常我们定时任务删除即可

比如我这边场景是镜像占用磁盘空间很大,那我定时个删除镜像的任务
每天凌晨1点,删除72小时之外所有没有被使用的镜像:
[root@develop-server]# crontab -e0 1 * * * docker image prune -a --force --filter "until=72h"

docker system info (docker info)

这个命令的缩写docker info相信大家都很熟悉
[root@localhost ~]# docker system infoContainers: 6 Running: 6 Paused: 0 Stopped: 0Images: 49Server Version: 17.06.2-ceStorage Driver: overlay Backing Filesystem: xfs Supports d_type: trueLogging Driver: json-fileCgroup Driver: cgroupfsPlugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslogSwarm: inactiveRuntimes: runcDefault Runtime: runcInit Binary: docker-initcontainerd version: 6e23458c129b551d5c9871e5174f6b1b7f6d1170runc version: 810190ceaa507aa2727d7ae6f4790c76ec150bd2init version: 949e6faSecurity Options: seccompProfile: defaultKernel Version: 3.10.0-514.26.2.el7.x86_64Operating System: CentOS Linux 7 (Core)OSType: linuxArchitecture: x86_64CPUs: 24Total Memory: 31.21GiBName: localhost.localdomainID: YTL2:6RWX:IZK6:X4XC:XKMO:WVXD:LXPR:E5GN:GEJB:WIUX:L5YH:PDFBDocker Root Dir: /var/lib/dockerDebug Mode (client): falseDebug Mode (server): falseRegistry: https://index.docker.io/v1/Experimental: falseInsecure Registries: 127.0.0.0/8Registry Mirrors: http://9zkjjecg.mirror.aliyuncs.com/ https://docker.mirrors.ustc.edu.cn/Live Restore Enabled: false

详细的解释

docker|docker prune命令可定时清理不常用数据的实现
文章图片

参考:

docker-system-—— 一个全新的命令集合
Docker 清理none镜像 Prune命令
【docker|docker prune命令可定时清理不常用数据的实现】到此这篇关于docker prune命令可定时清理不常用数据的实现的文章就介绍到这了,更多相关docker prune 可定时清理不常用数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

    推荐阅读