How To Remove Docker Containers, Images, Volumes, and Networks

How To Remove Docker Containers, Images, Volumes, and Networks

This guide serves as a cheat sheet to help Docker users keep their system organized, and to free disk space by removing unused Docker containers, images, volumes, and networks.

Docker is an open-source containerization platform that allows you to quickly build, test, and deploy applications as portable containers that can run virtually anywhere.

When working with Docker, you can quickly accumulate a large number of unused objects that consume significant disk space and clutter the output produced by the Docker commands. Docker doesn’t remove unused objects such as containers, images, volumes, and networks unless you explicitly tell it to do so.

This guide serves as a “cheat sheet” to help Docker users keep their system organized, and to free disk space by removing unused Docker containers, images, volumes, and networks.

Removing All Unused Objects

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

docker system prune

This command will prompt you to confirm the operation:

WARNING! This will remove:
        - all stopped containers
        - all networks not used by at least one container
        - all dangling images
        - all build cache
Are you sure you want to continue? [y/N]

Use the -f or --force option to bypass the prompt.

By default, the command doesn’t remove unused volumes to prevent losing important data. If you want to remove all unused volumes, pass the --volumes option:

docker system prune --volumes
WARNING! 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 dangling images
        - all build cache
Are you sure you want to continue? [y/N] y

Removing Docker Containers

Docker containers are not automatically removed when you stop them unless you start the container using the --rm flag.

Removing one or more containers

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

You can get a list of all containers bu invoking the docker container ls command with the -a option:

docker container ls -a

The output should look something like this:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
cc3f2ff51cab        centos                  "/bin/bash"              2 months ago        Created                                         competent_nightingale
cd20b396a061        solita/ubuntu-systemd   "/bin/bash -c 'exec …"   2 months ago        Exited (137) 2 months ago                       systemd
fb62432cf3c1        ubuntu                  "/bin/bash"              3 months ago        Exited (130) 3 months ago                       jolly_mirzakhani

Once you know the CONTAINER ID of the containers you want to delete, pass it to the docker container rm command. For example, to remove the first two containers listed in the output above you would run:

docker container rm cc3f2ff51cab cd20b396a061

If you get an error message similar to the one shown below, it means that the container is running. You’ll need to stop the container before removing it.

Error response from daemon: You cannot remove a running container fc983ebf47

docker docker containers neural networks

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

A beginner’s guide to networking in Docker

To create a network we use the $ docker network create command. The --driver or -d flag sets the driver to be used which defaults to bridge if not provided. We can also control the subnet (IP pool) using the --subnet else Docker will configure it automatically.

Docker Tutorial for Beginners 8 - Build and Run C++ Applications in a Docker Container

Welcome to this on Docker Tutorial for Beginners. In this video provides an Introduction on C++ development with Docker containers. So we will see How to ship C++ Programs in Docker.

List all containers in Docker(Docker command)

We can get a list of all containers in docker using `docker container list` or `docker ps` commands.

What is container? | what is docker | what is docker container ?

In this vide I have explained what is container? what is docker? and what is docker container? Docker solves a classic problem of development vs test, It wor...

Docker Explained: Docker Architecture | Docker Registries

Following the second video about Docker basics, in this video, I explain Docker architecture and explain the different building blocks of the docker engine; docker client, API, Docker Daemon. I also explain what a docker registry is and I finish the video with a demo explaining and illustrating how to use Docker hub.