How messages get exchanged between Docker containers

Now-a-days Since almost all applications employ containers, it goes without saying that they will need to communicate with one another. In this article, we will cover how messages may be easily sent and received between containers.

I’m assuming that your environment already has Docker installed. Next, we’ll build two apps with flask and node js and attempt to communicate between them.

Let’s start with a Python app that uses Flask.

Create a folder called “ping-app” and create app.py file, place the following code in it.

We can see from the code above that Redis is being used as a key-value pair database. and on the root route, we are returning a message with the count to print on the screen while also increasing the number of pageviews in redis by 1.

Since this application uses two packages, let’s make a requirements.txt file and include the following dependencies:

Now Create a file called Dockerfile and add the following code to begin dockerizing this application:

For the next step, we will need a docker-compose file with the following code in order to properly execute this application: Create a docker-compose.yaml file and add the following code:

In this code we can see that we have 2 services, 1 is web which is basically using our docker file to run our already created flask app and another service is redis, which basically runs on 6379 port by default. So lets run the app now using following command in your workspace terminal.

Your redis and web services will both become operational as a result.
Using http://localhost:5000, let’s open your flask application now on browser. and whoohoo, the results are displayed as shown.

Now Reload this app in your browser now a number of times to observe if the count increases each time the page loads.

Also Check out the other endpoint in the same app, http://localhost:5000/get-redis-count, which simply returns the screen count number value.

So far, everything has been rather simple. Let’s assume that you have another Node.js application that wishes to receive the pageview count.

let’s make that Node App as well utilising the techniques below:

Make a new folder called pong-app as a sibling to ping-app, and then place a package.json file containing the following information:

After this lets create app.js file parallel to package.json file with following code:

The code above is simply the beginning of a small express application. Hello Node, let’s first dockerize this application. thus make a Dockerfile file with the following information in it.

Lets create a docker-compose.yaml file for this app with following information:

You are now prepared for this app, docker-compose up can be used to start an application, which will then run on http://localhost:8000. and on the root route, it will output Hello Node.

Now that both apps are operating in separate containers, the easiest approach to display the number of pageviews for the Python app in the node js app is to access the /get-redis-count API on the existing Flask app.
Let’s create a function in Node.js to retrieve the pageview count from a Flask application using the existing Flask api.

We are attempting to call the departing endpoint /get-redis-count running inside the Flask app in the aforementioned code.


Also, if you’ve observed, we used web:5000 instead of localhost:5000 when creating the url for the flask app (http://web:5000/get-redis-count). Therefore, in order to access resources within containers using the http protocol, we must utilise the container service name defined in the compose file. The service name for this Flask app is web, which is why the url is web:5000.

Now let’s restart our node app and test whether we still obtain the desired results by accessing http://localhost:8000/get-data-from-ping-app.

Currently, the output message will read: Node App is unable to connect with Flask App.

But why? Because there is no networking between the two apps, which are running independently. Therefore, we need to make a few changes to both apps in order to create a network between them.

Just before services, add the network my-local-net with a driver bridge to the docker-compose.yaml file of both apps(ping & pong).

Add the following code to the services section of both docker-compose files to connect the created network to each service.

Let’s take a last glance at the docker-compose files:

Ping App’s docker-compose.yaml file(Flask based app)

Pong App’s docker-compose.yaml file(Node.js based app)

Restart both apps now, then try to access the following URL to view pageviews for the flask app in Node.js:

http://localhost:8000/get-data-from-ping-app

It will respond with something like

{“count”:x}

Note: that x is merely a stand-in for count, which will be an integer value.

Finally, since they are all declared under the same network, separate apps running in Docker containers can connect with one another using Network Driver Bridge.

From our download center, you can get the complete source code. Additionally, if you have any questions, please comment below. or email me on jeetendra.apps@gmail.com