In the last episode, we explored Docker’s built-in networking modes: Bridge, Host, and Overlay. Now, let’s go one step further — creating custom networks to give containers more flexibility, better isolation, and cleaner communication. This is where Docker networking becomes truly powerful in real-world projects.
🔹 Why Custom Networks?
By default, containers in the same bridge network can talk to each other via IP addresses. But in larger setups, we want:
- Service discovery (containers find each other by name).
- Network isolation (only specific containers can talk).
- Flexibility (connect/disconnect containers dynamically).
This is where custom networks shine.
🔹 Creating a Custom Network
docker network create my_custom_network
List available networks:
docker network ls
🔹 Running Containers in Custom Networks
docker run -dit --name app1 --network my_custom_network alpine sh
docker run -dit --name app2 --network my_custom_network alpine sh
Now, app1
and app2
can talk to each other using container names, not just IPs:
docker exec -it app1 ping app2
🔹 Connecting Containers to Multiple Networks
You can attach a container to more than one network:
docker network connect my_custom_network app1
docker network connect bridge app1
This is useful when bridging isolated services.
🔹 Real-World Use Case
Imagine you’re running:
- A database container (private network only).
- An API container (can access DB + external users).
- A frontend container (can access API but not DB).
By designing with custom networks, you can enforce this security boundary with ease.
🔹 Hands-On Exercise
- Create two networks:
backend_net
andfrontend_net
. - Run a database container inside
backend_net
. - Run an API container in both networks.
- Run a frontend container only in
frontend_net
. - Test communication boundaries.
✅ By now, you know how to design custom networks for real-world Docker applications. This sets the stage for building secure, scalable apps with Docker.
Top comments (0)