Sub topic 8
Isolating services with multiple networks
This chapter assumes you can run containers with docker run, and that you know what a user-defined bridge network is and how containers reach each other by name. It also assumes basic TCP/IP knowledge (subnets, ports).
Why one network is not enough
Every container on a user-defined bridge network can reach every other container on that network, on any port. That is convenient, but it means a compromised web-facing container can talk directly to your database.
Docker lets you attach a container to more than one network. A container can talk only to peers that share at least one network with it. By choosing which networks each container joins, you segment your application without any firewall rules.
A three-tier layout
Consider a proxy, an application, and a database. The proxy must reach the app. The app must reach the database. The proxy should never reach the database.
The app appears twice in the drawing, but it is a single container with one interface on each network.
Building it with docker run
docker network create frontend
docker network create backend
docker run -d --name db --network backend \
-e POSTGRES_PASSWORD=example postgres:16
docker run -d --name app --network backend nginx:alpine
docker network connect frontend app
docker run -d --name proxy --network frontend nginx:alpinedocker run --network accepts only one network at start. docker network connect adds a further network to a running container. docker network disconnect removes one.
Now test the boundaries from the proxy container:
docker exec proxy getent hosts app
docker exec proxy getent hosts dbThe first command prints an address for app. The second prints nothing and exits with status 2, because db is not on any network the proxy shares. Docker's embedded DNS answers only for containers on networks the asking container belongs to.
Internal networks
A bridge network normally gives containers a route out through the host, so they can reach the internet. Adding --internal removes that route:
docker network create --internal backend-private
docker run -d --name db2 --network backend-private \
-e POSTGRES_PASSWORD=example postgres:16
docker run --rm --network backend-private busybox ping -c 1 -W 2 8.8.8.8The ping fails. Containers on an internal network can still reach each other, but they cannot reach outside addresses, and ports cannot be published from them.
Checking membership
To confirm which networks a container has joined, inspect it:
docker inspect app --format '{{json .NetworkSettings.Networks}}'The output lists one entry per network, each with its own IP address. You will see frontend and backend for app, but only backend for db.
What isolation does not do
- It does not encrypt traffic. Containers on the same network exchange plain packets.
- It does not limit ports between containers that share a network. If two containers share a network, every listening port is reachable.
- Published ports (
-p) are reachable from the host and beyond, regardless of network layout. Publish only what the outside needs.
Segmentation works best when each network answers a simple question: who needs to talk to whom? Put a container on a network only if it has a reason to be there.
Exercise
You run docker network create --internal data, start a container cache on data, and start api on both data and the default bridge network. Can cache download a package from the internet? Can api?
Show answer
cache cannot, because an internal network has no route outside the host. api can, because its interface on the default bridge network provides outbound access. Also, cache can reach api by name through data.
Exercise
Which command adds an already-running container named worker to a network named jobs?
Show answer
docker network connect jobs worker