Docker数据清理
docker system 命令
$ docker system --help
Usage: docker system COMMAND
Manage Docker
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 system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 7 2 2.284GB 1.791GB (78%)
Containers 7 0 932MB 932MB (100%)
Local Volumes 23 2 2.321GB 1.909GB (82%)
Build Cache 0 0 0B 0B
docker system prune 清理无用数据
$ docker system prune --help
Usage: docker system prune [OPTIONS]
Remove unused data
Options:
-a, --all Remove all unused images not just dangling ones
--filter filter Provide filter values (e.g. 'label=<key>=<value>')
-f, --force Do not prompt for confirmation
--volumes Prune volumes
清除镜像:
docker system prune -a
清除volumes:
docker system prune --volumes
/var/lib/docker/overlay2 文件夹过大问题
查看/var/lib/docker/overlay2文件夹大小
root@iZbp16ky4kyfg36h94prmfZ:/etc/docker# du -shc /var/lib/docker/overlay2/*/diff | grep total
14G total
root@iZbp16ky4kyfg36h94prmfZ:/etc/docker# du -shc /var/lib/docker/overlay2/ | grep total
24G total
root@iZbp16ky4kyfg36h94prmfZ:/etc/docker#
You can see that the actual disk allocation (df command) is just cca 200MB more than before, but “du” on whole folder outputs 4.2G allocation.
“du” on “diff” folders shows 212M what is correct.
This is how Docker works and what makes it great!
出现原因
By default, Docker uses the json-file log driver and saves those logs in the overlay folder. Docker doesn’t limit the size of the files or how many log files there can be for a single container. If you continue to run that container, the log file will grow as long as it has drive space to do so. Thankfully, it’s pretty easy to set these limits.
清理方案
清楚未使用的镜像
docker system prune -a // Remove all unused images not just dangling ones
清楚容器或镜像等
docker [image/container] prune -a
清楚volumes
docker system prune --volumes
清理日志文件:
sudo sh -c "truncate -s 0 /var/lib/docker/containers/*/*-json.log"
预防方案
If you are running from the CLI, your option looks like:
docker run --log-opt max-size=10m --log-opt max-file=5 my-app:latest
This sets a maximum of five log files with a max size of 10 Mb each. So at most 50 Mb of logs for that container. Tune those numbers as you see fit.
If you are using docker-compose, your options should look like:
my-app:
image: my-app:latest
logging:
driver: "json-file"
options:
max-file: 5
max-size: 10m
You can also configure this as a default in your daemon settings docs.docker.com/config/cont…
在daemon.json中配置
{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3",
"labels": "production_status",
"env": "os,customer"
}
}
参考
查看容器占用资源情况
查看运行的容器占用的磁盘空间
docker ps --size
输出结果说明:
- Size - The amount of data (on disk) that is used by each container (write)
- Virtual size -The amount of disk space used for the read-only image of each container.
查看容器实时资源使用情况
$docker stats
\