Two Docker Container Communication Using Python and Redis— a Tiny Demonstration

Here is a super quick way to launch a Python based Docker Container application that talks to another Docker Container running a Redis Server using a custom bridge network.

Image for post
Image for post

Here is the Dockerfile

FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install --trusted-host pypi.python.org Flask Redis
EXPOSE 80
CMD ["python", "app.py"]

And here is the Python application app.py in the same folder (build context).

from flask import Flask
from redis import Redis, RedisError
import os
import socket
redis = Redis(host="redis-server", db=0, socket_connect_timeout=2, socket_timeout=2)app = Flask(__name__)@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis server to count</i>"
html = "<h3>Hello World!</h3>\n" \
"<b>Hostname:</b> {hostname}<br/>\n" \
"<b>Visits:</b> {visits}\n"
return html.format(hostname=socket.gethostname(), visits=visits)if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)

Note that the above application listens to port 80 which is also exposed in the Dockerfile and it expects the Redis Server’s hostname to be “redis-server” Let us test this.

First let us create a custom bridge network.

docker network create mynet

Then launch the Redis Server in the above network, remember to name this container “redis-server”.

docker run -d --rm --name redis-server --network mynet redis:alpine

Now let us build and run our Python application. You should be in the folder that contains the above Dockerfile and app.py files.

docker build -t temp .
docker run -d -p 8080:80 --rm --name pyapp --network mynet temp

Above assumes that port 8080 is free on the host machine. Let us now test this.

curl localhost:8080
<h3>Hello World!</h3><b>Hostname:</b> b3f2f3a731a6<br/><b>Visits:</b> 1
curl localhost:8080
<h3>Hello World!</h3><b>Hostname:</b> b3f2f3a731a6<br/><b>Visits:</b> 2

Notice that our app container is able to discover the IP address of the Redis Server using the host name “redis-server” as referred in the app.py

Instead of using a custom network, we could have also used the legacy — link option to link the app container to the Redis-Server container.

If you do not want to use a custom network or the deprecated link option, then you will need to run the Redis Server container with it port 6379 mapped to the host port and then use that in the app.py along with the hosts IP address.

Thanks for your time. Please follow me for more such tiny demo snippets!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store