If you've been using Docker extensively in your local development workflow, you've likely encountered a common challenge: rapidly diminishing disk space. One common issue you might encounter is the no space left on device
error when pulling images or running containers. While Docker is an incredibly powerful tool for development and deployment, it can be quite the disk space hog if left unchecked due to the accumulation of Docker artifacts over time. Regular Docker cleanup is essential to prevent disk space issues and maintain system performance. In this post, we'll explore the intricacies of Docker disk usage and how you can optimize your local workflow to keep your machine running smoothly.
Before we dive into solutions, it's crucial to understand what's eating up all that precious disk space. Docker's disk usage primarily comes from a few sources:
We can get a breakdown of how much disk space is used by Docker by running:
> docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 188 37 51.98GB 39.13GB (75%)
Containers 60 6 7.026GB 7.026GB (99%)
Local Volumes 21 17 2.985GB 115.1MB (3%)
Build Cache 955 0 2.68GB 2.68GB
Let's break down the main offenders in more detail:
Now that we've identified the space-hogging culprits, let's explore effective strategies to reclaim your disk space:
1. Remove unused images
docker image prune
This command removes dangling images, which are images that are not associated with any containers and don’t have tags. We can add the --all
flag to remove all unused images, not just the dangling ones:
> docker image prune --all
WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y
...
deleted: sha256:aed56c19ab37760be1be44464cac4702014d5cdb2e9851d3def27aefc707246f
deleted: sha256:6f5537365f3cc25e226e339d9f17804f322e69271f25c879234e871271af8c43
deleted: sha256:72c89f3b65d07e8c9a6828aa3eef45179a5fafdc7fa36f8522823762d1fc9ef4
Total reclaimed space: 12.36GB
2. Clean up stopped containers
docker container prune
This removes all stopped containers. Stopped containers persist their state on disk, so removing them should free up this space. In order to move all containers, we must stop all of them first and then run the same prune command.
docker stop $(docker ps -q) && docker container prune
3. Remove unused volumes
docker volume prune
This command removes all unused volumes.
4. Clear the build cache
> docker builder prune
WARNING! This will remove all dangling build cache. Are you sure you want to continue? [y/N] y
ID RECLAIMABLE SIZE LAST ACCESSED
7lizg81c20p2zmcoomyu6rt9t true 216MB 4 weeks ago
...
kfuzv2e0w1jpuc83u61f26ri4 true 0B Less than a second ago
tkrm4heav645tgg03a9wg2q7g true 0B Less than a second ago
Total: 19.24GB
This clears the build cache, which can significantly reduce disk usage.
5. Use multi-stage builds: While this doesn't directly free up space, it helps create smaller images, reducing overall disk usage over time.
6. Leverage .dockerignore: Prevent unnecessary files from being copied into your image by using a .dockerignore
file. For a Node.js project, an example of something you typically want in your .dockerignore
is the node_modules
directory, or the log
directory in a Ruby on Rails project.
To keep your Docker environment from getting bloated over time, some other best practices can help:
latest
tag, use specific version tags for your images. This makes it easier to identify and remove outdated images.docker system df
can give you an overview of Docker's disk usage.RUN
commands and clean up within the same layer to reduce image size.By implementing these strategies and best practices, you can maintain an efficient Docker environment that balances functionality with prudent disk space management. Regular maintenance and thoughtful Docker usage will help you make the most of your resources and keep your development workflow smooth and efficient.