Docker CLI Cheatsheet

This is just a cheatsheet of commands you may wish to execute to manage your docker images/containers.

Docker - Get Latest Built Image ID

docker images -q | sed -n 2p  

Remove stopped Containers

docker rm $(docker ps -a -q)

You may get the message
Error: failed to remove one or more containers
but that just occurs when you have running containers (running containers get passed to the remove command but will faile to get removed.

Stop And Remove All Containers

docker stop $(docker ps -aq)  
docker rm $(docker ps -aq)  

Delete "Dangling" Images

docker rmi $(docker images --quiet --filter "dangling=true")  


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

This could be unsafe if you run it whilst docker images are being pulled.

Delete All Images

docker rmi $(docker images -q)  


IMAGES=`docker images | tail -n +2 | tr -s ' ' | cut -d ' ' -f3`
echo "${IMAGES}" | xargs -I %s docker rmi %s

You won't be able to remove images that running containers are using.

Get the Storage Driver Being Used

Different distributions will use different storage drivers. For example, CentOS and Amazon linux will use the devicemapper driver, whereas Ubuntu will use aufs. To find out which storage driver you are using, run:

docker info | grep "Storage Driver"

Go here to get a list of the storage drivers you can use.



Stuart is a software developer with a passion for Linux and open source projects.

comments powered by Disqus
We are a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for us to earn fees by linking to and affiliated sites. More info.