Networks
Every Compose project gets a private network for free: <project>_default. All services join it, find each other by name, and nothing outside the project can reach them unless you publish a port. For many apps that's all you need. This lesson is about the cases where it isn't: separating a public tier from a database tier, cutting a service off from the internet, joining a network another project owns, or using the host's network directly.
If bridge networks are new to you, read Docker Networks first. How names resolve on these networks is the topic of the next lesson, Service Discovery & DNS.
The default network
services:
web:
image: nginx:1.29
ports:
- "8080:80"
api:
image: ghcr.io/acme/api:3.0name: demo
services:
api:
image: ghcr.io/acme/api:3.0
networks:
default: null
web:
image: nginx:1.29
networks:
default: null
ports:
- mode: ingress
target: 80
published: "8080"
protocol: tcp
networks:
default:
name: demo_default
Each service got networks: default: null ("join the network called default, no special options") and the top-level default network is named demo_default. The ports entry was also normalised: published is the host port, target the container port.
Several networks: tiers
A typical three-tier layout puts the reverse proxy and the API on a frontend network, and the API and the database on a backend network. The proxy can't even see the database:
host :80
│
┌──────▼──────┐ frontend ┌──────────┐ backend ┌──────────┐
│ proxy │──────────────▶│ api │─────────────▶│ db │
└─────────────┘ └──────────┘ └──────────┘
(on both) (internal:
no internet)# $ docker compose config --networks
# $ docker compose config
services:
proxy:
image: nginx:1.29
ports: ["80:80"]
networks: [frontend]
api:
image: ghcr.io/acme/api:3.0
networks:
frontend:
backend:
aliases: [api.internal]
ipv4_address: 172.28.0.10
db:
image: postgres:17
networks: [backend]
networks:
frontend:
backend:
internal: true
ipam:
config:
- subnet: 172.28.0.0/16$ docker compose config --networks
backend
frontend
$ docker compose config
name: demo
services:
api:
image: ghcr.io/acme/api:3.0
networks:
backend:
aliases:
- api.internal
ipv4_address: 172.28.0.10
frontend: null
db:
image: postgres:17
networks:
backend: null
proxy:
image: nginx:1.29
networks:
frontend: null
ports:
- mode: ingress
target: 80
published: "80"
protocol: tcp
networks:
backend:
name: demo_backend
ipam:
config:
- subnet: 172.28.0.0/16
internal: true
frontend:
name: demo_frontend
Things to notice:
- There's no
defaultnetwork any more. As soon as every service lists its ownnetworks, nobody usesdefault, so Compose doesn't create it. internal: truecreates a network with no route to the outside world. The database can talk to the API, but can't download anything or phone home, which is a cheap and effective security layer.ipamsets the subnet;ipv4_addresspins an address. You rarely need fixed IPs (use names instead), but some legacy software insists.aliasesadds extra DNS names on that one network: onbackend, the API is reachable asapiandapi.internal.
Which services can reach which? Two containers can talk if they share at least one network:
networks = {
"proxy": {"frontend"},
"api": {"frontend", "backend"},
"db": {"backend"},
}
internal = {"backend"}
def can_talk(a, b):
shared = networks[a] & networks[b]
return ", ".join(sorted(shared)) if shared else "-"
names = list(networks)
print(f"{'':<6}" + "".join(f"{n:<20}" for n in names))
for a in names:
print(f"{a:<6}" + "".join(f"{can_talk(a, b) if a != b else '(self)':<20}" for b in names))
print()
for svc, nets in networks.items():
outside = any(n not in internal for n in nets)
print(f"{svc:<6} can reach the internet: {'yes' if outside else 'no'}") proxy api db
proxy (self) frontend -
api frontend (self) backend
db - backend (self)
proxy can reach the internet: yes
api can reach the internet: yes
db can reach the internet: no
The API can still reach the internet because it is also on frontend, a normal network. Only a container whose every network is internal is fully cut off.
Mistakes Compose catches
A typo in a network name is caught before anything starts:
services:
api:
image: ghcr.io/acme/api:3.0
networks: [backend]service "api" refers to undefined network backend: invalid compose project
And network_mode (which replaces Compose networking entirely) can't be combined with networks:
services:
exporter:
image: prom/node-exporter:v1.9.1
network_mode: host
networks: [default]service exporter declares mutually exclusive `network_mode` and `networks`: invalid compose project
network_mode
network_mode takes the container out of Compose's networks:
| Value | Effect |
|---|---|
host |
share the host's network stack: no isolation, no port mapping needed (Linux only) |
none |
only a loopback interface: for batch jobs that must not touch the network |
service:api |
share the network namespace of another service's container (sidecar pattern: same IP, same localhost) |
container:<name> |
same, with a container outside the project |
network_mode: host is popular for monitoring agents that must see host traffic. A service in host mode can't be reached by name from other services: it isn't on their network.
Joining another project's network
Two projects (say a shared traefik reverse proxy and your app) can share a network that one of them, or you by hand, created. The other marks it external:
services:
web:
image: ghcr.io/acme/web:1.8
networks: [default, proxy]
networks:
proxy:
external: true
name: traefik_publicname: demo
services:
web:
image: ghcr.io/acme/web:1.8
networks:
default: null
proxy: null
networks:
default:
name: demo_default
proxy:
name: traefik_public
external: true
docker compose up fails with network traefik_public declared as external, but could not be found if the network doesn't exist. docker compose down never removes it.
Driver options
The default driver is bridge. You can tune it, or pick another one:
networks:
backend:
driver: bridge
driver_opts:
com.docker.network.bridge.name: br-backend # a readable interface name on the host
com.docker.network.driver.mtu: "1400" # e.g. inside a VPN or some clouds
enable_ipv6: true
labels:
com.acme.tier: data
cluster:
driver: overlay # multi-host, needs Swarm mode
attachable: trueOverlay networks span several hosts and belong to Swarm: see Overlay Networks. In Kubernetes there are no per-app networks at all; every Pod can reach every other Pod unless Network Policies say otherwise.
Learn more: Service Discovery & DNS · Docker Networks · Compose Security
Test yourself
Every service in a file lists networks: [frontend] or [backend]. Does Compose create demo_default?
- Yes, always
- No, because no service uses the default network
- Only if ports are published
- Only with --profile default
What does internal: true on a network do?
- Hides it from docker network ls
- Containers on it get no route to the outside world
- Makes it reachable only from the host
- Encrypts its traffic
What does network_mode: "service:api" do?
- Connects to api's networks with a new IP
- Shares api's network namespace: same IP and localhost
- Routes all traffic through api as a proxy
- Makes api depend on this service