docker使用时间长了总会产生一些临时镜像,这些镜像一般不被用到,这篇就记录一些清理docker镜像或容器等的命令。
清理镜像
- docker image prune清理没有被使用的镜像(- <none>标签的镜像,这些镜像被称为- dangling image)
- docker image prune -a清理没有容器使用的镜像(慎用!会删除那些用来做版本回退的镜像)
- docker image prune -f或者用- --force参数跳过警告,直接删除
- docker image prune --filter "until=240h"过滤删除,删除创建时间超过10天的并且未使用的镜像。
# 使用format命令格式化镜像表格信息
$ docker images --format 'table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedAt}}\t{{.Size}}'
REPOSITORY             TAG                 IMAGE ID            CREATED AT                      SIZE
halohub/halo           1.4.16              b7b9923025c2        2021-12-12 20:22:44 +0800 CST   325MB
redis                  latest              bc8d70f9ef6c        2021-05-13 03:07:40 +0800 CST   105MB
busybox                latest              f35646e83998        2020-10-13 16:39:44 +0800 CST   133MB
#这样就可以使用具体时间的过滤方式
$ docker image prune -a --force --filter "until=2017-01-04T00:00:00"
更多关于filter参数可参考Docker-docs
清理容器
docker container prune [OPTIONS]
| OPTIONS | DESC | 
|---|
| --force,-f | 不提示删除 | 
| --filter | 过滤删除,如: --filter "until=20h" | 
清理卷
docker volume prune [OPTIONS]
| OPTIONS | DESC | 
|---|
| --force,-f | 不提示删除 | 
| --filter | 过滤删除,如: --filter "until=20h" | 
清理网络
docker network prune [OPTIONS]
| OPTIONS | DESC | 
|---|
| --force,-f | 不提示删除 | 
| --filter | 过滤删除,如: --filter "until=20h" | 
清理系统
docker system prune [OPTIONS]
| OPTIONS | DESC | 
|---|
| -a | 清理没有容器使用的镜像 | 
| --force,-f | 不提示删除 | 
| --filter | 过滤删除,如: --filter "until=20h" | 
| --volumes | 清理卷(默认不清理卷) | 
$ docker system prune
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all dangling images
  - all dangling build cache
$ 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 dangling build cache