AI Hints
Home›Docker Tutorial›Connect & Compose
Docker · Lesson 6 of 8

Networking: Connect Your Containers

Let containers talk to each other.

⏱ 2 min read▶ Video at 8:50🎓 Free
Watch this lesson in the Docker courseWatch on YouTube ↗Full Docker course

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

  1. Your browserlocalhost:8080
  2. Port mapping-p 8080:5000 on the web container
  3. web containertalks to "db" and "cache" by name
  4. db and cachenot exposed to the outside

Network types

DriverWhen to use it
bridge (user-defined)the default choice: containers on one host talk by name
hostshare the host's network directly (Linux only), no isolation
noneno 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