Lesson 17 / 25
The Default Network and Service Names
Built-in DNS.
Reach services by name
Compose creates a default network for each project (named <project>_default) and attaches every service. Each service is reachable by its service name as a hostname: the API connects to db:5432, the web front end calls http://api:3000. Container IPs change on every recreation, so always use names. Scaled services (--scale worker=3) share one name and DNS returns several addresses.
Who can talk to whom
Services find each other by name on project networks; custom networks isolate tiers.
An office phone directory
You dial "accounts" and reach whoever is on the accounts desk today; you never need their personal number.
Use container ports between services
Inside the network use the container port (api:3000), not the published host port.
Quick check: How does the api service connect to the database in the demo?
- By container ID
- By the host IP and a published port only
- By the hostname db on the project network
- It cannot connect
Answer
By the hostname db on the project network — Service names are DNS names.