Programster's Blog

Tutorials focusing on Linux, programming, and open-source

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 image prune


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 prune images --all


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.

Last updated: 16th August 2018
First published: 16th August 2018