How to clean Up Docker Resources – Images, Containers, and Volumes

Estimated reading: 3 minutes 136 views

In the previous article, we showed you how to install and use Docker on CentOS 7 .As you work with Docker, you tend to accumulate an excessive number of unused images, volumes, and containers. These resources will clutter the output and consume a lot of disk space. In this tutorial, you will learn how to clean up Docker resources and organize your server.

Purge all

You can clean all the Docker resources including images, stopped containers, volumes, and networks with a single command. You can choose one of the options below:

Reference: Dangling resources are the ones that aren’t related to any running container.

OPTION 1:

docker system prune

This will remove:
– all stopped containers
– all networks not used by at least one container
– all dangling images
– all dangling build cache

OPTION 2:

docker system prune -a

This will remove:
– all stopped containers
– all networks not used by at least one container
– all images without at least one container associated with them
– all build cache

OPTION 3:

docker system prune -a --volumes

This will remove:
– all stopped containers
– all networks not used by at least one container
– all volumes not used by at least one container
– all images without at least one container associated with them
– all build cache

Removing Docker Images

To remove a specific image, you need to know its image ID. You can find the image ID of a Docker container using the “docker images” command

docker images

Remove image:

docker rmi <Image ID>

Removing Dangling Images:

When you build a Docker image, it generally has several layers of images. Dangling images are the layers that do not have any relation with any tagged image. Dangling images consume disk space but serve no purpose. They can be listed using the command:

docker images -f dangling=true

You can remove these images by running the following command:

docker image prune

Remove all images

You can list all the docker images by using the command:

docker images -a

Once you’ve decided to remove them all, you can use this command to delete them all:

docker images -a -q | xargs docker rmi

Removing Containers:

To list the containers, you can use the command:

docker ps -a

To remove the containers, use the command:

docker rm <<Container Name/Container ID>>

Running Containers Temporarily

If you want to run the container only once, you can choose to delete the container automatically once it exits. You can do so using the command:

docker run --rm <<ImageName>>

Removing exited containers

You can filter the exited containers using the “-f” argument. List the exited containers using the command:

docker ps -a -f status=exited

Now that you’ve filtered them, remove them using this command:

docker rm $(docker ps -a -f status=exited -q)

Stop and Remove All Containers

Before doing so, review all the containers on your server by listing them. Only once you’re sure that you want to delete them, run the following commands:

List the containers to review:

docker ps -a

Stop and Remove:

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

Remove a specific volume

To remove a specific volume, you need to know the volume name. To find that out, you can list the volumes.

docker volume ls

REMOVE:

docker volume rm <<VolumeName>>

Conclusion

That’s all we guide you in this article, you will know how to clean up your Docker resources and organize your server.


Leave a Comment