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

 更新时间:2021年10月25日 14:44:40   作者:阳光很暖吧  
磁盘使用过久就会导致磁盘空间越来越小,这时候就需要删除不相关的数据来释放磁盘空间,本文主要使用docker prune命令可定时清理不常用数据的实现,感兴趣的可以了解一下

场景:使用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

详细的解释

参考:

docker-system-—— 一个全新的命令集合

Docker 清理none镜像 Prune命令

到此这篇关于docker prune命令可定时清理不常用数据的实现的文章就介绍到这了,更多相关docker prune 可定时清理不常用数据内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 详细介绍如何安装最新版Docker Compose

    详细介绍如何安装最新版Docker Compose

    Docker Compose是一个用来定义和运行多个复杂应用的Docker编排工具,下面这篇文章主要给大家介绍了关于如何安装最新版Docker Compose的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-07-07
  • 一文教你如何通过 Docker 快速搭建各种测试环境

    一文教你如何通过 Docker 快速搭建各种测试环境

    这篇文章主要介绍了一文教你如何通过 Docker 快速搭建各种测试环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-07-07
  • Docker跨主机容器通信overlay实现过程详解

    Docker跨主机容器通信overlay实现过程详解

    这篇文章主要介绍了Docker跨主机容器通信overlay实现过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-05-05
  • docker(alpine+golang) 中 hosts 不生效问题解决方法

    docker(alpine+golang) 中 hosts 不生效问题解决方法

    这篇文章主要介绍了docker(alpine+golang) 中 hosts 不生效问题解决大全,本文给大家分享了三种解决方法,每种方法给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • Docker daemon 无法启动: does not match with stored UUID错误解决办法

    Docker daemon 无法启动: does not match with stored UUID错误解决办法

    这篇文章主要介绍了Docker daemon 无法启动: does not match with stored UUID错误解决办法的相关资料,需要的朋友可以参考下
    2016-11-11
  • Docker 常见问题解决

    Docker 常见问题解决

    这篇文章主要介绍了如何解决Docker 常见问题,帮助大家更好的理解和使用docker容器,感兴趣的朋友可以了解下
    2020-09-09
  • 如何优化Docker镜像的大小详解

    如何优化Docker镜像的大小详解

    本文详细介绍了Docker的定义和优点,以及如何通过优化Docker镜像来提高其轻量化和便携性,通过使用轻量化基础镜像和多阶段构建,可以显著减小Docker镜像的大小,从而提高部署和运行效率
    2025-03-03
  • docker-compose容器互相连接的实现

    docker-compose容器互相连接的实现

    本文主要介绍了docker-compose容器互相连接的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-03-03
  • docker中如何启动已存在容器

    docker中如何启动已存在容器

    这篇文章主要介绍了docker中如何启动已存在容器问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 使用jib打包docker镜像实战

    使用jib打包docker镜像实战

    这篇文章主要介绍了使用jib打包docker镜像实战,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08

最新评论