The purpose of this post is to learn how to easily delete Docker images, containers, networks and volumes. A Docker installation will be required to follow this tutorial. Docker version used to prepare this article:
18.04.0-ce. Note that other Docker version commands can vary.
Delete Docker containers
Command to remove a specific container:
$ docker container rm [container_name / container_id]
Command to delete all stopped containers:
$ docker container prune
Command to delete all stopped containers based on a filter:
$ docker container prune --filter 'NAME=VALUE' # Example: Deletes stopped containers created until 1 minute ago. $ docker container prune --filter 'until=1m'
Delete Docker images
Command to remove a specific Docker image:
$ docker image rm [image_name / image_id]
Command to delete dangling images:
$ docker image prune
Command to remove unused Docker image (previous dangling images included):
$ docker image prune -a
Checking remaining Docker images:
$ docker images # same result as previous command $ docker image ls
Delete Docker networks
Command to remove a specific Docker network:
$ docker network rm [network_name / network_id]
Command to delete unused networks:
$ docker network prune # using --filter you can filter which unused networks you want to delete as we did before with stopped containers $ docker container prune --filter 'NAME=VALUE'
Listing current networks:
$ docker network ls
Delete Docker volumes
Command to remove a specific Docker volume:
$ docker volume rm [volume_name]
Command to delete unused volumes:
$ docker volume prune # using --filter you can filter which unused volumes you want to delete as we did before with stopped containers and networks. $ docker volume prune --filter 'NAME=VALUE'
Deleting Docker containers, images, nets and volumes
Command to delete stopped containers, dangling images, networks and build cache:
$ docker system prune
Using -a at the end of the command will delete all unused images:
$ docker system prune -a
Finally, adding –volumes will delete unused volumes as well:
$ docker system prune -a --volumes
Previous commands will request [y/N] to confirm or not the deletion. You can add the
-f or --forceparameter to avoid prompt and perform the action right away.