3. Bridge network for pingme
A network bridge is a device that divides a network into segments. Each segment represent a separate collision domain, so the number of collisions on the network is reduced.
A quote from docs.docker.com
A bridge network uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network. The Docker bridge driver automatically installs rules in the host machine so that containers on different bridge networks cannot communicate directly with each other.
Bridge networks apply to containers running on the same Docker daemon host.
Bridge Networks in Docker
We will use this model from Understanding Docker Networking Drivers and their use cases
When you initially installed docker it created a bridge network called bridge
. You can see what networks are available if you type the command docker network ls
. All containers run using docker run
are by default assigned to this bridge network.
You can create your own bridge network using docker network create <network name>
e.g. docker network create mybridge
. Once you create a network you can assign containers to that network using the --network
switch when you create a new container
Try the following commands
docker network create test_network
docker run -dit --name pingtest --rm --network=test_network pingme
docker network inspect test_network
You should see something like this
Compare this to when you inspect the default bridge network bridge,
type docker network inspect bridge
.
So a bridge
network works as a private network internal to the host so containers on it can communicate. External access is granted by exposing ports to containers. In the picture above db
and web
can communicate with each other on a user-created bridge network called mybridge
.
Use a bridge network to isolate your containers. and when your applications run in standalone containers but need to communicate with other containers. The advantages of using user-defined bridges are
User-defined bridges provide better isolation.
Containers can be attached and detached from user-defined networks on the fly.
Each user-defined network creates a configurable bridge.
Linked containers on the default bridge network share environment variables.