docker, updated

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

DriverWhat it isUse it for
bridge (default bridge)The docker0 network every container joins when no --network is given. No DNS between containersNothing new. Legacy default
bridge (user-defined)Own Linux bridge and subnet per network, NAT to the outside, built-in DNSThe normal choice for single-host setups. Compose creates one per project
hostNo network namespace, the container uses the host’s interfaces and ports directlyPerformance or tools that need the host stack (monitoring, sniffing). -p is ignored
noneOnly loopbackBatch jobs and anything that must not talk to the network
overlaySpans several Docker hosts (Swarm), VXLAN between themSwarm services across nodes
macvlanContainer gets its own MAC and an IP directly on the physical LANAppliances that must look like a real host on the LAN. The host itself cannot reach these containers without an extra macvlan interface
ipvlanLike 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

WhatCommand
List networksdocker network ls
Create a user-defined bridgedocker network create appnet
With fixed subnet and gatewaydocker 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 LANdocker network create -d macvlan --subnet 192.168.10.0/24 --gateway 192.168.10.1 -o parent=eth0 lan
Start a container on a networkdocker 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 networkdocker network connect backnet web
Remove it againdocker network disconnect backnet web
Details, subnet and attached containersdocker network inspect appnet
Delete / delete all unuseddocker network rm appnet, docker network prune

When is a port exposed?

In the configReachable fromNotes
Nothing, or EXPOSE 80 / Compose expose:Containers on the same Docker network, and the host via the container IPEXPOSE is documentation only, it publishes nothing
-p 8080:80 / ports: ["8080:80"]Everyone who can reach the host, on all interfaces, IPv4 and IPv6Ignores ufw, see below
-p 127.0.0.1:8080:80 / ports: ["127.0.0.1:8080:80"]Only the host itselfRight choice behind a reverse proxy on the host or an SSH tunnel
-p 10.0.0.5:8080:80Clients that reach the host on 10.0.0.5Bind to one interface, e.g. the management network
-PEvery EXPOSEd port on a random high port, all interfacesEasy to forget, avoid on servers
--network hostWhatever the process listens on, like a local serviceHere ufw does apply (normal INPUT chain)
Container on an --internal network onlyOnly 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:

  1. Don’t publish what only other containers need. Put them on a shared network instead.

  2. 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.

  3. Change the default bind address for every -p without an IP, in /etc/docker/daemon.json, then sudo systemctl restart docker:

    {
      "ip": "127.0.0.1"
    }
  4. Filter in the DOCKER-USER chain. 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 DROP

    These rules are not persistent. Save them with iptables-persistent or a systemd unit that runs after docker.service.

Never set "iptables": false to make ufw work: containers lose outbound NAT and port publishing breaks.

See what is exposed

WhatCommand
Published ports of all containersdocker ps --format 'table {{.Names}}\t{{.Ports}}'
Ports of one containerdocker port web
What listens on the host (docker-proxy shows up here)sudo ss -tlnp
The NAT rules Docker createdsudo iptables -t nat -L DOCKER -n
Check from outsidenmap -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?

WhatCommand
Networks of all containersdocker ps --format 'table {{.Names}}\t{{.Networks}}'
Networks and IPs of one containerdocker inspect -f '{{range $n, $c := .NetworkSettings.Networks}}{{$n}} {{$c.IPAddress}}{{println}}{{end}}' web
Containers and IPs on one networkdocker network inspect -f '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{println}}{{end}}' appnet
Subnet and gateway of a networkdocker 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
ResolvesExample
Container nameping db
Compose service namepostgres://db:5432
Network alias--network-alias api, Compose aliases:
Short container IDping 3a8f60cfd28f
Everything elseForwarded to the host’s upstream resolvers
# 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

WhatCommand
Bridges (docker0, br-<network-id>)ip -br link show type bridge
veth pairs, one per container and networkip -br link show type veth
Which bridge belongs to which networkdocker network ls (ID matches br-<id>)
NAT and forwarding rulessudo iptables -t nat -S, sudo iptables -S FORWARD
Capture traffic of one networksudo tcpdump -i br-<id> -n
Enter a container’s namespace with host toolssudo nsenter -t $(docker inspect -f '{{.State.Pid}}' web) -n ss -tlnp

Gotchas

§ home
~/knubertus/home/
1,1 Top