How to use docker network prune command to remove unused networks

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to effectively manage your Docker networks by utilizing the docker network prune command. We will begin by creating several custom networks to simulate a real-world scenario with multiple networks.

Following the creation of these networks, you will learn how to list and identify which networks are currently unused. Finally, you will practice using the docker network prune command to remove these unused networks, including how to apply filters for more targeted pruning. This hands-on experience will equip you with the skills to keep your Docker environment clean and optimized.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL docker(("Docker")) -.-> docker/SystemManagementGroup(["System Management"]) docker(("Docker")) -.-> docker/NetworkOperationsGroup(["Network Operations"]) docker(("Docker")) -.-> docker/ContainerOperationsGroup(["Container Operations"]) docker/ContainerOperationsGroup -.-> docker/ls("List Containers") docker/ContainerOperationsGroup -.-> docker/ps("List Running Containers") docker/ContainerOperationsGroup -.-> docker/rm("Remove Container") docker/SystemManagementGroup -.-> docker/prune("Remove Unused Docker Objects") docker/NetworkOperationsGroup -.-> docker/network("Manage Networks") subgraph Lab Skills docker/ls -.-> lab-555178{{"How to use docker network prune command to remove unused networks"}} docker/ps -.-> lab-555178{{"How to use docker network prune command to remove unused networks"}} docker/rm -.-> lab-555178{{"How to use docker network prune command to remove unused networks"}} docker/prune -.-> lab-555178{{"How to use docker network prune command to remove unused networks"}} docker/network -.-> lab-555178{{"How to use docker network prune command to remove unused networks"}} end

Create some custom networks

In this step, we will learn how to create custom networks in Docker. By default, Docker provides three networks: bridge, host, and none. However, you can create your own custom networks to isolate containers and control how they communicate with each other.

To create a custom network, we use the docker network create command. Let's create a bridge network named my-network-1.

docker network create my-network-1

You should see output similar to this, indicating the network was created successfully:

<network_id>

Now, let's create another bridge network named my-network-2.

docker network create my-network-2

Again, you should see a network ID as output.

We can verify that the networks have been created by listing all available networks using the docker network ls command.

docker network ls

The output will show a list of networks, including the default networks and the two custom networks we just created (my-network-1 and my-network-2).

List networks to identify unused ones

In this step, we will learn how to list Docker networks and identify those that are not currently being used by any containers. This is useful for cleaning up your Docker environment and freeing up resources.

We already used the docker network ls command in the previous step to list all networks. Let's run it again to see the current state.

docker network ls

The output shows the network ID, name, driver, and scope for each network. Notice that the my-network-1 and my-network-2 networks we created are listed.

To identify unused networks, we can use the docker network prune command with the --dry-run flag. This flag allows us to see which networks would be removed without actually removing them.

docker network prune --dry-run

The output of this command will list the networks that are not attached to any running containers and would be pruned. Since we haven't attached any containers to my-network-1 or my-network-2, they should appear in the output of the dry run.

Let's run a simple container attached to my-network-1 to see how it affects the output of docker network prune --dry-run. We will use the hello-world image, which is very small. First, pull the image:

docker pull hello-world

Now, run a container using this image and attach it to my-network-1. The -d flag runs the container in detached mode (in the background).

docker run -d --network my-network-1 hello-world

You will see a container ID as output.

Now, let's run the docker network prune --dry-run command again.

docker network prune --dry-run

This time, my-network-1 should not appear in the list of networks to be pruned because it is being used by the running hello-world container. my-network-2, which is not being used, should still be listed.

To stop and remove the hello-world container, first find its ID using docker ps.

docker ps

Then, stop the container using docker stop <container_id> and remove it using docker rm <container_id>. Replace <container_id> with the actual ID from the docker ps output.

docker stop <container_id>
docker rm <container_id>

Now, if you run docker network prune --dry-run again, both my-network-1 and my-network-2 should be listed as networks to be pruned, as they are no longer in use.

Prune all unused networks

In this step, we will learn how to remove all unused networks using the docker network prune command. This command is useful for cleaning up your Docker environment and freeing up resources by removing networks that are not currently attached to any containers.

Before pruning, let's list the networks again to see which ones are currently present.

docker network ls

You should see the default networks (bridge, host, none) and the custom networks we created in the first step (my-network-1 and my-network-2). Since we stopped and removed the hello-world container in the previous step, both my-network-1 and my-network-2 should now be unused.

To prune all unused networks, simply run the docker network prune command without any flags. Docker will prompt you to confirm the action.

docker network prune

You will see a message asking for confirmation, like this:

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

Type y and press Enter to proceed with the pruning.

The output will list the networks that have been removed and the total amount of space reclaimed.

Now, let's list the networks again to confirm that my-network-1 and my-network-2 have been removed.

docker network ls

You should now only see the default networks (bridge, host, none) in the list.

Prune unused networks with filter

In this step, we will learn how to prune unused networks using filters. Filters allow you to selectively remove networks based on certain criteria, such as labels or creation time.

First, let's create a couple of new networks with labels so we can demonstrate filtering. We'll create network-a with the label env=dev and network-b with the label env=test.

docker network create --label env=dev network-a
docker network create --label env=test network-b

You should see the network IDs as output for both commands.

Now, let's list the networks to see the newly created ones with their labels.

docker network ls

You should see network-a and network-b in the list.

To prune networks based on a filter, we use the --filter flag with the docker network prune command. For example, to prune all unused networks with the label env=dev, we can use the filter label=env=dev.

Let's perform a dry run first to see which networks would be pruned with this filter.

docker network prune --filter label=env=dev --dry-run

The output should show network-a as the network that would be pruned, as it has the env=dev label and is currently unused.

Now, let's actually prune the unused networks with the env=dev label.

docker network prune --filter label=env=dev

You will be prompted for confirmation. Type y and press Enter.

The output will show that network-a has been removed.

Let's list the networks again to confirm that network-a is gone, but network-b (with the env=test label) is still present.

docker network ls

You should see network-b in the list, but not network-a.

You can also filter based on other criteria, such as networks created before a certain time. However, filtering by labels is a common and useful way to manage your networks.

Summary

In this lab, we learned how to manage Docker networks, specifically focusing on identifying and removing unused ones. We began by creating custom bridge networks using the docker network create command, demonstrating how to add new networks beyond the default ones. We then used the docker network ls command to list all available networks and verify the creation of our custom networks.

Subsequently, we explored how to identify unused networks. We learned that the docker network prune command, particularly with the --dry-run flag, is a valuable tool for previewing which networks are not attached to any containers and are therefore candidates for removal. This step is crucial for understanding the impact of pruning before actually deleting any networks.