This tutorial is going to cover how to remove unused Docker images, containers, volumes, and networks.

1. Remove Docker Images

1.1. List All Docker Images

Before removing a Docker image, we have to know its name or id first. And we can use the docker images command to list all Docker images in our environment. Let’s refer to another tutorial for more basic Docker commands.

To list all Docker images:

For example,

To list all Docker images including intermediate images:

1.2. Remove One or More Docker Images

1.2.1. Using the docker image rm command

To remove one ore more Docker images, we can use the docker image rm command which has the syntax as the following:

Some options:

Name, shorthand Default Description
–force, -f false Force removal of the image
–no-prune false Do not delete untagged parents

Let’s see an example which we will remove two Docker images hello-world and alpine:

1.2.2. Using the docker rmi command

We can use the docker rmi, another Docker command to remove one or more Docker images. And the command has syntax as the following:

Some options of the command:

Name, shorthand Default Description
–force, -f false Force removal of the image
–no-prune false Do not delete untagged parents
Let’s see the following example which we use the docker rmi command to delete Docker images: busybox and iron/node

1.3. Remove Unused Docker Images

To remove unused Docker images, we can use the docker image prune command which has the syntax as the following:

Some options of the command:

Name, shorthand Default Description
–all, -a false Remove all unused Docker images, not just dangling ones
–filter Provide filter values (e.g. ‘until= ‘)
–force, -f false Do not prompt for confirmation

By default, the command just removes all dangling Docker images which are layers that have no relationship to any tagged images. By adding the -a option to the command, we can delete all used Docker images.

Let’s see the following example which we are going to delete all unused Docker images, not just dangling ones:

2. Remove Docker Containers

2.1. List All Docker Containers

In similar to removing Docker images, before we remove Docker containers, we have to know its name or id first. And we can use the docker ps command to list all Docker containers in our environment. Let’s refer to another tutorial for more basic Docker commands.

To list all Docker containers (default shows just running):

Or we can use the new Docker syntax:

The sample output:

2.2. Remove One or More Docker Containers

2.2.1. Using the docker container rm command

We can use the docker container rm command which has the syntax as following, to remove one or more Docker containers:

Some options:

Name, shorthand Default Description
–force, -f false Force removal of the running containers (use SIGKILL)
–link, -l false Remove the specified link
–volumes, -v false Remove the volumes associated with the container

Example:

To remove the containers which have IDs: 545ca8616836, 365c4ef9668a

2.2.2. Using the docker rm command

Another Docker command that can be used to remove one or more Docker containers is docker rm. Let’s see its syntax:

The command options are similar to the docker container rm command options.

Example:

To remove container has id: bdfc72b71d26

2.3. Remove Stopped Docker Containers

To remove stopped Docker containers, we can use the docker container prune command which has the syntax as the following:

Some command options:

Name, shorthand Default Description
–filter Provide filter values (e.g. ‘until= ‘)
–force, -f false Do not prompt for confirmation

Example:

Let’s list all containers:

Then remove all stopped containers:

Let’s see the list of containers again:

We can see that only the stopped busybox container was removed while the opensue container is still running.

3. Remove Docker Networks

3.1. List All Docker Networks

To list all Docker networks, we can use the docker network ls command which has syntax as below:

3.2. Remove One or More Docker Networks

3.2.1. Using the docker network rm command

We can use the docker network rm command which syntax is described as below, to remove one ore more Docker networks.

3.3. Remove All Unused Docker Networks

To remove all unused Docker networks, we can use the docker network prune command which syntax is described as below:

Here are some command options:

Name, shorthand Default Description
–filter Provide filter values (e.g. ‘until= ‘)
–force, -f false Do not prompt for confirmation

4. Remove Docker Volumes

4.1. List All Docker Volumes

Before removing any Docker volume, we should know about it first. And to do that we can use the docker volume ls to list all Docker volumes. Let’s see the command syntax:

Some options:

Name, shorthand Default Description
–filter, -f Provide filter values (e.g. ‘dangling=true’)
–format Pretty-print volumes using a Go template
–quiet, -q false Only display volume names

4.2. Remove One or More Docker Volumes

To remove one or more Docker volume, we can use the docker volume rm command which syntax is as below:

Name, shorthand Default Description
–force, -f  false Force the removal of one or more volumes
Example:

To remove Docker volumes: data01, data02

4.3. Remove All Unused Docker Volumes

To remove all unused Docker volumes, we can use the docker volume prune command which has syntax as the following:

Options:

Name, shorthand Default Description
–force, -f false Do not prompt for confirmation
Example:

5. Remove All Docker Unused Data

To remove all Docker unused data which includes used images, stopped containers, unused networks and unused volumes, we can use the docker system prune command which has the following syntax:

Options:

Name, shorthand Default Description
–all, -a false Remove all unused images not just dangling ones
–filter Provide filter values (e.g. ‘until= ‘)
–force, -f false Do not prompt for confirmation

Example:

6. Conclusion

The article has just illustrated how to remove unused Docker images, containers, volumes, and networks especially unused Docker data. In addition, we can see that Docker CLI now provides us more new commands which can be used to manage images, containers, volumes and networks separately.

Here are other related articles for your references:

Attach and Detach from Docker Container

Copy Files, Folders from Host to Docker Container and Vice Versa

Install Docker on Ubuntu 16.04, 15.10, 14.04 Step By Step

Install Docker on CentOS 7.X

Introduction to Docker Compose

Vagrant Docker Provider Tutorial

Using Apache Kafka Docker

Insecure docker registry

How to Get IP Address of a Docker Container

0 0 vote
Article Rating