Chapter 0615 mins • intermediate
Container Networking & Embedded DNS
Custom Bridges, Service Discovery, and Why localhost Fails Between Containers
What You Will Master:
Understand why `localhost` inside container A cannot reach container B
Create user-defined bridge networks with `docker network create`
Use Docker automatic DNS resolution to connect microservices
The `localhost` Trap
A frequent beginner mistake is trying to make container `frontend` call `http://localhost:5000` to reach `backend`.
**Why this fails:** Each container has its own isolated network namespace and loopback interface (`127.0.0.1`). Inside `frontend`, `localhost` refers strictly to `frontend` itself, not other containers or the host machine!
User-Defined Bridge & Docker DNS
When you create a custom bridge network:
```bash
docker network create app-net
```
Docker activates an embedded DNS server (`127.0.0.11`). Containers attached to `app-net` can communicate simply using each other's container names (e.g. `http://api:5000`).
Interactive Lab
Connect Two Containers on Custom Network
Create a network named `app-net` and run a backend container attached to it.
0/2 Steps Done
Step 1: Create network named app-net
Run `docker network create app-net`
Step 2: Run api container on app-net
Run `docker run -d --name api --network app-net node:22-alpine`
Chapter Knowledge Check
Take the interactive MCQ test to unlock bonus XP
Sponsor & Resources