bash – Coffee and Code https://blog.coffeeandcode.com Thoughts on web development, collaboration, our local tech community, and probably tacos. Thu, 26 Oct 2017 18:05:07 +0000 en-US hourly 1 https://wordpress.org/?v=5.2.3 Cleanup Docker Images and Exited Containers https://blog.coffeeandcode.com/cleanup-docker-images-and-exited-containers/ Tue, 09 Feb 2016 20:14:39 +0000 https://blog.coffeeandcode.com/?p=83 Continue reading "Cleanup Docker Images and Exited Containers"

]]>
It’s pretty easy to accumulate Docker containers and images on a development machine. Normally, every container that is ran is preserved on your machine. Try running docker ps -a and see how much you’ve accumulated over time.

Concerned about loss of disk space over time, I found a blog post that talked about cleaning up after Docker. Part of the article talked about removing dangling images, or intermediary images that are created while building other containers.

docker rmi $(docker images -f "dangling=true" -q)

However, I also wanted to clean up exited containers that accumulate from running docker-compose run commands, but leave the containers that are automatically started and stopped from running docker-compose up.

I ended up with the following bash alias to help me retrieve previous disk space:

alias docker-cleanup='docker rm $(docker ps -a -f "name=_run_" -q) && docker rmi $(docker images -f "dangling=true" -q)'

As a bonus, here’s the bash alias I use to quickly connect a terminal session to the default docker-machine.

alias docker-setup='docker-machine start default; eval "$(/usr/local/bin/docker-machine env default)"'
]]>