How to remove Docker Images / Docker Network?

remove docker images and docker network

You can use these steps to quickly remove docker objects like docker images and docker network that are accumulating your disk space. This will help any docker user to finely organize their system from unwanted storage. Let’s see each one by one.

Steps to remove docker images:

Your downloaded docker images will be present in your server unless you remove them manually, therefore to remove them you need to identify the image ID, that you use this command;

$ docker image ls 

After execution, you get something like this.

output:

REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
centos                  latest              75835a67d134        7 days ago          200MB
ubuntu                  latest              2a4cca5ac898        2 months ago        111MB
linuxize/fedora         latest              a45d6dca3361        3 months ago        311MB
java                    8-jre               e44d62cf8862        3 months ago        311MB

You need to identify the image ID you no longer needed and then place it in the docker image rm command.

$ docker image rm 75835a67d134 2a4cca5ac898

If your container is using that image you will get an error message. So you will have to 1st remove the container holding that image.

To remove dangled or unused images use the below command:

Before you run this command, make sure your image has an alt tag, or else an image without an alt tag will be removed.

$ docker image prune 

and your output is,

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

You can now remove the unused image by simply adding the ’ -a ‘ option along with the prune command, like docker image prune –a

And your output will 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

Image in Use:

If you want to remove any images that are in use, you can remove them using the ‘ –filter ‘ option with the condition.

You can remove all images that are created more than 240 hours using the ‘until’

$ docker image prune -a --force --filter "until=240h"

Steps to remove docker networks:

In order to remove docker networks you need to find the network ID, for that use the command:

$ docker network ls

and your output will be:

NETWORK ID          NAME                DRIVER              SCOPE
107b8ac977e3        bridge              bridge              local
ab998267377d        host                host                local
c520032c3d31        my-bridge-network   bridge              local
9bc81b63f740        none                null                local

Once you identify the network ID pass their NETWORK ID to the docker network rm command, like this: docker network rm NETWORK ID

Eg: docker network rm c520032c3d31

If an existing container uses the network, you will get an error same as when we removed images.

So you will have to remove the container first.

Remove all unused network:

To remove all unused network you can use the command given below:

$ docker network prune

with the message where you can continue;

WARNING!
This will remove all networks not used by at least one container.
Are you sure you want to continue? [y/N]

Network in Use:

If you want to remove any network that is in use, you can remove it using the ‘ –filter ‘ option with the condition.

Let’s say you want to remove a network that has been created some 12 hours ago, you can use the below format:

$ docker network prune -a --filter "until=12h"

Hope this was helpful for you to remove docker images and docker network in an easy and simple way.  If you want to remove the docker container or docker volume you check our previous blog post.

For any assistance related to docker, you can Contact Us.

To get updates follow us on Facebook, Twitter, LinkedIn

Subscribe to get free blog content to your Inbox