Let’s See How The Communication Between Docker Containers Is Done

Reading Time: 4 minutes

Now let’s understand communication between docker containers.

Containers

Containers are a form of operating system virtualization. A single container might be used to run anything from a small microservice or software process to a larger application. Inside a container are all the necessary executables, binary code, libraries, and configuration files.

How do containers communicate?

Containers have a level of isolation from the environment around them, they often need to communicate with each other, and the outside world.

Two containers can talk to each other in one of two ways:-


Communicating through networking:

Containers are designed to be isolated. But they can send and receive requests to other applications, using networking. For example, a web server container might expose a port so that it can receive requests on port 80. Or an application container might make a connection to a database container.

Sharing files on disk: 

Some applications communicate by reading and writing files. These kinds of applications can communicate by writing their files into a volume, which can also be with other containers. For example, a data processing application might write a file to a shared volume that contains customer data. Or, two identical containers might even share the same files.

Communication between containers with networking

Most container-based applications talk to each other using networking. This basically means that an application running in one container will create a network connection to a port on another container.

For example, an application might call a REST or GraphQL API, or open a connection to a database.

Containers are ideal for applications or processes which expose some sort of network service. The most well-known examples of these kinds of applications are:

  • Web servers – e.g. Nginx, Apache
  • Backend applications and APIs – e.g. Node, Python, JBoss, Wildfly, Spring Boot
  • Databases and data stores – e.g. MongoDB, PostgreSQL

Building your (Virtual) Network

A Docker network lets your containers communicate with each other. If you are running more than one container, you can let your containers communicate with each other by attaching them to the same network.

Docker creates virtual networks which let your containers talk to each other. In a network, a container has an IP address, and optionally a hostname.

You can create different types of networks depending on what you would like to do. We’ll cover the easiest options:

  • The default bridge network, which allows simple container-to-container communication by IP address, and is created by default.
  • user-defined bridge network, which you create yourself, and allows your containers to communicate with each other, by using their container name as a hostname.

Default bridge network

The simplest network in Docker is the bridge network. It’s also Docker’s default networking driver. In a bridge network, each container is assigned its own IP address. So containers can communicate with each other by IP.

How to use the default bridge network

  1. Check that the bridge network is running: You can check it’s running by typing docker network ls.

2. Start your containers: Start your containers as normal, with docker run. When you start each container, Docker will add it to the bridge network.

3. Address another container by its IP address: Now one container can talk to another, by using its IP address. You’ll need to know the IP address of the container – check the little box below to find out how.


$ docker run --rm --name mynginx --detach nginx
$ docker inspect mynginx | grep IPAddress
            "IPAddress": "172.17.0.2",

$ sudo docker inspect mynginx | jq '.[].NetworkSettings.Networks.bridge.IPAddress'
"172.17.0.2"

$ docker run -it busybox sh

busybox$ wget -q -O - 172.17.0.2:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>

User-defined bridge

To let Docker containers communicate with each other by name, you can create a user-defined bridge network. In a user-defined bridge network, you can be more explicit about who joins the network, and you get an added bonus.
In a user-defined bridge network, you control which containers are in the network, and they can address each other by hostname

How to create a user-defined bridge network

  1. Create a user-defined bridge network: Create your own custom bridge network first using docker network create. Under the hood, Docker sets up the relevant networking tables on your operating system.
  2. Start a container and connect it to the bridge: Start your container as normal. Add it to your user-defined bridge network using the --net option, e.g. --net tulip-net.
  3. Address another container, using its name as the hostname: When two containers are joined to the same user-defined bridge network, one container is able to address another by using its name (as the hostname).
$docker network create tulip-net

$docker run --rm --net tulip-net --name tulipnginx -d nginx 

$ docker run --net tulip-net -it busybox sh

busybox$ wget -q -O - tulipnginx:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...you get the picture....

Conclusion

We have seen how containers communicate with each other. For containers to communicate with others, they need to be part of the same “network”.Docker creates a virtual network called bridge by default and connects your containers to it. If you want more control (and you definitely do), you can create a user-defined bridge, which will give you the added benefit of hostnames for your containers too.

Refrences

TutorialWorks

Edureka

Written by 

Deeksha Tripathi is a Software Intern at Knoldus Inc Software. She has a keen interest toward learning new technologies. Her practice area is Devops. When not working, she will be busy in listening music , and spending time with her family .

Leave a Reply