Introduction

Docker is an open-source utility for software developers. Docker allows users to create a container in which an application or process can run.

Containers are designed to provide a self-sufficient environment, with all the libraries and configurations needed for the software to execute. During development, they can grow unorganized with old, outdated, and unused components.

In this guide, you will learn how to organize a docker environment by removing Docker images, containers, volumes, and networks. 

Using these commands makes docker container management fast and straightforward.

header for tutorial on removing docker images and containers

Prerequisites

  • Linux system running Docker
  • Access to a terminal/command line (Ctrl-Alt-T in Ubuntu, Alt-F2 in CentOS)
  • User account with sudo privileges

NOTE: Your system may return an “access denied” message entering these commands. If that happens, use the sudo command at the beginning of the command. Then enter your password when prompted.


How to Remove Docker Containers

Containers are the foundation of the Docker application.

A docker container creates a specific environment in which a process can be executed. Since many containers may be created, tested, and abandoned during the development lifecycle, it’s important to have a way to find and remove them.

To remove a docker container, use the command:

docker container ls –aq 

This will generate a list of all the containers and their numeric ID’s.

Stop and remove containers

To stop a specific container, enter the following:

docker container stop ContainerID

Replace ContainerID with the numeric ID of the container from your list. You can enter multiple container ID’s into the same command.

To stop all containers, enter:

docker container stop $(docker container ls –aq)

This forces Docker to use the list of all container ID’s as the target of the stop command.

To remove a stopped container, use the command:

docker container rm ContainerID

Like before, this will remove a container with the ID you specify.

Remove all stopped containers

To remove all stopped containers:

docker container rm $(docker container ls –aq)

Removing All Docker Containers

To wipe Docker clean and start from scratch, enter the following command:

docker container stop $(docker container ls –aq) && docker system prune –af ––volumes

This command instructs Docker to stop the containers listed in the parentheses.

Inside the parentheses, we’re telling Docker to generate a list of all the containers with their numeric ID. This information is passed back to the container stop command and stops all the containers.

The && sign indicates a second function. In this case, we are instructing Docker to remove all stopped containers and volumes.

The –af switch indicates all containers (a) without a confirmation (f) prompt.

Removing containers with filters

You can also specify to delete all objects that do not match a specified label.

Enter the following:

docker container prune ––filter=”label!=maintainer=Jeremy”

This command tells Docker to remove all containers that are not labeled with a maintainer of “jeremy.” The != command is a logical notation that means “not equal to.”

A breakdown of the label commands:

  • label=<key>
  • label=<key>=<value>
  • label!=<key>
  • label!=<key>=<value>

Using these terms in conjunction with labels gives you in-depth control over removing assets in Docker.

How to Remove Docker Images

Docker images are files, which include multiple layers used to run code within a container.

Images may go through many iterations during development. Old and outdated images can clutter your system, taking up storage space and making searches more cumbersome.

To remove a Docker image, start by listing all the images on your system:

docker image ls

Make a note of the IMAGE ID – this is the identifier you’ll use to remove the image.

To remove an image:

docker image rm ImageID1 ImageID2

Replace ImageID1 and ImageID2 with the image ID you pulled from the first command. You can enter a single Image ID, or multiple Image IDs for removal.

The system may respond to your request with an error message, that there is a conflict and it is unable to remove the repository reference. This indicates that a container is using the image. You’ll need to remove the container first before you can remove the image.

Removing Docker Images With Filters

At the time of publication, the only supported filters are until and label. However, these can still be powerful tools for managing Docker resources.

Use the until filter to remove all resources up to a given time.

Enter the following:

docker image prune –a ––filter “until=24h”

This will remove all (–a) images created over the last 24 hours. This command can be used for containers, images, and filters. Make sure you specify the asset you want to remove.

The until command will accept Unix timestamps, date-formatted timestamps, or an amount of time (30m, 4h, 2h25m) calculated against the machine time.

Use the label command to remove assets defined by labels.

Enter the following:

docker image prune ––filter=”label=old”

This will remove all docker images that have been labeled “old”.

Filtering can also be used to specify a specific value of a label.

For example, if a container is labeled with a “maintainer” key, and the value of “maintainer” is either “bill” or “jeremy,” you could enter the following:

docker container prune ––filter=”label=maintainer=bill”

Docker would remove all containers with the label “maintainer” with a value of “bill.”

How to Remove Docker Volumes

Docker containers are designed to be fast and lean, so they generally aren’t used to store data. Instead, a volume is used to store Docker data.

A volume helps store and organize data outside containers in a way that it’s accessible to multiple containers.

Use the following command to generate a list of all the available Docker volumes:

docker volume ls

Take note of the VOLUME NAME of the volume you want to remove.

Then enter: docker volume rm VolumeName

Make sure to replace VolumeName with the actual name you generated with the previous command.

The system may respond with an error, noting that the volume is in use by an existing container. If that happens, you’ll need to remove the container first.

How to Remove Docker Networks

Docker networks are designed to allow different containers to communicate with each other freely while also preventing traffic from outside the network. This is typically done with a Docker bridge network.

The prune command (as above) will remove all unused networks.

Removing a Single Network

To remove a single network, start by listing all the current networks.

To display a list of all existing Docker networks:

docker network ls

Take note of the NETWORK ID – this is the identifier you’ll use to remove a specific network.

Enter: docker network rm NetworkID

Replace NetworkID with the network ID you captured from the first command.

You may receive an error message that says the network has active endpoints. That means that the network is currently in use by containers. You’ll need to remove the containers that are using the network before you can remove the network.

List all Docker Resources

Enter the following commands to display all of the specified resources:

docker container ls

docker image ls

docker volume ls

docker network ls

docker info – (lists number of containers, images, and information about the Docker installation)

These commands can help you locate and identify resources that you want to keep, or that you want to delete.

The following flags can also be added:

  • –a display all of a resource (including the ones that are stopped)
  • –q or --quiet (display only the numeric ID)

Remove All Unused Docker Objects

The prune command automatically removes all resources that aren’t associated with a container. This is a streamlined way to clean up unused images, containers, volumes, and networks.

In a terminal window, enter the following:

docker system prune

Additional flags can be included:

  • –a To include stopped containers and unused images
  • –f Bypasses confirmation dialog
  • ––volumes Removes all unused volumes

Also, you can specify a single type of object to be removed, instead of the entire environment:

docker container prune

docker image prune

docker volume prune

docker network prune

When running docker system prune -a, both unused and dangling images will be removed. Images used in a container, either currently running or exited, will NOT be affected.

Conclusion

In this guide, you learned the most common commands for removing Docker images, containers, volumes, and networks.

During the development cycle of a Docker application, it’s easy to use a lot of storage with old versions of images and containers.

Remember, the prune command is an excellent shortcut for cleaning up unused assets and recovering storage space. You can also refine control by using targeted removal of specific objects, or even by filtering by time and labels.