Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

Chapter 7Name in the course: Container networking

Connect containers with networks

Containers talk to each other and to the outside through networks. On a user-defined network they find each other by name; published ports make a service reachable from the host or the internet.

  • 4 Lessons
  • 5 Commands

$ run as a normal user# run as root (or with sudo)

7.1

Know the network drivers

The driver decides how the container is connected.

DriverMeaning
bridge (default)A private network on the host, NAT to the outside
User-defined bridgeLike bridge, plus DNS by container name — use this
hostNo isolation: the container uses the host's network
noneNo network at all
macvlan / ipvlanThe container gets an address on the physical LAN
overlayAcross several hosts (Swarm)

Commands in this lesson

7.2

Use a user-defined network

Containers on the same user-defined network reach each other by name. Only the front service publishes a port.

docker network create shopnet
docker run -d --name db --network shopnet -e POSTGRES_PASSWORD=secret postgres:16
docker run -d --name api --network shopnet -e DATABASE_URL=postgres://postgres:secret@db:5432/postgres myorg/api:2.0
docker run -d --name web --network shopnet -p 8080:80 nginx:1.27
docker exec api getent hosts db
docker network inspect shopnet --format '{{range .Containers}}{{.Name}} {{.IPv4Address}}{{"\n"}}{{end}}'

Remember

The database publishes no port. It is reachable only for containers on shopnet — a smaller attack surface.

7.3

Publish ports carefully

-p opens a port on the host. By default it listens on all host addresses.

docker run -d -p 8080:80 nginx:1.27
docker run -d -p 127.0.0.1:5432:5432 postgres:16
docker run -d -P nginx:1.27
docker port $(docker ps -lq)

Watch out

Docker writes its own firewall (iptables/nftables) rules. A published port can be reachable from the internet even if your host firewall seems to block it. Bind to 127.0.0.1 when only local access is needed.

7.4

Debug container networking

Test from inside the container, step by step: name → IP → port.

docker exec api getent hosts db
docker exec api sh -c 'nc -zv db 5432'
docker run --rm -it --network shopnet nicolaka/netshoot
docker network ls
docker network disconnect shopnet web

netshoot is a debug image with dig, curl, tcpdump, ss and more — join the same network and test.

All commands in this chapter