Connection failed between docker containers
Ping failed between two Docker containers hosted on the same default
bridge network
“By default, Docker creates all containers on the default bridge
network unless explicitly mention different Network driver”
Let us check the basic functionality by creating 2 containers and try the ping module to check the connectivity.
Check the available Networks
$
docker network list
Create 2 containers, here I have taken an alpine image as an example.
$ docker run -d --name
alpine1 alpine sleep 3600
$ docker run -d --name alpine1 alpine sleep 3600
$ docker run -d --name alpine1 alpine sleep 3600
Now check the Network settings and get the assigned Ip for each of the containers
[osboxes@master docker]$ docker inspect
alpine1
[osboxes@master
docker]$ docker inspect alpine2
Note the Ips for ping test:
Now try the ping test using IP first and then follow up with Container Names.
$ docker exec {container id/name} ping {IP address}
Now we have reason to use the custom bridge to isolate our
resources as well as overcome the previous scenario. Let us dive in….
We need to create a custom network first.
“list the existing default networks
comes with Docker”
$ docker network list
“ Now create the network name ‘xploritec’
”
$
docker network create --driver=bridge xploritec
Let us create the containers again but using our custom network.
$
docker run -d --name alpine1 --network xploritec alpine sleep 3600
$ docker run -d --name alpine2 --network xploritec alpine sleep 3600
$ docker run -d --name alpine2 --network xploritec alpine sleep 3600
Repeat the same steps as above.
$ docker inspect alpine1
$ docker inspect alpine2
Finally, test the Ping with both IP address and
container name, this time assigned IP addresses are different as we used custom
bridge network.
$ docker exec
-it alpine1 ping 172.18.0.3
$ docker exec -it alpine2 ping 172.18.0.2
$ docker exec -it alpine1 ping alpine2
$ docker exec -it alpine2 ping alpine1
$ docker exec -it alpine2 ping 172.18.0.2
$ docker exec -it alpine1 ping alpine2
$ docker exec -it alpine2 ping alpine1
Conclusion: This approach is used when there is a need of addressing or connecting the other services with the name of the container.
Comments
Post a Comment