A collection of computer systems and programming tips that you may find useful.
Brought to you by Craic Computing LLC, a bioinformatics consulting company.

Friday, September 19, 2014

Clean up unused Docker Containers and Images

Docker is a great way to package applications with all their dependent libraries etc and then deploy them easily on various hosts. It builds on tools like Vagrant.

In Docker you create Images that contain your code, the OS, libraries, etc and Containers which are the instances of the Image which you actually run.

Docker does not do a good job cleaning up old Images and Containers and when you are doing a lot of developing this can become a problem. Various people have proposed ways to handle the issue. The best of the web posts that I have seen is http://blog.stefanxo.com/2014/02/clean-up-after-docker/

Here is a slight restating of those solutions which work for me on MacOSX.

To clean up non-running containers

$ docker ps -a --no-trunc | grep 'Exit' | awk '{print $1}' | xargs docker rm

This removes any containers where the Status contains the string Exit. I have seen containers with no status which perhaps have crashed or hung. You just have to remove these manually.

Once you have removed unused Containers you can then remove unused Images. If you try this the other way round you will get errors that the Images are still in use.

Unused Images look like this in the output of 'docker ps' - they are the ones with <none> as the repository and tag

craic/sinatra_example            v1                  f9d2702eb2f7        2 days ago          481.3 MB
<none>                           <none>              196afed3dded        2 days ago          378.5 MB
<none>                           <none>              56f86f0a985e        2 days ago          378.5 MB

<none>                           <none>              011f588e88db        2 days ago          586.8 MB

To remove them use this command

$ docker images -f dangling=true -q | xargs docker rmi

If you are using Dockerfiles to build your Images (recommended) then you can always rebuild an image should anything get removed accidentally.

No comments:

Archive of Tips