How to remove an unused Docker network

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful containerization platform that allows you to build, deploy, and manage applications in a consistent and scalable environment. As you work with Docker, it's important to maintain a clean and efficient network configuration. This tutorial will guide you through the process of identifying and removing unused Docker networks, helping you optimize your Docker environment.


Skills Graph

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

Understanding Docker Networks

Docker networks are a fundamental concept in the Docker ecosystem. They provide a way to connect containers and enable communication between them. Docker supports several network drivers, each with its own set of features and use cases.

The default network driver in Docker is the bridge driver, which creates a virtual bridge on the host machine and attaches containers to it. This allows containers to communicate with each other and the host system. Other network drivers include overlay, host, and macvlan, each with its own advantages and use cases.

graph LR A[Host Machine] --> B[Docker Bridge Network] B --> C[Container 1] B --> D[Container 2] B --> E[Container 3]

The overlay network driver is used for multi-host networking, allowing containers on different Docker hosts to communicate with each other. The host network driver removes the network isolation between the container and the host, while the macvlan driver allows containers to be assigned a MAC address, making them appear as physical devices on the network.

Understanding the different network drivers and their use cases is crucial when working with Docker, as it allows you to choose the most appropriate network configuration for your application.

Identifying and Listing Unused Docker Networks

To identify and list unused Docker networks, you can use the following Docker CLI commands:

Listing All Docker Networks

To list all the Docker networks on your system, you can use the docker network ls command:

docker network ls

This will output a table with the following columns:

  • NETWORK ID
  • NAME
  • DRIVER
  • SCOPE

Identifying Unused Docker Networks

To identify unused Docker networks, you can use the docker network ls command with the -f (filter) option:

docker network ls -f "dangling=true"

This will list all the Docker networks that are not being used by any containers. The dangling=true filter ensures that only the networks without any connected containers are displayed.

Alternatively, you can use the following command to list all the networks that are not being used by any running containers:

docker network ls --filter "scope=local" --filter "driver=bridge" --filter "name=^((?!host|none|bridge).)*$" --format "{{.ID}}\t{{.Name}}\t{{.Driver}}"

This command filters the network list to show only the local, bridge-based networks that are not the default host, none, or bridge networks.

The output of this command will be a table with the following columns:

  • NETWORK ID
  • NAME
  • DRIVER

By using these commands, you can easily identify and list the unused Docker networks on your system, which can be useful for cleaning up your Docker environment.

Removing Unused Docker Networks

After identifying the unused Docker networks, you can remove them using the docker network rm command.

Removing a Single Unused Network

To remove a single unused Docker network, you can use the following command:

docker network rm <network_name>

Replace <network_name> with the name of the network you want to remove.

Removing Multiple Unused Networks

If you have multiple unused Docker networks, you can remove them in a single command by using the docker network ls command with the -q (quiet) option to get the network IDs, and then passing them to the docker network rm command:

docker network ls -f "dangling=true" -q | xargs docker network rm

This command first lists all the unused (dangling) Docker networks using the docker network ls command with the -f "dangling=true" filter. The -q option is used to output only the network IDs. These IDs are then passed to the docker network rm command using the xargs utility, which removes all the listed networks.

Removing All Unused Networks

If you want to remove all the unused Docker networks on your system, you can use the following command:

docker network prune

This command will remove all the networks that are not being used by any containers.

By using these commands, you can easily remove the unused Docker networks on your system, which can help keep your Docker environment clean and organized.

Summary

In this tutorial, you have learned how to effectively manage your Docker networks by identifying and removing unused networks. By following the steps outlined, you can ensure that your Docker environment remains organized and efficient, leading to improved performance and reduced resource consumption. Mastering Docker network management is a crucial skill for any Docker user or administrator.

Other Docker Tutorials you may like