Bridge network
- Docker & Kubernetes course
- Beginner
- Lesson 74/82
- 4 min
Learn to create a custom bridge network.
The bridge network
Containers with no network specified are attached to the default bridge network. On this network containers can reach each other by IP but not by name (there's no DNS). User-defined bridge networks offer built-in DNS: a container's name is its host name for the others on the network:
docker network create uygulama-agi
docker run -d --name db --network uygulama-agi -e POSTGRES_PASSWORD=gizli postgres:16
docker run --rm --network uygulama-agi alpine ping -c 1 db
# PING db (172.19.0.2): 56 data bytesNetwork creation options
When creating a network, the subnet and gateway can be chosen by hand; handy to avoid ranges clashing with the company network. Unused networks can be removed:
docker network create --driver bridge --subnet 10.50.0.0/24 --gateway 10.50.0.1 ozel-ag
docker network rm ozel-ag
docker network prune -fPractice Access by name
Write three commands: create the uygulama-agi network; start a container named cache from redis:7 in the background on this network; in an alpine container deleted when done, run ping -c 1 cache on the same network.
Starter code
# buraya yazSample solution
docker network create uygulama-agi
docker run -d --name cache --network uygulama-agi redis:7
docker run --rm --network uygulama-agi alpine ping -c 1 cacheExpected output
5e6f708192a3… f8091a2b3c4d PING cache (172.19.0.2): 56 data bytes 64 bytes from 172.19.0.2: seq=0 ttl=64 time=0.112 ms
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Why doesn't ping db work on the default bridge network?
- The port isn't published
- db has stopped
- There's no DNS resolution by container name on the default bridge Correct answer
- ping isn't installed
Explanation: Name-based access needs a user-defined network.
2 Create a new network.
docker network ____ uygulama-agi
docker network create uygulama-agi
Explanation: The default driver is bridge.
3 Match.
| Term | Meaning |
|---|---|
default bridge |
No DNS by name |
user-defined bridge |
Built-in DNS |
--subnet |
IP range |
network prune |
Remove unused ones |