Custom Bridge Networks
While Docker provides a default bridge network, creating custom bridge networks offers better isolation and control over inter-container communication.
- First, let's see the current list of Docker networks:
docker network ls
You should see something like this:
NETWORK ID NAME DRIVER SCOPE
296d1b460b17 bridge bridge local
91199fc6ad2e host host local
1078d2c781b6 none null local
- Now, let's create a custom bridge network:
docker network create my-custom-bridge
- List all Docker networks again to verify the creation:
docker network ls
You should now see my-custom-bridge
in the output:
NETWORK ID NAME DRIVER SCOPE
296d1b460b17 bridge bridge local
91199fc6ad2e host host local
7215f99d0080 my-custom-bridge bridge local
1078d2c781b6 none null local
- Launch two containers on the custom bridge network and install ping:
docker run --network=my-custom-bridge --name container1 -d nginx
docker run --network=my-custom-bridge --name container2 -d nginx
Now, let's install the ping utility in both containers. We need to do this because the default Nginx image doesn't include ping:
docker exec container1 apt-get update && docker exec container1 apt-get install -y iputils-ping
docker exec container2 apt-get update && docker exec container2 apt-get install -y iputils-ping
- Test the communication between the containers:
docker exec container1 ping -c 4 container2
You should see successful ping responses, demonstrating that containers on the same custom bridge network can communicate using container names. This works because Docker's built-in DNS resolves container names to their IP addresses within the same network.
If you don't see successful pings, make sure both containers are running (docker ps
) and that you've correctly installed the ping utility.