Docker Custom Bridge Networks
Create custom bridge networks so containers can communicate by name in a cleaner way.
Custom bridge networks are one of the most useful Docker networking features for beginners.
They let related containers communicate with each other by container name. This makes multi-container setups cleaner than using the default bridge network.
Create a custom network
docker network create app-networkCheck it:
docker network lsRun containers on the network
Start a database container:
docker run -d --name database --network app-network mysql:8Start an app container on the same network:
docker run -d --name api --network app-network my-api:1.0Now api can reach the database using the hostname:
databaseDocker provides name resolution inside the custom network.
Why custom networks are better
Custom bridge networks are better for app setups because:
- Containers can find each other by name
- Related containers are grouped together
- Network setup is easier to inspect
- You avoid hard-coding container IP addresses
- It feels closer to Docker Compose and production-style setups
Inspect a network
docker network inspect app-networkThis shows containers connected to the network and their assigned IP addresses.
Connect an existing container
You can connect a running container to a network:
docker network connect app-network container-nameAnd disconnect it:
docker network disconnect app-network container-nameRemove a network
After removing containers, remove the network:
docker network rm app-networkDocker will not remove a network if containers are still attached.
Simple mental model
Use a custom bridge network when containers belong to the same application.
app-network
-> api container
-> database container
-> cache containerFor multi-container practice, create a custom bridge network and use container names instead of IP addresses.
How is this guide?
Last updated on
