Docker Networking
Containers usually need network communication.
Examples:
- web server ↔ database
- API ↔ Redis
- reverse proxy ↔ backend
Docker provides virtual networking.
Default Bridge Network
By default, Docker creates:
bridge
network.
Containers connected to bridge can communicate internally.
Visual Network Example
+-------------------+
| Docker Host |
| |
| +-------------+ |
| | nginx | |
| +-------------+ |
| ↓ |
| +-------------+ |
| | postgres | |
| +-------------+ |
+-------------------+
Port Mapping
Containers are isolated.
To expose services externally:
docker run -p 8080:80 nginx
Breakdown:
| Part | Meaning |
|---|---|
8080 |
host port |
80 |
container port |
Access:
http://localhost:8080
Listing Networks
docker network ls
Inspect network:
docker network inspect bridge
Creating Custom Networks
docker network create mynetwork
Run container inside network:
docker run --network mynetwork nginx
Custom networks improve:
- isolation
- service discovery
- organization