Docker networking
Docker network drivers, when a port is really exposed and why published ports bypass ufw, which network a container is on, and how the internal DNS resolves names.
Network types
| Driver | What it is | Use it for |
|---|---|---|
bridge (default bridge) | The docker0 network every container joins when no --network is given. No DNS between containers | Nothing new. Legacy default |
bridge (user-defined) | Own Linux bridge and subnet per network, NAT to the outside, built-in DNS | The normal choice for single-host setups. Compose creates one per project |
host | No network namespace, the container uses the host’s interfaces and ports directly | Performance or tools that need the host stack (monitoring, sniffing). -p is ignored |
none | Only loopback | Batch jobs and anything that must not talk to the network |
overlay | Spans several Docker hosts (Swarm), VXLAN between them | Swarm services across nodes |
macvlan | Container gets its own MAC and an IP directly on the physical LAN | Appliances that must look like a real host on the LAN. The host itself cannot reach these containers without an extra macvlan interface |
ipvlan | Like macvlan but shares the host’s MAC (L2 mode) or routes (L3 mode) | When the switch or cloud allows only one MAC per port |
container:<name> | Joins another container’s network namespace (same IP, same localhost) | Sidecars, debugging a container that has no tools in it |
# Debug a minimal container with tools from another image, in its namespace
docker run --rm -it --network container:web nicolaka/netshoot
Commands
| What | Command |
|---|---|
| List networks | docker network ls |
| Create a user-defined bridge | docker network create appnet |
| With fixed subnet and gateway | docker network create --subnet 10.50.0.0/24 --gateway 10.50.0.1 appnet |
| Internal network (no route out, no published ports) | docker network create --internal backnet |
| macvlan on the LAN | docker network create -d macvlan --subnet 192.168.10.0/24 --gateway 192.168.10.1 -o parent=eth0 lan |
| Start a container on a network | docker run -d --network appnet --name web nginx |
Fixed IP (needs a network with --subnet) | docker run -d --network appnet --ip 10.50.0.10 nginx |
| Add a running container to a second network | docker network connect backnet web |
| Remove it again | docker network disconnect backnet web |
| Details, subnet and attached containers | docker network inspect appnet |
| Delete / delete all unused | docker network rm appnet, docker network prune |
When is a port exposed?
| In the config | Reachable from | Notes |
|---|---|---|
Nothing, or EXPOSE 80 / Compose expose: | Containers on the same Docker network, and the host via the container IP | EXPOSE is documentation only, it publishes nothing |
-p 8080:80 / ports: ["8080:80"] | Everyone who can reach the host, on all interfaces, IPv4 and IPv6 | Ignores ufw, see below |
-p 127.0.0.1:8080:80 / ports: ["127.0.0.1:8080:80"] | Only the host itself | Right choice behind a reverse proxy on the host or an SSH tunnel |
-p 10.0.0.5:8080:80 | Clients that reach the host on 10.0.0.5 | Bind to one interface, e.g. the management network |
-P | Every EXPOSEd port on a random high port, all interfaces | Easy to forget, avoid on servers |
--network host | Whatever the process listens on, like a local service | Here ufw does apply (normal INPUT chain) |
Container on an --internal network only | Only containers on that network | -p is silently ignored |
Two containers on different user-defined networks can’t reach each other at all, not even by IP. A container on several networks is reachable from each of them.
Why published ports ignore ufw
ufw filters the INPUT chain, which is for traffic to the host itself. A published port is not that: Docker adds a DNAT rule in the nat table (chain DOCKER) that rewrites the destination to the container IP before filtering. The packet is then forwarded through the FORWARD chain to the bridge, where Docker’s own rules accept it. ufw’s deny 8080 never sees the packet.
sudo ufw status # says 8080 is denied...
sudo iptables -t nat -L DOCKER -n # ...but this DNAT rule forwards it anyway
sudo ss -tlnp | grep 8080 # docker-proxy listening on 0.0.0.0:8080
Ways to fix it, in order of preference:
-
Don’t publish what only other containers need. Put them on a shared network instead.
-
Bind to localhost (
-p 127.0.0.1:8080:80) and let a reverse proxy on the host or in Docker take the public traffic. -
Change the default bind address for every
-pwithout an IP, in/etc/docker/daemon.json, thensudo systemctl restart docker:{ "ip": "127.0.0.1" } -
Filter in the
DOCKER-USERchain. Docker jumps there first in FORWARD and never touches it. Match the original port with conntrack, because the packet is already rewritten:# Allow 8080 only from the admin network, drop it for everyone else sudo iptables -I DOCKER-USER -i eth0 -p tcp -m conntrack --ctorigdstport 8080 ! -s 10.0.0.0/24 -j DROPThese rules are not persistent. Save them with
iptables-persistentor a systemd unit that runs afterdocker.service.
Never set "iptables": false to make ufw work: containers lose outbound NAT and port publishing breaks.
See what is exposed
| What | Command |
|---|---|
| Published ports of all containers | docker ps --format 'table {{.Names}}\t{{.Ports}}' |
| Ports of one container | docker port web |
| What listens on the host (docker-proxy shows up here) | sudo ss -tlnp |
| The NAT rules Docker created | sudo iptables -t nat -L DOCKER -n |
| Check from outside | nmap -p- <host-ip> from another machine |
0.0.0.0:8080->80/tcp in docker ps means public; 127.0.0.1:8080->80/tcp means localhost only; a bare 80/tcp means not published.
Which network is a container on?
| What | Command |
|---|---|
| Networks of all containers | docker ps --format 'table {{.Names}}\t{{.Networks}}' |
| Networks and IPs of one container | docker inspect -f '{{range $n, $c := .NetworkSettings.Networks}}{{$n}} {{$c.IPAddress}}{{println}}{{end}}' web |
| Containers and IPs on one network | docker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{println}}{{end}}' appnet |
| Subnet and gateway of a network | docker network inspect -f '{{json .IPAM.Config}}' appnet |
| Internal network? | docker network inspect -f '{{.Internal}}' backnet |
$ docker ps --format 'table {{.Names}}\t{{.Networks}}\t{{.Ports}}'
NAMES NETWORKS PORTS
db backnet
web appnet,backnet 127.0.0.1:8080->80/tcp
Defining the networks
The usual pattern: a public network for the proxy and the app, an internal one for the app and the database. The database is not published and can’t reach the internet.
docker network create frontnet
docker network create --internal backnet
docker run -d --name db --network backnet postgres:17
docker run -d --name app --network frontnet myapp:1.0
docker network connect backnet app # app talks to both sides
docker run -d --name proxy --network frontnet -p 443:443 caddy
Same in Compose. Without a networks: key every service lands on <project>_default:
services:
proxy:
image: caddy
ports: ["443:443"]
networks: [front]
app:
image: myapp:1.0
networks: [front, back]
db:
image: postgres:17
networks: [back] # no ports: only reachable from app
networks:
front:
back:
internal: true
When a container is on several networks, the default route goes through the network with the highest gateway priority (docker network connect --gw-priority 10 frontnet app, Compose gw_priority:, default 0). On a tie the network whose name sorts first wins, not the one connected first. docker exec app ip route shows which one is used.
Internal DNS
On user-defined networks every container uses Docker’s embedded DNS server at 127.0.0.11:
$ docker exec db cat /etc/resolv.conf
nameserver 127.0.0.11
options ndots:0
| Resolves | Example |
|---|---|
| Container name | ping db |
| Compose service name | postgres://db:5432 |
| Network alias | --network-alias api, Compose aliases: |
| Short container ID | ping 3a8f60cfd28f |
| Everything else | Forwarded to the host’s upstream resolvers |
- Only names on a shared network resolve. A container on
frontnetgets NXDOMAIN fordbonbacknet. - The default
bridgenetwork has no embedded DNS. Containers get a copy of the host’s resolvers and only reach each other by IP (or the deprecated--link). - Several containers with the same alias get DNS round-robin: a cheap way to spread load over replicas (
docker compose up -d --scale app=3does the same with the service name). - If the host uses
systemd-resolved(127.0.0.53), Docker forwards to the real upstream servers from/run/systemd/resolve/resolv.conf, since the container can’t reach the host’s loopback. - Override per container with
--dns 10.0.0.53and--dns-search corp.local, or for all containers with"dns": ["10.0.0.53"]indaemon.json. host.docker.internalonly exists on Linux if you add it:--add-host host.docker.internal:host-gateway.
# Which names does a container answer to on each network?
docker inspect -f '{{range $n, $c := .NetworkSettings.Networks}}{{$n}}: {{join $c.DNSNames ", "}}{{println}}{{end}}' web
# Test resolution from inside the network
docker run --rm --network appnet alpine nslookup web
On the host
| What | Command |
|---|---|
Bridges (docker0, br-<network-id>) | ip -br link show type bridge |
| veth pairs, one per container and network | ip -br link show type veth |
| Which bridge belongs to which network | docker network ls (ID matches br-<id>) |
| NAT and forwarding rules | sudo iptables -t nat -S, sudo iptables -S FORWARD |
| Capture traffic of one network | sudo tcpdump -i br-<id> -n |
| Enter a container’s namespace with host tools | sudo nsenter -t $(docker inspect -f '{{.State.Pid}}' web) -n ss -tlnp |
Gotchas
- Default address pools overlap. Docker takes 172.17.0.0/16 and up. If that collides with your LAN or VPN, the host loses routes. Set
"default-address-pools": [{"base": "10.200.0.0/16", "size": 24}]indaemon.json. -p 8080:80also publishes on IPv6 ([::]:8080) when IPv6 is on. Check both inss -tlnp.- Docker rewrites the firewall on every restart. Hand-made changes to its own chains are lost; only
DOCKER-USERis left alone. EXPOSEin a Dockerfile is not a security boundary. Every port the process listens on is reachable from the same network, exposed or not.