Clean Docker

Photo by Ian Taylor on Unsplash

Clean Docker

Docker doesn’t remove unused objects

With Docker, we can easily accumulate a large number of unused objects that consume significant disk space and it doesn’t remove unused objects such as containers, images, volumes, and networks unless we explicitly tell it to do.

Removing All Unused Objects

The docker system prune command will remove all stopped containers, all dangling images, and all unused networks:

docker system prune

If you also want to remove all unused volumes, pass the --volumes flag:

docker system prune --volumes

Removing Docker containers

Docker containers are not automatically removed when you stop them unless you start the container using -rm. Running containers with --rm flag is good for those containers that you use for very short while just to accomplish something.

Remove one or more containers

To remove one or more Docker images use the docker container rm command followed by the ID of the containers you want to remove.

docker container rm cc3f2ff51cab cd20b396a061

Remove all stopped containers

To remove all stopped containers use the docker container prune command:

docker container prune

Remove docker images

To remove one or more Docker images use the docker images ls command to find the ID of the images you want to remove.

docker image ls

Remove all unused images

To remove all images which are not referenced by any existing container, not just the dangling ones, use the prune command with the -a flag.

docker image prune -a

Remove volumes

To remove one or more Docker volumes use the docker volume ls command to find the ID of the volumes you want to remove.

docker volume ls

Remove all unused volumes

To remove all unused volumes use the docker image prune command:

docker volume prune

Remove networks

To remove one or more Docker networks use the command:

docker network rm c520032c3d31

Remove all unused networks

docker network prune

Source:

Did you find this article valuable?

Support Gonzalo Federico Fernández by becoming a sponsor. Any amount is appreciated!