Cleanup docker frequently used commands from occupied disk space

The benefits of using docker

All services, including databases running inside Docker benefits:

  • Configure all servers are very simple, just install the Docker, so when the new server is much simpler.
  • Can easily move a variety of services between servers, download Docker image can run, you do not need to manually configure the operating environment.
  • Strictly consistent development / test and production environments, do not worry because the environmental problems caused deployment failure.

However, the use of docker would be more up disk space.

The basic steps

  1. Use df -h to view the initial state of the disk;
  2. View create a mirror image size docker image ls
  3. Use docker docker system df view disk usage
  4. Performing docker system prune and cleaning containers and dangling image data volumes and network failure. This command removes all the containers and dangling off the mirror. Example, the name of the mirror contains three 1GB random file is occupied, the name: for dangling mirror, and therefore will be deleted. At the same time, all the intermediate image will be deleted.
  5. Furthermore, the use of docker system prune -a, the -a option can do deep cleaning. Then we will see more serious WARNING information, this command will clean up the entire system, and will only retain the image, container, data volumes and network in real use, requiring extra caution. For example, we can not run prune -a command in a production environment, because some alternate mirror (for backup, rollback, etc.) sometimes need to use, if these images are deleted, you will need to re-download when you run the container.

Manual cleaning of commonly used commands

Delete all closed containers

sudo docker container prune

Delete all dangling mirror (ie no tag Mirror)

docker image prune

Delete all dangling data volumes (ie useless Volume)

docker volume prune

Limit the size of the container log

And related files on ubuntu centos docker, including mirroring, container and so saved in / var / lib / docker directory:
du -hs / var / lib / docker
using the du command to continue to see, can be positioned to take up so much real disk the catalog.

Use truncate command can be a container log files "cleared":
truncate 0 -s / var / lib / Docker / Containers / a376aa694b22ee497f6fc9f7d15d943de91c853284f8f

This may be achieved by configuring logging max-size, it is the following Nginx container docker-compose profile:

nginx:
image: nginx:1.12.1
restart: always
logging:
driver: "json-file"
options:
  max-size: "5g"

After the restart Nginx container, the size of its log files is restricted to 5GB.

Restart docker

Clean the disk did not make it, it may restart the Docker.

Published 291 original articles · won praise 104 · views 410 000 +

Guess you like

Origin blog.csdn.net/Enjolras_fuu/article/details/105099858