How to Remove Docker Images, Networks, Containers and Volumes

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

Remove Docker Images, Networks, Containers and Volumes

Docker enables you to build and deploy applications and services as portable containers so that you can run it anywhere. When you are working with Docker it will not remove the excessive number of unused objects until you tell them to do. If you don’t do this then it these unused objects will take your disk space and will mess the output. In this tutorial, you are going to learn how to remove Docker Images, Networks, Containers and Volumes.

Removing Unused or Dangling Objects

Following Docker command will remove all the images, networks, containers and volumes those are not associated with a container.

docker system prune

The above command will prompt you for confirmation just enter y to continue.

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]

Removing Docker Images

To remove Docker Images you can follow below instructions:

Remove a Specific Docker Image

To remove a specific Docker Image you should first list all the Docker images. To list all the Docker Images run following command:

docker image ls

The output should be:

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
ubuntu                  latest              545acca98ac2        2 months ago        113MB
centos                  latest              781335a6d457        4 months ago        210MB

You can see in the above output IMAGE ID column is there to show the ID of the image.

To remove the image you can run below command. If the image is not used by any container then it will be removed otherwise it will show you the error indicating first remove the container to remove the targeted image. To remove the image you should just specify IMAGE ID with docker image rmi command:

docker image rmi 545acca98ac2

Remove Dangling Images

To remove all the dangling images you can type the following command in the terminal, It will also remove the images those are not tagged:

docker image prune

The output will be:

WARNING! This will remove all dangling images.
Are you sure you want to continue? [y/N] y

Enter y to remove all the dangling images.

Now To remove the images those are not associated with any containers you should run docker image prune command with -a flag:

docker image prune -a

The output should be:

WARNING! This will remove all images without at least one container associated to them.
Are you sure you want to continue? [y/N] y

Remove Images Using Label

To remove images using label you should run docker image prune -a command with --filter option by specifying the label of the image.

The following command will remove images with label first_ubuntu:

docker image prune -a --filter "label=first_ubuntu"

Removing Docker Networks

To remove Docker Networks you can follow below instructions:

Remove a Specific Docker Networks

To remove a specific Docker Network you should first list all the Docker networks. To list all the Docker Networks run following command:

docker network ls

The output should be:

NETWORK ID          NAME                DRIVER              SCOPE
df902d74307a        first_bridge        bridge              local
972bc97b913b        host                host                local
49d337bf91f5        none                null                local

You can see in the above output NETWORK ID column is there to show the ID of the network.

To remove the network you can run below command. To remove the network you should just specify NETWORK ID with docker network rmi command it will remove first_bridge network:

docker network rmi 972bc97b913b

If the network is not used by any container then it will be removed otherwise it will show you the error indicating first remove the container to remove the targeted network.

Remove Dangling Networks

To remove all the dangling network you can type the following command in the terminal, It will also remove the networks those are not tagged:

docker network prune

The output will be:

WARNING! This will remove all dangling networks.
Are you sure you want to continue? [y/N] y

Enter y to remove all the dangling networks.

Remove Network Using Label

To remove networks using label you should run docker network prune -a command with --filter option by specifying the label of the network.

The following command will remove networks with the label first_bridge:

docker network prune -a --filter "label=first_bridge"

Removing Docker Containers

To remove Docker Containers you can follow below instructions:

Remove a Specific Docker Container

To remove a specific Docker Container you should first list all the Docker Containers. To list all the Docker Containers run following command:

docker container ls -a

The output should be:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                      PORTS               NAMES
2ffcc3fb51ca        centos                  "/bin/bash"              3 months ago        Created                                         rising_wave
624c132cf3fb        ubuntu                  "/bin/bash"              4 months ago        Exited (132) 4 months ago                       albert

You can see in the above output CONTAINER ID column is there, to show the ID of the container.

To remove the container you can run below command by just specifying CONTAINER ID with docker container rmi command:

docker container rmi 2ffcc3fb51ca

Remove Stopped Container

First list all the stopped Containers by using following command:

docker container ls -a --filter status=exited --filter status=created

To remove all the stopped containers you can type the following command in the terminal:

docker container prune

The output will be:

WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y

Enter y to remove all the stopped container.

Remove Container Using Label

To remove containers using label you should run docker container prune -a command with --filter option by specifying the label of the container.

The following command will remove container with label first_ubuntu:

docker container prune -a --filter "label=first_ubuntu"

Removing Docker Volumes

To remove Docker Volumes you can follow below instructions:

Remove a Specific Docker Volumes

To remove a specific Docker Volume you should first list all the Docker Volumes. To list all the Docker Volumes run following command:

docker volume ls

The output should be:

DRIVER              VOLUME NAME
local               4e678419bf18adddc3c7a4b23457512af8913af888ba7243dec4b6db64293163
local               67243decaf8913af88878419bba45754b6db64293163f18adddc3c7a4b234e12

You can see in the above output VOLUME NAME column is there to show the NAME of the volume.

To remove the volume you can run below command. To remove the volume you should just specify VOLUME NAME with the docker volume rmi command:

docker volume rmi 4e678419bf18adddc3c7a4b23457512af8913af888ba7243dec4b6db64293163

If the volume is not used by any container then it will be removed otherwise it will show you the error indicating first remove the container to remove the targeted volume.

Remove Dangling Volumes

To remove all the dangling volume you can type the following command in the terminal, It will also remove the volumes those are not tagged:

docker network prune

The output will be:

WARNING! This will remove all dangling volumes.
Are you sure you want to continue? [y/N] y

Enter y to remove all the dangling volumes.

Conclusion

Here you have successfully learned How to Remove Docker Images, Networks, Containers and Volumes. If you have any queries regarding this tutorial then you can comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here