π How to Bridge Networks in Docker Compose (docker-compose.yml)
Docker Compose makes it super easy to define and run multi-container applications. But when it comes to networking, things can get a bit confusing β especially if you want your containers to talk across different custom networks.
In this guide, we'll dive into bridging networks in Docker Compose β what it means, how to do it, and some real-world tips.
π§ What Does "Bridging Networks" Mean?
In Docker, each container is attached to a network. By default, Docker Compose creates a network for each Compose project. Sometimes, though, you want containers to talk across different networks β maybe even across different Compose files or services in isolated environments.
Bridging networks means:
- Creating multiple custom networks
- Attaching a container to more than one network
- Allowing selective cross-communication between services
ποΈ Example: Bridge Two Services Across Networks
Letβs say you have a web service and a db service, and you want to isolate them in different networks but still allow controlled communication.
π docker-compose.yml
version: '3.9'
services:
web:
image: nginx
networks:
- frontnet
- backnet # π This allows it to talk to the db
db:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: example
networks:
- backnet
networks:
frontnet:
driver: bridge
backnet:
driver: bridge
π§ͺ What Just Happened?
-
webis attached to bothfrontnetandbacknet -
dbis attached only tobacknet - This allows the
webcontainer to:- Communicate with other front-facing containers via
frontnet - Access the database via
backnet
- Communicate with other front-facing containers via
- The
dbcontainer cannot reach services onfrontnet, which is great for security.
π Use Case: Multi-Compose Setup
Letβs say you have multiple Docker Compose files:
frontend/docker-compose.ymlbackend/docker-compose.yml
You want the frontend to talk to the backend over a shared network.
Step 1: Create a shared network manually
docker network create sharednet
Step 2: In both Compose files, use external: true
networks:
sharednet:
external: true
Then attach services to sharednet in both Compose files:
services:
frontend-app:
networks:
- sharednet
services:
backend-api:
networks:
- sharednet
Now both services, managed separately, can talk to each other over the shared network.
π Security Tips
- Use multiple networks to segment services (e.g., frontend/backend/db).
- Avoid exposing sensitive services (like
dborredis) to the public or shared networks. - Use aliases to control how services are addressed.
networks:
backnet:
aliases:
- internal-db
π Debugging Tips
- Use
docker network inspect <network_name>to see what's connected. -
docker exec -it <container> shinto containers topingorcurltest others. - Try using hostnames instead of IPs (e.g.,
http://db:3306).
π§° Tools & Tricks
-
netshoot: A Docker image with networking tools.
docker run -it --rm --network=backnet nicolaka/netshoot
-
dig,nslookup,tracerouteandip awill become your best friends.
π Summary
β You can bridge Docker networks in Compose by:
- Attaching services to multiple networks
- Creating shared external networks
- Managing cross-service communication precisely
π€ TL;DR
| Concept | What it Does |
|---|---|
networks: |
Defines custom Docker networks |
external: true |
Use an existing network created outside Compose |
| Multi-networking | Allows containers to act as bridges across layers |
| Network segmentation | Helps isolate services for security |
βWith great networks comes great responsibility.β
β Uncle Compose π§π³
Now go forth and connect your containers like a pro!




Top comments (1)
Compose networking is more nuanced than most teams realize. Three production patterns:
Bridge network is per-Compose by default β each docker-compose up creates its own bridge, so containers from different Compose files on the same host cannot talk to each other without external_network: + network_mode: host. We standardize on one shared 'edge' network for cross-project communication and let inner services stay private.
Bind mount permissions are platform-specific β Linux: uid:gid in the path. macOS/Windows: gRPC-FUSE caches the bind mount, but writes from inside the container do not propagate back instantly. For dev workflows where you need live reload, run docker-compose watch or use Mutagen (Synology/Compose+.macOS).
host network mode for storage daemons β if you run MinIO, Postgres, or any service that needs raw network throughput, host mode is the only way to escape the bridge NAT overhead. We use it sparingly because it bypasses DNS, but for storage-heavy containers it's worth it.
Mount layer for ops browsing container volumes from Windows: ScsDriver