Networking and ports
Real apps are several containers talking to each other: a web app, an API and a database. Docker networking decides who can reach whom, and how the outside world gets in.
Publishing ports: outside → container
By default, a container's ports are private. -p publishes them on the host:
docker run -d -p 8080:80 nginx # host 8080 → container 80
docker run -d -p 127.0.0.1:8081:80 nginx # only reachable from this machine
docker run -d -P nginx # publish EXPOSEd ports on random host ports
docker port <container> # show the mappingsOn a server,
-p 5432:5432exposes your database to the internet, and Docker's port publishing can bypass host firewalls likeufw. Only publish what must be public, and bind admin ports to127.0.0.1.
Container ↔ container: user-defined networks
Containers on the same user-defined network can reach each other by name. Docker runs a built-in DNS server.
docker network create appnet
docker run -d --name db --network appnet \
-e POSTGRES_PASSWORD=secret postgres:16
docker run -it --rm --network appnet postgres:16 \
psql -h db -U postgres -c "SELECT 'connected by name!';"The second container connects to host db. No IP addresses and no published ports are needed. The database is reachable only from inside appnet, which is exactly what you want.
docker network ls
docker network inspect appnet
docker network connect appnet some-container # attach a running containerNetwork drivers
| Driver | Use |
|---|---|
bridge |
Default; private network on a single host |
host |
Container shares the host's network stack (no isolation; Linux only) |
none |
No networking at all |
overlay |
Spans multiple hosts (Docker Swarm) |
The default bridge network (used when you don't pass
--network) does not provide DNS by container name. Always create your own network, or use Compose, which does it for you.
The localhost trap
Inside a container, localhost means the container itself, not your computer and not other containers.
| From | To | Use |
|---|---|---|
| Container | Another container | Its name on a shared network (db:5432) |
| Container | Your host machine | host.docker.internal (Docker Desktop; on Linux add --add-host=host.docker.internal:host-gateway) |
| Host | Container | localhost:<published port> |
A two-container app by hand
docker network create shop
docker run -d --name redis --network shop redis:7-alpine
docker run -d --name api --network shop -p 3000:3000 \
-e REDIS_URL=redis://redis:6379 \
my-api-imageYour app reads REDIS_URL and connects to redis:6379. Only the API is published to the outside world. This works, but typing these commands gets tedious fast, which is exactly what Docker Compose fixes in the next lesson.
Debugging network issues
docker exec -it api sh
# inside:
ping redis
nc -zv redis 6379 # is the port open?
wget -qO- http://other:8080/health
# or use a toolbox image on the same network:
docker run -it --rm --network shop nicolaka/netshootPractice
- Create a network
lab. Runnginxnamedwebon it without publishing ports. - Run
alpineon the same network andwget -qO- http://webto fetch the page by name. - Try the same from an alpine container not on
lab, and explain the result.
1.Inside a container, what does localhost refer to?
2.How does an API container reach a database container named db?