Building your own bridge for container communication
As we already know, when the Docker daemon starts, it creates a bridge called docker0
, and all the containers will get the IP from it. Sometimes, we might want to use a different bridge for some of our containers. Let's see how we can do that in this recipe.
Getting ready
I am assuming you already have Docker set up.
How to do it...
Follow these steps:
- Create a new custom bridge with the name
br0
:
$ docker network create br0 --subnet 192.168.2.1/24
- Confirm that the network has been created:
$ docker network ls

- Start a container using the new network and make sure that it is using the correct subnet:
$ docker container run -d --network br0 --name br0demo redis $ docker container inspect br0demo

How it works...
The preceding steps will create a new bridge and it will assign the IP from the 192.168.2.0
subnet to any of the containers that are assigned to that network.
There's more...
If you no longer need the network, you...