Docker networking explained: bridge, host, container DNS and published ports
Why localhost inside a container is not your laptop: the default bridge, user-defined networks and Docker DNS, published ports, the host network and host.docker.internal, run for real with a Go app, Postgres and Redis.
The most common Docker question I see is some form of "my app can't connect to the database". The app works on the laptop, the database runs in a container, and inside a container the same localhost:5432 gives connection refused. It's always networking, and it's always the same few ideas.
In this blog we will go through them by running shorty, the Go URL shortener from Building a REST API in Go with only the standard library, with Postgres and Redis in separate containers: the default bridge, user-defined networks with DNS, published ports, the host network, and how a container reaches your laptop through host.docker.internal. Level is intermediate. If images and containers are new, start with Docker from zero.
I ran everything on Docker 29.8.2 with OrbStack on a Mac. OrbStack does a few things differently from Docker Desktop and from Docker on Linux, and I'll point those out where I hit them, because a couple of them surprised me.
localhost is the container itself
Each container gets its own network namespace: its own interfaces, its own routing table, its own localhost. So localhost inside a container means that container and nothing else. Not your laptop. Not the Postgres container next to it.
Here it is, using a network we will create in a minute:
docker run --rm --network shorty-net redis:8-alpine redis-cli -h localhost ping
Could not connect to Redis at localhost:6379: Connection refused
There is no Redis in the container that ran redis-cli, so nothing listens on its localhost. The fix is never "make localhost work". The fix is to use a name that points at the right container.
There's one exception, and it's worth knowing because ECS and Kubernetes are built on it. Containers can share one network namespace:
docker run --rm --network container:redis redis:8-alpine redis-cli -h localhost ping
PONG
--network container:redis puts the new container inside the Redis container's namespace, so their localhost is the same. That's exactly what a Kubernetes pod is, and what an ECS task in awsvpc mode is: containers in one task talk over localhost. Inside one container group, localhost works. Between groups, you need names.
The default bridge has no DNS
Docker creates three networks on install:
docker network ls
NAME DRIVER
bridge bridge
host host
none null
Every docker run without --network lands on bridge. Containers there get an IP from a private subnet and can reach each other by IP, but not by name:
docker run -d --rm --name redis1 redis:8-alpine
docker run --rm redis:8-alpine redis-cli -h redis1 ping
Could not connect to Redis at redis1:6379: Name does not resolve
By IP it works:
docker inspect redis1 --format '{{range $k,$v := .NetworkSettings.Networks}}{{$k}} {{$v.IPAddress}}{{end}}'
docker run --rm redis:8-alpine redis-cli -h 192.168.215.2 ping
bridge 192.168.215.2
PONG
But IPs change every time a container is recreated, so nobody should hard-code them. The default bridge is a leftover from the early days. There used to be --link for names on it, which is deprecated. Just don't use the default bridge for anything that talks to anything else.
User-defined networks have DNS
Create your own network and the problem goes away:
docker network create shorty-net
docker run -d --rm --name redis --network shorty-net redis:8-alpine
docker run --rm --network shorty-net redis:8-alpine sh -c \
'redis-cli -h redis ping; getent hosts redis; grep -v "^#" /etc/resolv.conf'
PONG
192.168.147.2 redis redis
nameserver 127.0.0.11
options ndots:0
On a user-defined network every container's name (and any --network-alias) resolves through Docker's embedded DNS server at 127.0.0.11. Names outside the network get forwarded to the normal DNS. On the default bridge, the same resolv.conf showed nameserver 0.250.250.200 instead, OrbStack's own resolver, and no container names at all.
Docker Compose creates one of these networks for every project. That's why services in a compose file find each other by service name. My blog's Caddy does reverse_proxy ghost:2368 for exactly this reason, see Automatic HTTPS and www redirects for a Ghost blog using Caddy. Ghost and MySQL don't publish any ports at all, only Caddy does.
shorty on its own network
Now the real thing. Postgres gets the alias postgres so shorty's usual DATABASE_URL works unchanged:
docker run -d --rm --name pg --network shorty-net --network-alias postgres \
-e POSTGRES_USER=shorty -e POSTGRES_PASSWORD=shorty -e POSTGRES_DB=shorty \
-v shorty-pgdata:/var/lib/postgresql postgres:18-alpine
docker run --rm --network shorty-net \
-e DATABASE_URL='postgres://shorty:shorty@postgres:5432/shorty?sslmode=disable' \
shorty:dev /shorty migrate up
OK up 00001_create_links.sql (2.99ms)
And the API, with a published port:
docker run -d --rm --name api --network shorty-net -p 127.0.0.1:18080:8080 \
-e DATABASE_URL='postgres://shorty:shorty@postgres:5432/shorty?sslmode=disable' \
-e REDIS_URL='redis://redis:6379/0' -e BASE_URL=http://localhost:18080 \
shorty:dev
docker port api
curl -s 127.0.0.1:18080/readyz
8080/tcp -> 127.0.0.1:18080
{"postgres":"ok","redis":"ok"}
Note that Postgres and Redis publishes nothing. They're reachable by name from inside shorty-net and from nowhere else, which is what you want for a database.
Published ports
-p 127.0.0.1:18080:8080 means: listen on port 18080 of the host's 127.0.0.1, forward to port 8080 in the container. The IP part matters a lot. If you write -p 18080:8080, Docker listens on all interfaces, 0.0.0.0, so anyone on your Wi-Fi (or the internet, on a server) can reach it. On Linux, Docker writes its own iptables rules for published ports, and they get applied before rules from ufw, so a firewall you thought was protecting the port often isn't. I put 127.0.0.1: in front of every port that only I need. In shorty's compose file every port is written that way.
EXPOSE in a Dockerfile doesn't publish anything, by the way. It's documentation.
Now the OrbStack surprise. In the previous post I tried to publish a local registry on port 5055, and curl got answers from a completely different program. Today I tried again on purpose, with a Python dev server already listening on 127.0.0.1:5055 on my Mac:
docker run -d --rm --name clash -p 127.0.0.1:5055:6379 redis:8-alpine
docker port clash
(printf 'PING\r\n'; sleep 1) | nc -w 2 127.0.0.1 5055
6379/tcp -> 127.0.0.1:5055
HTTP/1.1 400 Bad request syntax ('PING')
Server: Werkzeug/3.1.9 Python/3.15.2
docker run succeeded and docker port showed the mapping, but the port belonged to the Python process and my Redis was not reachable from the Mac at all. On Linux I'd expect docker run to fail with an "address already in use" error instead. So on a Mac, when a published port answers with something weird, check lsof -nP -iTCP:5055 -sTCP:LISTEN before you check anything else.
From a container to your laptop: host.docker.internal
Sometimes the container needs something on your machine: a service you run from your IDE, a mock server. localhost won't work, we covered that. Docker Desktop and OrbStack give you a name for the host, host.docker.internal:
docker run --rm redis:8-alpine sh -c \
'wget -qO- -T 3 http://localhost:18080/healthz; wget -qO- -T 3 http://host.docker.internal:18080/healthz'
wget: can't connect to remote host: Connection refused
{"status":"ok"}
The first call hit the container's own localhost. The second went out to the Mac and back in through the published port.
Then I started a plain python3 -m http.server 18999 --bind 127.0.0.1 on the Mac, bound to loopback only, and tried from a container:
docker run --rm redis:8-alpine wget -qO- -T 3 http://host.docker.internal:18999/
hello from the mac
On OrbStack that works even though the server only listens on the Mac's 127.0.0.1. Inside the container host.docker.internal resolved to 0.250.250.254, an OrbStack address that forwards to the Mac's loopback. OrbStack also has host.orb.internal for the same thing.
On Docker Engine on Linux, host.docker.internal doesn't exist by default. You add it yourself:
docker run --rm --add-host=host.docker.internal:host-gateway redis:8-alpine \
wget -qO- -T 3 http://host.docker.internal:18999/
host-gateway maps the name to the bridge's gateway IP on the host. The catch on Linux is that a service bound to 127.0.0.1 is not reachable on the gateway IP, so it has to listen on 0.0.0.0 or on the bridge address. On OrbStack the --add-host version gave the same 0.250.250.254 and worked as well, so putting it in a compose file is a cheap way to make one setup work on both. In Compose that's extra_hosts: ["host.docker.internal:host-gateway"].
The host network
--network host skips the namespace: the container uses the host's network directly. No port publishing, no NAT, the app's port is the host's port.
docker run -d --rm --name hostnet --network host redis:8-alpine redis-server --port 16390
(printf 'PING\r\n'; sleep 1) | nc -w 2 127.0.0.1 16390
docker exec hostnet hostname
+PONG
orbstack
The hostname gives it away. On a Mac "the host" is Docker's Linux VM, not macOS, and inside it ip addr showed the VM's docker0 bridge and friends. OrbStack then forwards the port to the Mac's localhost, which is why nc from macOS got a PONG. Docker Desktop added host networking later, as an opt-in setting, so don't count on it behaving the same there.
On a Linux server host networking is real and has its uses: lots of ports, or every bit of network performance. Mostly I avoid it. You lose isolation and two containers can't use the same port. My blog's EC2 host doesn't need it, the SSM agent and Docker are enough, as in Running a Docker host on EC2 with SSM instead of SSH.
Cleaning up
docker rm -f api pg redis
docker network rm shorty-net
docker volume rm shorty-pgdata
A network can only be removed when no container is attached to it, so the order matters.
If I had to boil it down to one rule for a new project: put everything on a user-defined network, connect by name, publish only what your browser needs and always with 127.0.0.1: in front. Next part wraps all of these docker run lines in a single compose file, so you won't have to type them again.