How to use docker network rm command to remove networks

DockerDockerBeginner
Practice Now

Introduction

In this lab, you will learn how to manage Docker networks using the docker network rm command. You will begin by creating a custom network to understand the process of network creation.

Following network creation, you will practice removing a single network by its name. The lab will then guide you through creating multiple networks and subsequently removing them using both their names and IDs, demonstrating different methods for network cleanup.


Skills Graph

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

Create a custom network

In this step, you will learn how to create a custom network in Docker. By default, Docker provides several network drivers, but creating a custom network allows you to isolate containers and enable communication between specific containers.

To create a custom network, you use the docker network create command followed by the network driver and the network name. The most common driver for custom networks is bridge.

Let's create a custom bridge network named my-custom-network.

docker network create bridge my-custom-network

After executing the command, Docker will create the network and output its ID.

You can verify that the network was created successfully by listing the available networks using the docker network ls command.

docker network ls

You should see my-custom-network listed among the networks.

Remove a single network by name

In this step, you will learn how to remove a single Docker network using its name. This is useful for cleaning up networks that are no longer needed.

To remove a network, you use the docker network rm command followed by the network name.

Let's remove the my-custom-network that we created in the previous step.

docker network rm my-custom-network

If the network is successfully removed, Docker will output the network ID that was removed.

You can verify that the network has been removed by listing the available networks again using docker network ls.

docker network ls

The my-custom-network should no longer appear in the list.

Create multiple networks

In this step, you will learn how to create multiple Docker networks. This is useful when you need to isolate different groups of containers or set up complex network topologies.

You can create multiple networks by running the docker network create command multiple times with different network names.

Let's create two new bridge networks named network1 and network2.

docker network create bridge network1
docker network create bridge network2

After executing these commands, Docker will create both networks.

You can verify that both networks were created successfully by listing the available networks using the docker network ls command.

docker network ls

You should see both network1 and network2 listed among the networks.

Remove multiple networks by name and ID

In this step, you will learn how to remove multiple Docker networks using a single command, specifying them by both name and ID. This is an efficient way to clean up several networks at once.

You can remove multiple networks by listing their names or IDs after the docker network rm command, separated by spaces.

First, let's list the networks to get their IDs.

docker network ls

You should see network1 and network2 from the previous step. Note down the Network ID for network2.

Now, let's remove network1 by its name and network2 by its ID in a single command. Replace <network2_id> with the actual ID you noted down.

docker network rm network1 <network2_id>

If the networks are successfully removed, Docker will output the IDs of the networks that were removed.

You can verify that both networks have been removed by listing the available networks again using docker network ls.

docker network ls

Neither network1 nor network2 should appear in the list.

Summary

In this lab, you learned how to manage Docker networks using the docker network command. You began by creating a custom bridge network using docker network create and verified its creation with docker network ls.

Subsequently, you practiced removing a single network by its name using docker network rm. You then created multiple networks and learned how to remove them simultaneously by specifying their names and IDs in the docker network rm command. This hands-on experience demonstrated the fundamental operations for creating and removing Docker networks, essential for managing container isolation and communication.