Skip to content

Instantly share code, notes, and snippets.

@DoganM95
Last active June 15, 2024 21:34
Show Gist options
  • Save DoganM95/e3f705a243d7f9228972d5e96cf415a3 to your computer and use it in GitHub Desktop.
Save DoganM95/e3f705a243d7f9228972d5e96cf415a3 to your computer and use it in GitHub Desktop.

Remove all dangling images

Windows

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

Linux

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

Stop all running containers

Windows

docker stop $(docker ps -q)

Linux

docker stop $(docker ps -q)

Remove all stopped containers

Windows

docker rm $(docker ps -a -q)

Linux

docker rm $(docker ps -a -q)

Remove all containers

Windows

docker rm -f $(docker ps -a -q)

Linux

docker rm -f $(docker ps -a -q)

Remove all services

Linux

docker service rm $(docker service ls -q)

Remove all images

Windows

docker rmi $(docker images -q)

Linux

docker rmi $(docker images -q)

Shell into a running container

Windows

docker exec -it $(docker ps -a --format '{{.Names}}' | grep 'substring_of_container_name' | head -n 1) /bin/sh

Linux

docker exec -it $(docker ps -a --format '{{.Names}}' | grep 'substring_of_container_name' | head -n 1) /bin/sh

Stop and remove any running name-defined container

Windows

docker ps -a --format "{{.Names}}" | Where-Object { $_ -like "*somestringincontainername*" } | ForEach-Object { docker stop $_; docker rm $_ }; 

Linux

docker ps -a --format "{{.Names}}" | grep 'somestringincontainername' | while read name; do docker stop "$name"; docker rm "$name"; done

Delete any existing name-defined docker images

Windows

docker images --format "{{.Repository}}:{{.Tag}}" | Where-Object { $_ -like "*somestringincontainername*" } | ForEach-Object { docker rmi $_ }

Linux

docker images --format "{{.Repository}}:{{.Tag}}" | grep 'somestringincontainername' | while read image; do docker rmi "$image"; done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment