Need your frontend project to talk to a backend in another Compose project? External networks let you connect containers across different stacks.
The problem
Two separate Compose projects need to communicate:
frontend/compose.yml- React appbackend/compose.yml- API service
By default, each creates its own isolated network.
The solution
Create a shared external network:
# Create the network once
docker network create shared-network
Then reference it in both projects:
backend/compose.yml:
services:
api:
image: myapi:latest
networks:
- shared # Connect to external network
- default # Keep internal network too
networks:
shared:
external: true
name: shared-network
frontend/compose.yml:
services:
web:
image: myfrontend:latest
environment:
API_URL: http://api:8080 # Use service name!
networks:
- shared
networks:
shared:
external: true
name: shared-network
Now web can reach api by name across projects!
Real microservices example
# shared/compose.yml
services:
postgres:
image: postgres:15
networks: [backbone]
redis:
image: redis:7-alpine
networks: [backbone]
networks:
backbone:
external: true
name: company-backbone
---
# api/compose.yml
services:
api:
image: company/api:latest
networks:
- backbone # Access shared services
- internal # Private network
environment:
DATABASE_URL: postgres://postgres:5432/api
REDIS_URL: redis://redis:6379
networks:
backbone:
external: true
name: company-backbone
internal: {} # Project-specific
Service discovery
Services on the same external network can reach each other by name:
# From frontend container
docker compose -f frontend/compose.yml exec web sh
$ curl http://api:8080/health # Works!
Hybrid networking
Keep sensitive services isolated:
services:
public-api:
networks:
- shared # External access
- internal # Internal only
database:
networks:
- internal # Not on shared network!
networks:
shared:
external: true
name: shared-network
internal:
# Project-specific, isolated
Troubleshooting
# "Network not found" error? Create it first:
docker network create shared-network
# Can't connect? Verify both services are on same network:
docker network inspect shared-network
Pro tip
Create the external network first, or your stack won’t start:
# Always create before using
docker network create shared-network
docker compose up -d