Lesson 6 of 9 · 10 min · Container networking
Container networking: bridge, host, none and port publishing
What you will learn
- The network drivers: bridge, user-defined bridge, host, none
- How containers get IP addresses and reach the outside world
- Publishing ports:
-p, binding to localhost,-P - How to see what happened (
docker port,ss,docker network inspect) - Security consequences of published ports
In simple words
Each container gets its own private network address, like a flat inside a gated society. Flats can talk to each other, but outsiders can't reach one unless the gate is told to forward visitors to it. Publishing a port does exactly that. This lesson shows the options and their risks.
Words to know
- Bridge network: Docker's private network that containers on one machine share.
- Publish a port: making a container's port reachable from outside, such as
-p 8080:80. - Host network: letting the container use the machine's own network directly, with no separation.
- localhost: the machine itself. Binding a port to it keeps that port private to the machine.
Every container gets its own network stack
A container has its own network namespace: its own interfaces, IP address, routing table and ports (Linux course and Networking course). Two containers can both listen on port 80 without conflict, because each has its own port 80. Docker connects those namespaces to the outside through network drivers.
docker network ls
shows at least bridge, host and none.
The bridge network (default)
When you do not say otherwise, a container joins the default network called bridge. Docker creates a virtual switch on the host (the interface docker0), gives each container a virtual network cable (a veth pair) plugged into it, and assigns addresses from a private range, by default 172.17.0.0/16. The host acts as the gateway and does NAT, as in the Networking course: containers can reach the internet with the host's address, but the outside world cannot reach them unless you publish a port.
container A 172.17.0.2 ──┐
├── docker0 bridge 172.17.0.1 ── NAT ── eth0 (host) ── internet
container B 172.17.0.3 ──┘
From inside a container, ip route shows default via 172.17.0.1, as in our earlier traffic experiments (the Docker bridge was hop 1 in the traceroute).
If 172.17.0.0/16 clashes with a corporate network or VPN, change the daemon's default (default-address-pools in /etc/docker/daemon.json) or choose a subnet for your own networks.
Default bridge versus user-defined bridge
The built-in bridge network has a handicap: no name resolution between containers. In our test two containers on it could not find each other by name:
default bridge, ping by name: ping: bad address 'a2'
On a user-defined bridge network, Docker provides a built-in DNS server, and containers reach each other by container name:
docker network create --subnet 172.30.0.0/24 appnet
docker run -d --name b1 --network appnet alpine:3.20 sleep 300
docker run -d --name b2 --network appnet alpine:3.20 sleep 300
docker exec b1 ping -c1 b2
64 bytes from 172.30.0.3: seq=0 ttl=64 time=0.104 ms
docker network inspect appnet listed b2=172.30.0.3/24 b1=172.30.0.2/24. User-defined networks also give better isolation (only containers on the same network can talk) and let you connect or disconnect running containers (docker network connect). Always create your own network for multi-container apps (Compose does this automatically). The DNS details are the next lesson.
host and none
--network host: the container shares the host's network stack. No isolation, no NAT, no port publishing: if the app listens on 8080 it is on the host's port 8080. Fastest, useful for high-performance networking or tools that must see the host's interfaces; but containers can then conflict on ports and have access to host-only services. Not available the same way on Docker Desktop for Windows and macOS.--network none: only a loopback interface; no network at all. Good for batch jobs that must be isolated from the network.- Others:
macvlan(a container gets its own MAC and address on the physical network),overlay(spans several Docker hosts; Swarm and some orchestrators). Kubernetes uses its own network plugins (CNI).
Publishing ports
By default nothing outside the host can reach a container's ports. Publishing asks Docker to forward traffic from a host port to a container port, using iptables NAT rules:
docker run -d -p 18081:80 nginx:1.27-alpine # host 18081 -> container 80, on ALL host interfaces
docker run -d -p 127.0.0.1:18082:80 nginx:1.27-alpine # only reachable from the host itself
docker run -d -P nginx:1.27-alpine # publish every EXPOSEd port on a random host port
docker ps showed:
n1 0.0.0.0:18081->80/tcp, [::]:18081->80/tcp
n2 127.0.0.1:18082->80/tcp
n3 0.0.0.0:32768->80/tcp, [::]:32768->80/tcp
and ss -tln on the host listed 0.0.0.0:18081 and 127.0.0.1:18082 — exactly the listening addresses you know from the Linux course. Both URLs returned 200 from the host. The format is -p [host_ip:]host_port:container_port[/protocol]; add /udp for UDP. docker port NAME shows the mappings.
Points that trip people up:
-p 8080:80binds to all interfaces (0.0.0.0). On a server with a public IP, the port is open to the internet, even if the app was meant to be private. Use-p 127.0.0.1:8080:80when only a local reverse proxy should reach it.- Docker edits iptables itself, and its rules are evaluated before typical
ufwrules, soufwdoes not block published container ports (Networking course, firewalls lesson). Control exposure with the bind address, or with Docker'sDOCKER-USERchain. EXPOSEin a Dockerfile only documents a port; it publishes nothing.- Only one container can use a given host port. Two containers with
-p 80:80conflict (port is already allocated). The usual solution is a single reverse proxy container publishing 80/443 and routing to the others by name. - Containers on the same network need no published ports to talk to each other: use the container port directly (
http://b2:80). Publish only what outside clients need.
Looking at what Docker did
docker inspect NAME --format '{{json .NetworkSettings.Networks}}' # IPs, gateways, networks
docker network inspect appnet # who is attached, subnet
docker exec NAME ip addr # inside the container's network namespace
sudo iptables -t nat -L DOCKER -n # the NAT rules for published ports
(For some images ip is BusyBox's small version without -br. In a minimal image there may be no tools at all; debug from a helper container that shares the namespace: docker run --rm -it --network container:NAME nicolaka/netshoot.)
Containers reach the internet through the host's NAT, so from inside, the source address a remote server sees is the host's address. Calls to localhost from inside a container reach the container itself, not the host: to reach a service on the host use the host's address or host.docker.internal (Docker Desktop; on Linux add --add-host=host.docker.internal:host-gateway). This is a top beginner confusion when an app in a container cannot find the database "on localhost".
Common mistakes
- Using
localhostto reach another container or the host. - Publishing ports to
0.0.0.0on a public server and exposing databases. - Relying on
ufwto protect published ports. - Using the default bridge for multi-container apps and then asking why names do not resolve.
- Hard-coding container IPs (they change when containers restart). Use names.
- Port conflicts with something already listening on the host port.
--network hostcasually, losing isolation.
Try it yourself
- Create a user-defined network, start two containers on it, and ping by name. Repeat on the default bridge and note the difference.
- Publish nginx with
-p 18081:80and again with-p 127.0.0.1:18082:80; usess -tlnandcurlfrom another machine (or the host's LAN address) to see which is reachable. - Start a container with
--network hostand see which ports it uses on the host withss -tlnp. - Run
docker run --rm --network none alpine ip addrand explain what you see.
Check yourself
Why do two containers listening on port 80 not clash?
Each container has its own network namespace and therefore its own port space. Conflicts occur only when you publish both to the same host port.
What is the difference between -p 8080:80 and -p 127.0.0.1:8080:80?
The first binds the host port on all interfaces, making it reachable from other machines; the second binds only the loopback address, reachable only from the host itself.
Why can't containers on the default bridge reach each other by name, and what is the fix?
The default bridge has no built-in DNS name resolution between containers. Create a user-defined bridge network (or use Compose), where Docker's DNS resolves container and service names.
What does --network host do and what is the trade-off?
The container uses the host's network stack directly: no NAT or port publishing, slightly faster, but no network isolation and potential port conflicts.
Interview questions
How does a request from the internet reach a container?
The host's published port receives it; an iptables NAT (DNAT) rule rewrites the destination to the container's IP and port on the bridge network; the container replies and the reply is translated back (conntrack).
An app in a container cannot connect to a database running on the host at localhost:5432. Why?
Inside the container localhost is the container itself. Use the host's address on the bridge (gateway), host.docker.internal, or run the database as another container on the same network.
How would you let a reverse proxy reach several app containers without publishing their ports?
Put them on the same user-defined network and proxy to their names and container ports; publish only the proxy's ports.
Summary
- Containers have their own network namespaces; Docker joins them with drivers: bridge (default, NAT), user-defined bridge (DNS by name), host (shared stack), none.
-p [ip:]host:containercreates NAT forwarding; bind to127.0.0.1for private services;-pbypasses ufw.- Use user-defined networks for apps; internal traffic needs no published ports.
localhostinside a container is the container.
Going deeper
- Docker networking overview: drivers and concepts.
- Bridge network driver: default and user-defined bridges.
- Published ports:
-poptions and caveats. - Packet filtering and firewalls: how Docker interacts with iptables and ufw.
See how much of this you can explain
A free mock interview asks you new questions on Docker and containers for DevOps Engineer and scores every answer, with what to fix.