Skip to content

Instantly share code, notes, and snippets.

@maksimerohin
Last active January 25, 2024 20:37
Show Gist options
  • Save maksimerohin/2d13b197e5cca9cb150cab800ee2969c to your computer and use it in GitHub Desktop.
Save maksimerohin/2d13b197e5cca9cb150cab800ee2969c to your computer and use it in GitHub Desktop.

Clean Up Unwanted Containers, Images, Volumes and Networks

https://oracle-base.com/articles/linux/docker-clean-up-unwanted-containers-images-layers-volumes-networks

docker network ls выводит список сетей docker network prune Remove unused networks docker network prune -f Remove unused networks

Containers

The docker ps command allows you to identify existing containers.

docker ps Display running containers.

docker ps -a
docker ps --all Display all containers using "-a" or "--all".

Filter output using "-f" or "--filter". docker ps -f "status=exited" docker ps --filter "status=exited"

Show only the container ID using "-q" or "--quiet". docker ps -q -f "status=exited" docker ps --quiet --filter "status=exited" Containers are removed using the docker rm command.

Remove an individual container by ID or name. Use "-v" or "--volumes" to remove associated volumes. Use "-f" or "--force" to remove running containers. docker rm -vf b0479f9d1ea4 docker rm --volumes --force ol7_ords_con

Remove all the containers matching the "ps" output. docker rm -vf $(docker ps -a -q --filter "status=exited")

Images

Images are displayed using the docker images command.

Show top-level images only. docker images

Show all images using "-a" or "--all". docker images -a docker images --all

Filter the output using "-f" or "--filter". docker images -f "dangling=true" docker images --filter "dangling=true"

Display only the image ID using "-q" or "--quiet". docker images -q -f "dangling=true" docker images --quiet --filter "dangling=true" Images are removed using the docker rmi command.

Remove image by image ID or repository:tag docker rmi ffcd22192b23 docker rmi ol7_122:latest

Force the remove using "-f" or "--force". docker rmi -f ffcd22192b23 docker rmi --force ol7_122:latest

Remove images matching list. docker rmi -f $(docker images -q -f "dangling=true")

Volumes

Docker volumes are listed using the docker volume ls command.

List all volumes. docker volume ls

Filter output using "-f" or "--filter". docker volume ls -f "dangling=true" docker volume ls --filter "driver=local"

Display only volume name using "-q" or "--quiet". docker volume ls -q -f "driver=local" docker volume ls --quiet --filter "driver=local" Volumes are removed using the docker volume rm command.

Remove specified volume. docker volume rm test_vol

Force removal using "-f" or "--force". docker volume rm -f test_vol docker volume rm --force test_vol

Remove unused volumes. docker volume rm -f $(docker volume ls -f "dangling=true") You can also use the docker volume prune command.

Remove unused volumes. docker volume prune -f

Networks

Networks don't waste any disk space, but you might want to clean up unused networks anyway.

Networks are listed using the docker network ls command.

List all networks. docker network ls

Filter output using "-f" or "--filter". docker network ls -f "driver=bridge" docker network ls --filter "driver=bridge"

Display only network ID using "-q" or "--quiet". docker network ls -q -f "driver=bridge" docker network ls --quiet --filter "driver=bridge" Networks are removed using the docker network rm command.

Remove network by name or ID. docker network rm my_network2 docker network rm 6466079abd47 Alternatively, you can remove unused networks using the docker network prune command.

Remove unused networks. docker network prune

Force prune using "-f" or "--force". docker network prune -f docker network prune --force

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment