Docker · Lesson 6 of 8
Networking: Connect Your Containers
Let containers talk to each other.
Real apps are made of several pieces: a web app, a database, a cache. Each runs in its own container, so they need a way to find and talk to each other. Docker networks give them that, with built-in DNS so containers can reach each other by name.
Create a network
terminal
docker network create appnet b3f0... docker run -d --name cache --network appnet redis:7 41aa... docker run --rm --network appnet redis:7 redis-cli -h cache ping PONG
The second container reached the first simply by its name, cache. Docker's DNS did the lookup.
How it fits together
- Your browserlocalhost:8080
- Port mapping-p 8080:5000 on the web container
- web containertalks to "db" and "cache" by name
- db and cachenot exposed to the outside
Network types
| Driver | When to use it |
|---|---|
| bridge (user-defined) | the default choice: containers on one host talk by name |
| host | share the host's network directly (Linux only), no isolation |
| none | no network at all, for fully isolated jobs |
Check your understanding
Quick check
How does a web container reach a database container named db on the same user-defined network?
Summary
Key takeaways
- User-defined networks give containers DNS by name
- Only publish ports (-p) for what users must reach
- Keep databases private inside the network
Next up: Docker Compose: Your Whole Stack — one file, one command