Communication Between Containers in a Custom Bridge Network
When working with Docker, containers by default are isolated. However, when containers need to communicate with each other, you can connect them to the same Docker network. A user-defined bridge network provides more control over how Docker containers communicate compared to the default bridge network. This guide will walk you through setting up a custom bridge network, launching multiple Nginx containers on that network, and verifying communication between them.
Why Use a User-Defined Bridge Network?
By default, Docker containers can communicate over a built-in network called the "default bridge network." However, using a user-defined bridge network provides the following benefits:
Name resolution: Containers connected to the same network can communicate by their container names, making it easier to manage multi-container setups.
Isolated environment: Containers on a user-defined network are isolated from others unless explicitly connected to other networks.
Security: You can control which containers can communicate by connecting them only to specific networks.
Now, let's move on to creating the network and launching our containers.
Creating the User-Defined Bridge Network
The first step is to create a custom bridge network. Docker allows you to create networks of different types, such as bridge, overlay, and host. Here, we'll use the bridge driver, which is the default type for local container communication on a single host.
Run the following command to create the network:
docker network create --driver bridge my-bridge-network
This command creates a bridge network named my-bridge-network. You can inspect the network details using the command below:
docker network inspect my-bridge-network
This will give you detailed information about the network, including its subnet, gateway, and connected containers.
Verifying Network Creation
You can list all existing Docker networks by running:
docker network ls
Expected Output:
This will run the Nginx web server in the background (-d) and assign the name container1 to the instance.
Launching Container 2
Similarly, to launch container2, use:
docker run -d --name container2 --network=my-bridge-network nginx
Launching Container 3
Finally, to launch container3, use:
docker run -d --name container3 --network=my-bridge-network nginx
With all three containers running, they are now connected to the same network, my-bridge-network. This enables them to communicate directly with one another.
Verifying Container Status
To check the status of the running containers, use the command:
docker ps
Expected output:
Pinging Container 3 from Container 1
Next, try pinging container3 from container1:
ping container3 -c 5
Expected Output:
Pinging Container 3 from Container 2
ping container3 -c 5
Expected Output:
These tests confirm that all the containers can communicate with each other over the custom bridge network.
Conclusion
By following these steps, we successfully created a user-defined bridge network and launched multiple Nginx containers connected to the network. We verified that they can communicate with each other by pinging container names. This demonstrates how Docker networking facilitates smooth communication between containerized applications, making it easier to manage interconnected services.

SOCIAL SHARE CARD GENERATOR