I have this one Docker image, which I need every 2-3 months to run a task in an outdated piece of software, which is no longer supported on modern systems. That works fine, but after a while I figured that Docker is keeping all the build layers, even though I remove the images and containers. Worst, sometimes I update the build for the Dockerfile, and the entire image needs to be rebuild. Still Docker caches all the previous build layers as well.
Removing the build layers is not obvious, there is no specific Docker command.
Docker images, containers, and build layers
When building an image, Docker might create multiple layers internally, and combine everything into the final image. This image is then started as container. Let’s look at building an example image:
| |
And clean out images and containers:
| |
Still, docker system df tells me that 19.82GB are used by the layer cache. That’s a lot of layers which I “collected” over time, and certainly the majority of them are no longer necessary. And the image was built in less than a second, which means it’s all cached and no new downloads. Let’s clean this up.
Clear the docker layer cache
Docker has commands for showing (docker images) and clearing (docker image prune) the images, or for containers (docker container ls). But the layer cache is somewhat hidden behind the docker system command. Not obvious.
docker system prune --all (or docker system prune --all --force) will clear this cache:
| |
And now I also have to figure out if the layer cache accidentally ended up in my backups. But that is a different story.
Summary
Docker keeps all built layers around, in case they are needed for building another image. The docker system prune command will clear the build cache.
Header image
Image by len_lov from Pixabay, modifications by me.
Comments
With an account on the Fediverse or Mastodon, you can respond to this post. Known replies are displayed below:
Note: This will load data from mastodon.social.
Learn how this is implemented here.
Share: