Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.

Dockerize an API based Flask App and Redis using Docker Desktop

2 min read

If you’re a developer looking out for a lightweight but robust framework that can help you in web development with fewer lines of code, then Flask is the right tool for you. Flask is a simple, light-weight framework popularly categorized as a micro framework.  Simple applications can be built with ease and do not require too much coding.  Flask apps requires much fewer lines of code for a simple task.

Flask comes with some standard functionalities and allows developers to add any number of libraries or plugins for an extension. If you have a simple, innovative use case to be added to an existing application, Flask should be your choice as it offers flexibility. Flask comes with a small set of easy to learn API, and the documentation is excellent. If you are new to Python, start your web development with Flask, so that you can get the feel of backend and frontend both as well as learn the core concepts well.

Compelling Features of Flask

  • Comes with a built-in development server and fast debugger
  • Easy and flexible configurations
  • Coherent and neat API
  • RESTful and HTTP request handling
  • Integrated Unit testing support

In this tutorial, we will see how to containerize API based Flask app with Redis and deploy it over Docker Desktop.


  • Install Docker Desktop

Visit https://www.docker.com/get-started/ to download Docker Desktop for Mac and install it in your system.

getting started with docker

Once the installation gets completed, click “About Docker Desktop” to verify the version of Docker running on your system.

about docker desktop pull down menu

If you follow the above steps, you will always find the latest version of Docker desktop installed on your system.

Docker Desktop Version 4.7 welcome

Create a project directory

We will be creating the following project files:

├── Dockerfile
├── README.md
├── app.py
├── docker-compose.yml
└── requirements.txt

Create a new directory called flask-redis.

mkdir flask-redis

Create app.py

Let’s add some code to handle simple web requests. Open this working directory in your favorite IDE and enter the following code into the app.py file.

from flask import Flask
from redis import Redis
app = Flask(__name__)
redis = Redis(host='redis', port=6379)
def hello():
    counter = str(redis.get('hits'),'utf-8')
    return "This webpage has been viewed "+counter+" time(s)"
if __name__ == "__main__":
    app.run(host="", debug=True)


Create requirements.txt

In Python, the requirement.txt file is a type of file that usually stores information about all the libraries, modules, and packages in itself that are used while developing a particular project. It also stores all files and packages on which that project is dependent or requires to run. 


Create a Dockerfile

In order to dockerize the flask app, we will require to create an empty file called “Dockerfile” and add the following content:

FROM python:3.11.0a6-alpine3.15
COPY requirements.txt /code
RUN pip install -r requirements.txt --no-cache-dir
COPY . /code
CMD python app.py

Create a Docker Compose file

     image: redislabs/redismod
       - '6379:6379' 
image: redislabs/redisinsight:latest
- '8001:8001'
        build: .
            - "5000:5000"
            - .:/code
            - redis

Deploy with docker-compose

$ docker-compose up -d
[+] Running 24/24
 ⠿ redis Pulled   
   ⠿ 565225d89260 Pull complete                                                                                                                                                                                                      
[+] Building 12.7s (10/10) FINISHED
 => [internal] load build definition from Dockerfile                                                                                                                                                                                  ...
[+] Running 3/3
 ⠿ Network flask-redis_default    Created                                                                                                                                                                                             
 ⠿ Container flask-redis-redis-1  Started                                                                                                                                                                                             
 ⠿ Container flask-redis-web-1    Started

Expected result

Listing containers must show one container running and the port mapping as below:

$ docker-compose ps
NAME                  COMMAND                  SERVICE             STATUS              PORTS
flask-redis-redis-1   "redis-server --load…"   redis               running   >6379/tcp
flask-redis-web-1     "/bin/sh -c 'python …"   web                 running   >5000/tcp

After the application starts, navigate to http://localhost:5000 in your web browser or run:

$ curl localhost:5000
This webpage has been viewed 2 time(s)

Monitoring Redis keys

Connect to the Redis database by using redis-cli command and monitor the keys.

redis-cli -p 6379> monitor 
1646634062.732496 [0] "INCRBY" "hits" "1" 
1646634062.735669 [0] "GET" "hits"

Visualizing the Redis Keys using RedisInsight

RedisInsight is a visual tool that lets you do both GUI- and CLI-based interactions with your Redis database, and so much more when developing your Redis based application. It is a fully-featured pure Desktop GUI client that provides capabilities to design, develop and optimize your Redis application. It works with any cloud provider as long as you run it on a host with network access to your cloud-based Redis server. It makes it easy to discover cloud databases and configure connection details with a single click. It allows you to automatically add Redis Enterprise Software and Redis Enterprise Cloud databases.

Running Redis GUI using Docker


docker run -d -v redisinsight:/db -p 8001:8001 redislabs/redisinsight:latest

Open http://localhost:8001 to access RedisInsight.  Supply “localhost” as a database URL, 6379 as port number and database name of your choice.

Stop and remove the containers

docker-compose down


  1. Docker Awesome Compose
  2. Build Your Python Image
  3. How to build and run a Python app in a container

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Docker Captain, ARM Innovator & Docker Bangalore Community Leader.