Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

DK
Docker
0/10 completed
Beginner
Why containers?Install Docker and run your first containerImages vs containers
Intermediate
Writing a DockerfileVolumes and persistent dataNetworking and ports
Advanced
Docker ComposeMulti-stage builds and small images
Pro
Security and production best practicesRegistries and CI/CD
Tracks / Docker / Intermediate

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:

bash
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 mappings

On a server, -p 5432:5432 exposes your database to the internet, and Docker's port publishing can bypass host firewalls like ufw. Only publish what must be public, and bind admin ports to 127.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.

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

bash
docker network ls
docker network inspect appnet
docker network connect appnet some-container    # attach a running container

Network 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

bash
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-image

Your 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

bash
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/netshoot

Practice

  1. Create a network lab. Run nginx named web on it without publishing ports.
  2. Run alpine on the same network and wget -qO- http://web to fetch the page by name.
  3. Try the same from an alpine container not on lab, and explain the result.
✎ Check your understanding

1.Inside a container, what does localhost refer to?

2.How does an API container reach a database container named db?

← Volumes and persistent data