0
0
Dockerdevops~5 mins

Connecting containers to multiple networks in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes, a container needs to talk to different groups of containers or services separately. Connecting a container to multiple networks lets it communicate in more than one isolated space, like having different phone lines for different friends.
When you want a container to access both a frontend network and a backend database network separately.
When a container needs to connect to an internal network for secure data and an external network for public access.
When you want to isolate traffic between different parts of your application but still allow some containers to bridge them.
When testing how a container behaves in different network environments without changing its setup.
When you want to connect a container to a monitoring network and an application network at the same time.
Commands
Create the first network called frontend-net where some containers will connect.
Terminal
docker network create frontend-net
Expected OutputExpected
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6
Create the second network called backend-net for other containers to connect.
Terminal
docker network create backend-net
Expected OutputExpected
z9y8x7w6v5u4t3s2r1q0p9o8n7m6l5k4j3i2h1g0f9e8d7c6b5a4
Start a container named my-app connected to the frontend-net network.
Terminal
docker run -d --name my-app --network frontend-net nginx
Expected OutputExpected
3f4e5d6c7b8a9f0e1d2c3b4a5e6f7d8c9b0a1e2f3d4c5b6a7e8f9d0c1b2a3
-d - Run container in background (detached mode)
--name - Assign a name to the container
--network - Connect container to a specific network on start
Connect the running my-app container to the backend-net network as well.
Terminal
docker network connect backend-net my-app
Expected OutputExpected
No output (command runs silently)
Check details of frontend-net to see which containers are connected.
Terminal
docker network inspect frontend-net
Expected OutputExpected
[ { "Name": "frontend-net", "Id": "a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6", "Containers": { "3f4e5d6c7b8a9f0e1d2c3b4a5e6f7d8c9b0a1e2f3d4c5b6a7e8f9d0c1b2a3": { "Name": "my-app", "IPv4Address": "172.18.0.2/16" } } } ]
Check details of backend-net to confirm my-app is connected there too.
Terminal
docker network inspect backend-net
Expected OutputExpected
[ { "Name": "backend-net", "Id": "z9y8x7w6v5u4t3s2r1q0p9o8n7m6l5k4j3i2h1g0f9e8d7c6b5a4", "Containers": { "3f4e5d6c7b8a9f0e1d2c3b4a5e6f7d8c9b0a1e2f3d4c5b6a7e8f9d0c1b2a3": { "Name": "my-app", "IPv4Address": "172.19.0.2/16" } } } ]
Key Concept

A container can join multiple networks to communicate separately in each network space.

Common Mistakes
Trying to connect a container to multiple networks only at start with --network flag.
The --network flag only connects to one network at container start; others must be added later.
Start container with one network, then use 'docker network connect' to add more networks.
Assuming container IP addresses are the same across all networks.
Each network assigns its own IP; container has different IPs per network.
Inspect each network separately to see the container's IP address in that network.
Not creating the networks before connecting containers to them.
Docker cannot connect containers to networks that do not exist.
Always create networks first using 'docker network create' before connecting containers.
Summary
Create multiple Docker networks to isolate container communication.
Start a container connected to one network using --network flag.
Use 'docker network connect' to add the container to additional networks.
Inspect networks to verify container connections and IP addresses.