Start a Project

Understanding Communication Between Docker Containers

understanding-communication-between-docker-containers

In our earlier blogs, we have seen various architectures of docker containers to deploy the Magento 2 server.

We have seen how to set up a single container architecture that included a web server and database server inside the single container using Dockerfile.

We have also seen multi-container architectures having different services being run on different containers.

These containers were interlinked with each other and were configured by a docker-compose tool which uses a single configuration file docker-compose.yml file to deploy all the containers.

In between these setups, we have used various terms like container linking, port exposure, volume mapping, etc, but we had never discussed how containers communicate with each other.

So let’s take a break from setting up docker architecture and understand communication between docker containers.

Docker Network

Docker utilizes three inbuilt networks that come with fresh docker installation. If you run,

you might see three networks:

Let’s take a brief overview of these networks

Apart from these built-in networks, there are also user-defined networks.

We can create our network and control which containers can communicate with each other, and also enable automatic DNS resolution of container names to IP addresses.

With the help of default network drivers being provided, we can create the following networks:

However, here we will keep our discussion limited to the default bridge network.

Bridge Network

As mentioned earlier, docker containers are attached to a bridge or docker0 network by default if no other network is mentioned.

Take note that all containers within the same bridge network can communicate with each other via IP addresses.

However, they cannot resolve container names so communication via container names is not possible.

To ensure communication via container names, these containers are needed to link with each other in one way or another.

Docker0 bridge allows port mapping and linking to allow communication among containers or communication between container and host.

Communication between containers is managed at an operating system level and it depends on two factors:

For communication between the host and containers, it is mandatory that iptables settings and port mapping are done correctly to transfer packets between the docker container and hosts.

Understanding Docker Containers Linking By Example:

Let us understand container linking and port exposure by an example. We will link apache2 and MySQL-server containers using docker command line tools.

First, we will need a mysql-server image,

Then we will launch a container from this image,

In the above command, the -p arguments map the host’s port with the container’s port, and the –name argument defines the container’s name.

We are mapping the 3306 port of the host with the 3306 port inside the container. All traffic to and from the MySQL server will be routed from these ports.

Make sure ports on the host are available to be used.

After image and docker container creation, we can check their status as,

Now, lets pull apache2 server with php5 image as,

Now that we have the MySQL container already running, we will launch the apache2 container being linked to the MySQL container as,

In the above command, we have launched a container named apache2 whose port 80 is mapped with the 80 port of the host, and this container is linked with our database container named mysql by –link

Check the networks of both containers as:

You will see the default bridge network for both containers.

We can check this connectivity by,

and,

If we get a response for both the pings, then the docker containers have been linked properly.

Conclusion

So far we have seen how to link to docker containers within the default bridge network from simple command-line docker commands.

In our later blogs, we will discuss more user-defined networks and container communication.

Need Support?

Thank You for reading this Blog!

For further more interesting blogs, keep in touch with us. If you need any kind of support, simply raise a ticket at https://webkul.uvdesk.com/en/.

For further help or queries, please contact us or raise a ticket.

Exit mobile version