try hard

Reclaiming Disk Space in Docker

Docker
Docker
One day, I encountered a troubling issue when my Amazon EC2 server ran out of storage space. After investigating, I discovered that Docker was consuming a significant portion of this precious space, largely due to the accumulation of cache. In this blog post, I'll delve into why Docker cache can become a storage hog and provide you with detailed steps on how to reclaim that vital disk space.

Docker stores various resources, including images and containers, in a storage driver, which can lead to a substantial increase in disk space usage over time. Docker's default storage driver, devicemapper, is known for its inefficiency in managing storage, which often contributes to this problem.

Check Docker Disk Usage
Begin by examining your Docker storage usage with the docker system df command. This will give you an overview of how much space Docker is currently consuming.
docker system df

Prune Unused Resources
Use the docker system prune command to remove all unused data, including stopped containers and dangling images.
docker system prune

Remove Specific Containers and Images
If you need to delete particular containers or images, you can do so using the following commands:
To remove a container:
# Show all containers
docker container ls
# Remove container
docker rm <container_name_or_id> 

# Show all images
docker image ls
# Remove image
docker rmi <image_name_or_id> 

Clear Build Cache
If you want to clear the build cache, use the docker builder prune command. This command removes all build cache, which can be quite useful for freeing up additional space.
docker builder prune

Adjust Docker Disk Usage
For long-term improvements, consider changing the Docker storage driver to overlay2. This driver is more efficient in managing disk space. Edit the Docker daemon configuration file, typically located at /etc/docker/daemon.json, and add the following configuration:
{ "storage-driver": "overlay2" }

Created at 2023-10-21 06:29:22 UTC

Related blogs