使用Docker prune命令定时清理磁盘数据

开源
随着时间的推移,Docker容器和镜像占用的磁盘空间可能会变得非常大,尤其是在生产环境中。为了释放磁盘空间,本文将探讨如何删除Docker相关的不用的数据。

Docker是一个开放源代码软件,它允许开发人员将应用程序打包成“容器”,并在不同的操作系统和环境中快速、轻松地部署。这种灵活性使得Docker成为了开发人员的首选工具之一。然而,随着时间的推移,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
责任编辑:赵宁宁 来源: 微技术之家
相关推荐

2009-08-03 12:21:03

linux at命令linux at命令详定时关机

2023-12-21 13:02:25

Linux系统Ubuntu

2009-08-03 12:15:29

linux at命令linux at命令详定时启动程序

2011-08-22 13:27:55

Ubuntu磁盘清理

2018-01-11 15:36:23

命令磁盘空间Docker

2019-09-17 08:00:24

DockerCompose命令

2012-05-08 11:11:43

Linuxcrontab命令

2023-08-15 16:20:42

Pandas数据分析

2021-05-20 08:07:48

磁盘签名Wipefs

2009-08-03 12:26:00

Linux at命令linux at命令详Win2000

2009-10-22 10:59:29

linux磁盘命令

2009-10-22 11:36:31

linux磁盘管理

2019-09-16 08:28:17

Mysql数据库binlog

2022-11-06 19:34:53

UbuntuLinux

2021-08-04 12:09:04

Linuxdf命令

2009-10-22 10:50:47

linux磁盘管理命令

2009-10-22 10:19:57

linux磁盘命令

2009-10-22 10:54:51

linux磁盘管理命令

2017-01-24 18:49:28

Windows磁盘清理

2015-11-25 13:37:52

磁盘空间LinuxUbuntu
点赞
收藏

51CTO技术栈公众号