Sub topic 13
Limiting container networking and published ports
This chapter assumes you can run containers, publish ports with -p, and read basic firewall rules. It covers how to reduce what a container can reach and what can reach it.
The default is wider than you want
Containers started without options join the default bridge network. Every container on it can talk to every other one by IP address. Publishing a port with -p 8080:80 binds it to 0.0.0.0, so any machine that can reach the host can reach the container.
Docker also writes its own iptables rules for published ports. These rules are evaluated before the ones that tools such as ufw manage. A port you published can therefore be reachable even when your host firewall appears to block it.
Bind published ports to an address
Publish only the ports that must be reachable, and bind them to the narrowest address that works.
# Reachable only from the host itself (for example, by a local reverse proxy)
docker run -d --name proxy -p 127.0.0.1:8080:80 nginx:1.27
# Reachable from every interface: avoid unless intended
docker run -d --name open -p 8081:80 nginx:1.27Do not publish databases, caches, or admin ports at all. Other containers can reach them over a shared Docker network without a published port.
Separate networks by role
Create user-defined networks so containers share a network only when they need to talk to each other. User-defined networks also give you DNS names for containers. The --internal flag blocks all traffic to and from outside the Docker host.
docker network create frontend
docker network create --internal backend
docker run -d --name cache --network backend redis:7
docker run -d --name api --network backend myapp:1.0
docker network connect frontend api
docker run -d --name proxy --network frontend -p 127.0.0.1:8080:80 nginx:1.27Here cache has no route to the internet and no path to proxy. Only api sits on both networks.
Remove networking you do not need
- Use
--network nonefor batch jobs that need no network. The container gets only a loopback interface. - Never use
--network hostfor ordinary services. The container shares the host's network stack, so it can bind any host port and see host-local services. Port isolation is gone. - On the default bridge, set
"icc": falsein/etc/docker/daemon.jsonto stop container-to-container traffic unless containers are linked explicitly. User-defined networks are the better long-term answer.
Filter traffic with the DOCKER-USER chain
Docker does not modify the DOCKER-USER iptables chain, and it is evaluated before Docker's own rules. Put your restrictions there. This example drops traffic to published ports unless it comes from one trusted subnet. It assumes the external interface is eth0.
sudo iptables -I DOCKER-USER -i eth0 ! -s 203.0.113.0/24 -j DROPCommon errors
- Assuming
ufw deny 8081blocks a published port. It usually does not. - Putting every service on one shared network "for simplicity".
- Using
--network hostto avoid debugging port mappings. - Forgetting that
-p 5432:5432exposes a database to the whole network.
Exercise
A database container needs to be reachable only by an API container, and must not reach the internet. Which network setup and port options achieve this?
Show answer
Create a network with docker network create --internal backend, attach both the database and the API to it, and publish no ports for the database. The API reaches the database by container name over backend. If the API must serve outside clients, also connect it to a second, non-internal network and publish its port bound to a specific address.