We have some
ansible scripts that deploy
docker containers on to various environments. All has been going fine until this week, when we fell foul of the untidy teenager that is docker!
Our deployment was failing as the box had run out of disk space. Turns out you need to tidy up after docker...
Docker is a great toolset but is still evolving, and garbage collection is one of the things on the roadmap but not yet implemented. What we didn't realise was that every time we pull a new image, it can leave
dangling images and
exited containers on the machine.
Basically, the layers of images that make up a complete image can build up over time when you build or pull images. If some of those layers are no longer needed, they just sit on the machine taking up disk space.
Similarly, any containers that we have stopped which are no longer needed are just sitting there taking up disk space.
For us the fix was to run the
docker rmi (remove images) and
docker rm (remove containers) commands.
To remove only the dangling images, first list them with
docker images --quiet --filter dangling=true, and then pass that list into
docker rmi $(docker images -qf "dangling=true")
If you need to suppress the warning / error when there is no result from
docker images then pipe it into
docker images -qf dangling=true | xargs --no-run-if-empty docker rmi
Similarly, to remove exited containers, run..
docker ps -aqf status=exited | xargs --no-run-if-empty docker rm
You may want to add the
-v flag to
docker rm to remove the volume as well
Be warned, if you are using a container to simply create a data volume, which is then mounted into another container, you DON'T want to remove your stopped data container. If you do, the next time you run the container that uses the data container via
--volumes-fromargument, it will no longer be able to find the container, and thus not be able to find its volume!
These command work for our workflow, but they are deleting images and containers, and it may have un wanted side affects in your workflow - i.e delete something you need!
One such issue I have read is that if you run
docker rmi -gf dangling=true DURING a pull, images being pulled down at that point in time could get removed.