8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Linux » Here

Docker : Clean Up Unwanted Containers, Images, Volumes and Networks

It's easy for Docker to consume large amounts of space holding objects you are no longer using. This article shows how to identify and clean up unused containers, images, volumes and networks.

Related articles.

Quick Fixes

Some quick cleanup commands I use regularly.

# Remove all containers that aren't running.
docker rm -vf $(docker ps -a -q --filter "status=exited")

# Remove untagged images.
docker rmi -f $(docker images -q -f "dangling=true")

# Remove unused volumes using "rm" or "prune".
docker volume rm -f $(docker volume ls -f "dangling=true")
docker volume prune -f

# Remove unused networks.
docker network prune -f

The sections below give a brief overview of identifying and removing objects, as well as links to the documentation for each command.

Containers

The docker ps command allows you to identify existing containers.

# Display running containers.
docker ps

# Display all containers using "-a" or "--all".
docker ps -a
docker ps --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

For more information see:

Hope this helps. Regards Tim...

Back to the Top.