Multi-stack external and internal networks in Docker Compose
Connect separate Docker Compose stacks by service name with external networks, isolate databases with internal: true, and avoid the default-network drop trap.
Splitting a reverse proxy and backend API into separate
compose.yamlfiles isolates their default bridges, breaking service-name DNS and direct container traffic. This post walks through connecting stacks withexternal: true, blocking database outbound traffic withinternal: true, and avoiding the default-network drop trap.
All commands and outputs below were run locally on Docker Engine
29.9.3and Docker Composev5.7.1using the default single-hostbridgedriver.
Separate Compose projects isolate networks by default
When every service lives in a single compose.yaml—like a local Spring Boot, PostgreSQL, and Redis Compose setup—containers reach each other by service name out of the box. Compose creates one <project>_default bridge network and attaches every service to it automatically.
That convenience disappears as soon as you split a reverse proxy (stack-edge) and an application stack (stack-api) into separate directories or repositories. Each project creates its own default bridge, so containers on the same host can’t resolve or reach each other.
To verify this baseline, I started two minimal stacks in separate folders with no custom network configuration:
1
2
3
4
5
6
# stack-edge/compose.yaml
services:
edge:
image: nginx:1.27-alpine
ports:
- "18610:80"
1
2
3
4
# stack-api/compose.yaml
services:
api:
image: nginx:1.27-alpine
Listing the networks and querying api from edge shows two layers of isolation:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ docker network ls --filter "name=stack-" --format "table {{.Name}}\t{{.Driver}}\t{{.Scope}}"
NAME DRIVER SCOPE
stack-api_default bridge local
stack-edge_default bridge local
$ docker compose -f stack-edge/compose.yaml exec -T edge nslookup api
Server: 127.0.0.11
Address: 127.0.0.11:53
Non-authoritative answer:
** server can't find api: NXDOMAIN
# exit code: 1
$ docker compose -f stack-edge/compose.yaml exec -T edge curl -sS -o /dev/null -w '%{http_code}\n' http://api
curl: (6) Could not resolve host: api
000
# exit code: 6
$ docker compose -f stack-edge/compose.yaml exec -T edge curl -sS --connect-timeout 2 -o /dev/null -w '%{http_code}\n' http://172.26.0.2
000
curl: (28) Connection timed out after 2008 milliseconds
# exit code: 28
Two rules are at work here:
- Per-network DNS scope: Docker’s embedded DNS (
127.0.0.11) resolvesedgeinsidestack-edge_default, but returnsNXDOMAINforapibecause it lives onstack-api_default. - Bridge-to-bridge isolation: Even calling
api’s container IP (172.26.0.2) directly times out after 2 seconds (exit28). Docker isolates traffic between separate user-defined bridges by default. For the underlying bridge and127.0.0.11mechanics, see my note on Docker bridge networking and DNS service discovery.
Connecting stacks with external: true and aliases
To let independent Compose stacks talk by service name without merging their files, attach both stacks to a shared network marked external: true.
The first failure you’ll hit is running docker compose up -d before creating that network:
1
2
3
$ docker compose -f stack-edge/compose.yaml up -d
network shared-edge-net declared as external, but could not be found
# exit code: 1
Declaring external: true tells Compose that this network’s lifecycle is managed outside the project. Create it once beforehand:
1
docker network create shared-edge-net
Then reference shared-edge-net in both files. When multiple stacks share one bridge, adding network aliases prevents service-name collisions (such as two stacks both naming a service api) and gives callers a clear domain name:
1
2
3
4
5
6
7
8
9
10
11
12
# stack-edge/compose.yaml
services:
edge:
image: nginx:1.27-alpine
ports:
- "18610:80"
networks:
- shared-edge-net
networks:
shared-edge-net:
external: true
1
2
3
4
5
6
7
8
9
10
11
12
# stack-api/compose.yaml
services:
api:
image: nginx:1.27-alpine
networks:
shared-edge-net:
aliases:
- orders-api
networks:
shared-edge-net:
external: true
After restarting both stacks, edge resolves both api and orders-api to 172.22.0.3 and gets an HTTP/1.1 200 OK response:
1
2
3
4
5
6
7
8
9
10
11
12
$ docker compose -f stack-edge/compose.yaml exec -T edge getent hosts api
172.22.0.3 api api
$ docker compose -f stack-edge/compose.yaml exec -T edge getent hosts orders-api
172.22.0.3 orders-api orders-api
$ docker compose -f stack-edge/compose.yaml exec -T edge curl -sS -I http://api | head -n 5
HTTP/1.1 200 OK
Server: nginx/1.27.5
Date: Sat, 05 Dec 2026 05:45:57 GMT
Content-Type: text/html
Content-Length: 615
Inspecting shared-edge-net confirms that both containers sit on the same 172.22.0.0/16 subnet:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
{
"Name": "shared-edge-net",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Config": [
{
"Subnet": "172.22.0.0/16",
"Gateway": "172.22.0.1"
}
]
},
"Internal": false,
"Containers": {
"49382da9b947": {
"Name": "stack-api-api-1",
"IPv4Address": "172.22.0.3/16"
},
"6010c2531b77": {
"Name": "stack-edge-edge-1",
"IPv4Address": "172.22.0.2/16"
}
}
}
Running
docker compose down -vremoves project-owned networks, but leavesshared-edge-netuntouched. Because it’s markedexternal: true, you have to remove it explicitly withdocker network rm shared-edge-netafter stopping all attached stacks.
Isolating databases with internal: true
A common mistake with shared proxy networks is attaching databases to the same bridge. The edge proxy only needs to reach api, never db. You also want to prevent a database container from initiating outbound connections to the internet if it’s ever compromised.
Setting internal: true solves both. Attach api to both shared-edge-net and backend-isolated-net, and keep db exclusively on backend-isolated-net:
flowchart TD
subgraph Shared["shared-edge-net (external)"]
Edge["edge (stack-edge)"]
Api["api / orders-api"]
end
subgraph Isolated["backend-isolated-net (internal)"]
Db["db (Redis 7)"]
end
Ext["Internet (1.1.1.1)"]
Edge -->|"HTTP 200"| Api
Api -->|"TCP 6379"| Db
Api -->|"default route"| Ext
Db -.->|"no default route"| Ext
Update stack-api/compose.yaml to add the isolated network and a Redis container:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# stack-api/compose.yaml
services:
api:
image: nginx:1.27-alpine
networks:
shared-edge-net:
aliases:
- orders-api
backend-isolated-net:
db:
image: redis:7-alpine
networks:
- backend-isolated-net
networks:
shared-edge-net:
external: true
backend-isolated-net:
internal: true
Once applied, api reaches db:6379 while edge fails on both DNS lookup and direct IP connection:
1
2
3
4
5
6
7
8
9
10
11
$ docker compose -f stack-api/compose.yaml exec -T api nc -zv db 6379
db (172.26.0.2:6379) open
# exit code: 0
$ docker compose -f stack-edge/compose.yaml exec -T edge nc -zv -w 2 db 6379
nc: bad address 'db'
# exit code: 1
$ docker compose -f stack-edge/compose.yaml exec -T edge nc -zv -w 2 172.26.0.2 6379
nc: 172.26.0.2 (172.26.0.2:6379): Operation timed out
# exit code: 1
Looking one layer deeper reveals how internal: true actually enforces outbound blocking. Compare docker network inspect with ip route inside each container:
1
2
3
4
5
6
7
8
9
10
$ docker network inspect stack-api_backend-isolated-net --format 'Name={{.Name}} Internal={{.Internal}} Subnet={{(index .IPAM.Config 0).Subnet}} Gateway={{(index .IPAM.Config 0).Gateway}}'
Name=stack-api_backend-isolated-net Internal=true Subnet=172.26.0.0/16 Gateway=172.26.0.1
$ docker compose -f stack-api/compose.yaml exec -T db ip route
172.26.0.0/16 dev eth0 scope link src 172.26.0.2
$ docker compose -f stack-api/compose.yaml exec -T api ip route
default via 172.22.0.1 dev eth0
172.22.0.0/16 dev eth0 scope link src 172.22.0.3
172.26.0.0/16 dev eth1 scope link src 172.26.0.3
IPAM still displays Gateway=172.26.0.1 in docker network inspect, which can be misleading if you only read inspect output. Inside db, however, Docker injects no default via route at all—only the local link route for 172.26.0.0/16. Meanwhile, api gets default via 172.22.0.1 dev eth0 from shared-edge-net.
Testing outbound connections confirms the difference immediately:
1
2
3
4
5
6
7
8
9
10
11
$ docker compose -f stack-api/compose.yaml exec -T db nc -zv -w 3 1.1.1.1 80
nc: 1.1.1.1 (1.1.1.1:80): Network unreachable
# exit code: 1
$ docker compose -f stack-api/compose.yaml exec -T db wget -T 3 -qO- https://example.com
wget: bad address 'example.com'
# exit code: 1
$ docker compose -f stack-api/compose.yaml exec -T api nc -zv -w 3 1.1.1.1 80
1.1.1.1 (1.1.1.1:80) open
# exit code: 0
Attaching a container to both an
internal: truenetwork and a regular network restores outbound internet access through the regular interface—just asapireached1.1.1.1:80above. Keep databases and caches attached solely tointernal: truenetworks, and don’t publish hostports:unless strictly required. See my post on Docker port binding and EXPOSE for how published ports interact with host firewalls.
The networks: trap that drops default
Once you start declaring custom networks, a subtle Compose rule often breaks services inside the same file: as soon as you add a networks: block to a service, Compose stops attaching that service to the project’s default network.
To see this in action, add a worker service to stack-api/compose.yaml without a networks: key:
1
2
3
4
# stack-api/compose.yaml (excerpt)
worker:
image: alpine:3.21
command: ["sleep", "infinity"]
Inspecting network membership explains why worker can’t resolve api:
1
2
3
4
5
6
7
8
9
10
11
12
13
$ docker inspect stack-api-api-1 stack-api-db-1 stack-api-worker-1 --format '{{.Name}} -> {{range $k, $v := .NetworkSettings.Networks}}{{$k}} ({{$v.IPAddress}}) {{end}}'
/stack-api-api-1 -> shared-edge-net (172.22.0.3) stack-api_backend-isolated-net (172.26.0.3)
/stack-api-db-1 -> stack-api_backend-isolated-net (172.26.0.2)
/stack-api-worker-1 -> stack-api_default (172.28.0.2)
$ docker compose -f stack-api/compose.yaml exec -T worker nslookup api
Server: 127.0.0.11
Address: 127.0.0.11:53
Non-authoritative answer:
** server can't find api: NXDOMAIN
# exit code: 1
Because worker omitted networks:, it landed on stack-api_default. Because api declared custom networks, Compose dropped it from stack-api_default.
Either move worker onto an explicit network, or add default: back to api.networks:
1
2
3
4
5
6
7
8
9
# stack-api/compose.yaml (updated api service)
api:
image: nginx:1.27-alpine
networks:
default:
shared-edge-net:
aliases:
- orders-api
backend-isolated-net:
After re-running docker compose up -d, api joins stack-api_default (172.28.0.3) alongside its other two networks, and worker connects cleanly:
1
2
3
4
5
6
7
8
$ docker inspect stack-api-api-1 stack-api-db-1 stack-api-worker-1 --format '{{.Name}} -> {{range $k, $v := .NetworkSettings.Networks}}{{$k}} ({{$v.IPAddress}}) {{end}}'
/stack-api-api-1 -> shared-edge-net (172.22.0.3) stack-api_backend-isolated-net (172.26.0.3) stack-api_default (172.28.0.3)
/stack-api-db-1 -> stack-api_backend-isolated-net (172.26.0.2)
/stack-api-worker-1 -> stack-api_default (172.28.0.2)
$ docker compose -f stack-api/compose.yaml exec -T worker nc -zv api 80
api (172.28.0.3:80) open
# exit code: 0
Choosing the right network pattern and limits
Match the network type to the trust boundary rather than putting every container on one shared bridge:
| Network pattern | Best fit | DNS & routing behavior | Lifecycle |
|---|---|---|---|
Project default | Single-stack local dev and simple internal services | Auto-attaches services without a networks: key | Removed on docker compose down |
| Custom bridge | Tier separation inside one compose.yaml | Explicit opt-in per service; outbound internet allowed | Removed on docker compose down |
external: true | Shared reverse proxy and multi-stack communication | Resolves service names and aliases across projects | Must be pre-created; survives down |
internal: true | Databases, caches, and private background workers | No default via route; outbound internet blocked | Removed on docker compose down |
When not to rely on Compose bridge isolation
This pattern assumes a single-host Docker Engine deployment and has clear boundaries:
- Multi-node deployments: Local
bridgenetworks don’t span hosts. Use Docker Swarmoverlaynetworks or Kubernetes CNI andNetworkPolicywhen workloads run across multiple machines. - Fine-grained L4/L7 access control: Bridge segmentation only controls which containers share a virtual network. If you need port-level or HTTP-path allowlists between services on the same network, enforce them at a proxy, firewall, or Kubernetes
NetworkPolicy. - Containers that need outbound access: Don’t place a container solely on an
internal: truenetwork if it needs to call external webhooks, fetch OAuth keys, or pull packages at runtime—external DNS and routing will fail.
Production checklist
Before deploying a multi-stack Compose layout, check these four items:
- Pre-create external networks in provisioning: Ensure
docker network createruns beforedocker compose up -din your bootstrap or CI/CD script. - Assign domain-specific
aliases: Avoid collisions on shared proxy networks when multiple stacks define generic service names likeapiorweb. - Verify
internal: trueisolation withip route: Confirm that databases have nodefault viaroute, aren’t attached to a secondary non-internal network, and don’t publish unnecessary hostports:. - Audit
defaultnetwork membership: Whenever you addnetworks:to a service in an existing stack, check whether sibling services (workers, sidecars) still rely on the project’sdefaultnetwork.