The container does not free up disk space while docker is running.

  docker, question

Hello everyone, would you like to ask how docker manages disks? I have a jenkins application in a container. After running for a period of time, the container became very large. Then I deleted some large files in the container. df -h found that neither the virtual partition of the container nor the disk of the host released any space. It was still occupied by 42% (container) and 50% (host). What is the reason for this? How to solve it?

Need to understand Dockermirror imageAndcontainer. Of course, this is understood from the perspective of the file system.

Docker images are read-only files. To emphasize, it is read-only, so we cannot actually delete files in the mirror. When you delete, you just make a mark so that the container cannot see the file. So this file still exists in the mirror and will take up disk space.

The Docker container is a mirror-based read-write layer that is readable and writable. When reading and writing a file, the file is copied from the mirror to the read-write layer of the container, and then the copied file is read and written while the original file is still in the mirror. Moreover, the read-write layer of this container also occupies disk space.

Therefore, we can only free up disk space by deleting mirrors and containers.

Delete mirror

sudo docker rmi <Image Name>

Delete container

sudo docker rm <Container Name>

Delete all mirrors

sudo docker rmi -a

Delete all containers

sudo docker rm -a

In addition, the data volume of the container also occupies disk space, and the invalid volume can be deleted by the following command:

sudo docker volume rm $(docker volume ls -qf dangling=true)

Of course, the most violent way is to delete the directory (/var/lib/doc) of the doc storage mirror, container and data volume

Use carefully! ! ! :

sudo service docker stop
 sudo rm -rf /var/lib/docker
 sudo service docker start