How to create a Docker bridge network?
· Category: Docker
Short answer
Use docker network create to create a custom bridge network. Containers attached to this network can communicate by container name and are isolated from containers on other networks.
Steps
- Create the network:
docker network create <name>. - Attach a container during creation with
--network. - Connect existing containers with
docker network connect. - Verify with
docker network inspect.
Example
docker network create backend
docker run -d --name db --network backend postgres:15
docker run -d --name api --network backend -p 8080:8080 myapp
Connect an existing container:
docker network connect backend mycontainer
Tips
- Use custom bridge networks instead of the default bridge for automatic DNS resolution.
- You can specify subnets, gateways, and IP ranges:
docker network create --subnet=172.20.0.0/16 --gateway=172.20.0.1 mynet
- Use network aliases for service discovery:
docker run --network backend --network-alias database postgres
Common issues
- Containers on the default bridge cannot resolve each other by name without
--link. - Port mappings work on the host interface, not between containers on the same network.
- Overlapping subnets can cause routing conflicts.