Make a conversation with Docker containers
We have some times playing around the Docker container as standalone app. How can we manage to group two of them or more and allow them to communicate together?
A network between Docker container is called “Bridge”. Bridge can be one of the followings:
- Default bridge
This is the out-of-the-box bridge. Active containers will be automatically added into the default bridge. We can use a local IP of a container inside this bridge. - User-defined bridge
Its benefits are letting us refer other containers in the bridge by their names (not only IP addresses) and allowing to control which containers do connect to this bridge. So this bridge type has more secure.
Bridge information
We can verify IP addresses and connected bridges of a containers by this command.
docker inspect -f '{{json .NetworkSettings.Networks}}' container_name | json_pp
Apply -f
to format the output with '{{json .NetworkSettings.Networks}}'
for JSON format then find the value at the jsonpath $.NetworkSettings.Networks
. Finally apply json_pp
(JSON Pretty Print) for JSON formatting output. Here is an example output.
The “bridge” is name of the default bridge. Now we know that this container is “172.17.0.4” in the default bridge. We can ping this IP when we are in this bridge.
User-defined bridge
It is the time we can build our bridges with this command.
docker network create bridge_name
And list all bridges.
docker network list
So we run this to add or remove a container in the bridges.
docker network connect bridge_name container_name
docker network disconnect bridge_name container_name
After all, run this to show a bridge’s information.
docker network inspect bridge_name
Here is the sample result.
As we mentioned before, the container can be identified by its name since it is a user-defined bridge. We try to ping “cent01” container.
At the last, remove the bridge after use with the command.
docker network rm bridge_name
The bridge is useful for us if we want to develop multiple containers and require a communication system between them.
References: