NashTech Insights

How to Containerize a Flask Application with Redis using Docker Compose

Mohd Muzakkir Saifi
Mohd Muzakkir Saifi
Table of Contents
black background with text overlay screengrab

Hello Learners, I am back with another blog. In this blog, We will learn how to containerize a Flask application with Redis using Docker Compose. If you like my blogs, you can read more blogs here.


Flask is a lightweight and popular web framework for building web applications in Python. It is known for its simplicity and flexibility, making it a popular choice for small to medium-sized projects.

Flask provides features such as URL routing, request handling, template rendering, and session management. It also supports extensions that add functionality, such as database integration, authentication, and API development.

Docker Compose:-

Docker Compose is a tool provided by Docker that simplifies the management of multi-container Docker applications. It allows you to define and run multi-container applications using a YAML file called a “docker-compose.yml” file.

Docker Compose enables you to define the services, networks, and volumes required for your application’s containers in a declarative way.

Using the docker-compose.yml file, you can define multiple services that make up your application, such as web servers, databases, and cache servers.


Redis is an open-source, in-memory data structure store known for its high performance and versatility. It can serve as a database, cache, and message broker. Redis stores data primarily in memory, allowing for fast read and write operations. Redis is commonly used for caching, pub/sub messaging, and in distributed systems.

Flask Application:-

In this blog, We will build a Flask application that implements Redis. Basically, we are using Redis for caching. In this blog, We will test our Flask application locally then containerize that application and run it using the Docker-Compose. Docker Compose will run the two containers as one application. We will require a few things like – Python installed, Docker, and One IDE [ I am using VS Code]. Let’s get started:-

To build this flask application, there are a few packages that we need to install before the building application. I am assuming that you have installed Python in your system and now we are going to install Flask and Redis using the following pip commands in your terminal:-

pip3 install flask
pip3 install redis

After the installation, we will create our flask application. For building the application, First, create a folder as flask_application and then inside this folder, you need to create a file and paste the below code:-

from flask import Flask
from redis import Redis

app = Flask(__name__)
redis = Redis(host='localhost', port=6379)

def count():
    number = redis.get('hits').decode('utf-8')
    return "Hey, Welcome to my page!!!! Your visting count " + number + " time(s)"

if __name__ == "__main__":"", debug=True)

This above application will use Redis for the caching purpose and provide a fast result. Now we will run this application using the below command:-


This command will start the application and print the logs like this:-

You can access this application at localhost:5000. You can hit this URL in your browser.

Now we will create a new file requirements.txt and this file contains the requirement to run or build this application. You can paste the below lines in this file:


Docker file for the Flask Application:-

We will create a docker file which contains all the dependency for this application and then we will create a docker-compose file to create the service and use this docker file in it:

FROM python:3.10-alpine

WORKDIR /flask-app

COPY requirements.txt /flask-app

RUN pip install -r requirements.txt --no-cache-dir

COPY . /flask-app

CMD python3

Now, we will create the docker-compose file. So first create the docker-compose.yaml file and and add the below code in it:-

version: '3'
    image: redis:latest
    container_name: redis-blog
      - '6379:6379'
      context: .
      dockerfile: Dockerfile
    container_name: flask-blog
      - "5000:5000"
      - .:/flask-app
      - redis
    image: my-flask-app:1.0

Now run the below command to up the docker container:

docker-compose up

You can access your application here localhost:5000


Basically, this blog shows us the process of containerizing a Redis Flask application using Docker Compose. It begins by developing a Python Flask application with Redis integration for caching. Next, a Dockerfile is created and then the docker-compose.yml file is defined and configured the application’s containers as individual services. Finally, the application is run by running the “docker-compose up”. You can access your application at or localhost:5000. If you like my blog then you can like this or want to read more blogs then follow this link.

Mohd Muzakkir Saifi

Mohd Muzakkir Saifi

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: