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.



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

comments powered by Disqus