You need to understand Dockerimages and containers. 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 the files in the image. When you delete it, you just make a mark so that the container cannot see the file. So this file still exists in the image and takes up disk space.
Docker container is a read-write layer based on the image, which is readable and writable. When reading and writing a file, the file is copied from the image 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 image. Moreover, the read-write layer of this container also takes up disk space.
So, we can only free up disk space by deleting images and containers.
Delete image
sudo docker rmi <Image Name>
Delete container
sudo docker rm <Container Name>
Delete all images
sudo docker rmi -a
Delete all containers
sudo docker rm -a
In addition, the container’s data volume also takes up disk space. You can delete the expired volume through the following command:
sudo docker volume rm $(docker volume ls -qf dangling=true)
Of course, the most violent way is to delete the directory where Docker stores images, containers and data volumes (/var/lib/docker)
Use with caution! ! ! :
sudo service docker stop
sudo rm -rf /var/lib/docker
sudo service docker start
You need to understand Dockerimages and containers. 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 the files in the image. When you delete it, you just make a mark so that the container cannot see the file. So this file still exists in the image and takes up disk space.
Docker container is a read-write layer based on the image, which is readable and writable. When reading and writing a file, the file is copied from the image 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 image. Moreover, the read-write layer of this container also takes up disk space.
So, we can only free up disk space by deleting images and containers.
Delete image
Delete container
Delete all images
Delete all containers
In addition, the container’s data volume also takes up disk space. You can delete the expired volume through the following command:
Of course, the most violent way is to delete the directory where Docker stores images, containers and data volumes (/var/lib/docker)
Use with caution! ! ! :