At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

How To Debug Your Python Flask App In Docker Container

  • By Rajasekar
  • December 10, 2019

Debugging Python applications in the Docker containers. In this blog, we kick start with pdb, Flask, Gunicorn, to dissect the process of debugging.

Docker is a container packaging system that uses virtualization package and isolates application parts from your system, making it easier to configure virtual machines for use in app development, testing, and deployment inside the container.
Let’s get started with Debugging the Python Flask app inside the Docker container.
For that, you should create the folder structure. Your folder structure should be like this,
Python docker container
After you recall the above structure and script given below, You should create the base directory. In my case, I’m creating “design”, inside of this to create a flask app “dockerapp” and composer file “docker-compose.yml” to demonstrate here. Create the docker file inside the flask app as “Dockerfile” and define flask application as “” file and “requriments.txt” for install dependency packages.

version: '3'
            context: .
            dockerfile: dockerapp/Dockerfile
            - "8000:8000"
        command: /usr/local/bin/gunicorn -w 2 -t 3600 -b :8000 app:app --reload
            - ./dockerapp:/usr/src/app
        stdin_open: true
        tty: true
  • Version – To specify compose file format versions. Here we are using version 3
  • Services – A service contains a configuration that is applied to each container for initializing service
  • Context – Path to a directory containing a Dockerfile
  • Dockerfile – Compose uses an alternate file to build. (Docekapp/Dockerfile)
  • Ports – Specify both ports (HOST:CONTAINER), or just the container port
  • Command – Override the default command

We are using Gunicorn to restart –reload feature. To make modifications done in our app.
So, we have to attach the Gunicorn process with 8000 port.

#/usr/local/bin/gunicorn -w 2 -t 3600 -b :8000 app:app --reload
  • Volumes – Named volumes, – volumename:path/to/volume
  • And enable the feature for debugging stdin_open,tty

from flask import Flask
import pdb
app = Flask(__name__)
def index():
    test = "Welcome **** "
    return test
if __name__ == "__main__":
    app.secret_key = "qwertyuiop1234567890!@#$%^&*()ZXCVBNM<>?ASDFGHJK"

Here we created a simple hello world flask app with an inbuilt debugger pdb module. And mention the pdb.set_trace() for a breakpoint in the application, it will stop at the point and look for the debug command.


For the above code and docker-compose file, we use Gunicorn and flask, To start working, we have to install this in our docker.


FROM python:3
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY dockerize/requirements.txt /usr/src/app
RUN pip install --no-cache-dir -r requirements.txt

Docker file is quite easier to understand its like formal English from (where), run (command), workdir (work directory when we enter into the container), copy (copy from local system to our container). Here is a Link for your reference.
docker container
To install Docker in Ubuntu, click here.

Terminal Command

#Update the apt package index:
sudo apt-get update
#depended packages:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
#Add Docker’s official GPG key:
curl -fsSL | sudo apt-key add -
#Install the Docker Repository
sudo add-apt-repository "deb [arch=amd64]  $(lsb_release -cs) stable"
#Install Latest Version of Docker
sudo apt-get install docker-ce
sudo apt-get install docker-compose

Build and Debug

Now it’s time to build the container. the command for build docker-compose up –build, But we want to run services in the background, -d flag will help (for “detached” mode)
Open a terminal with the base directory and type the below command to build.

sudo docker-compose up -d --build

docker container
In this case, docker-compose used the cache data if you are doing this for the first attempt it will take a while to load. To attach the debugger into the container, use the below command

sudo docker container ps
sudo docker attach f0b404dfefd5

docker python flask appLet’s see the output in the browser.
http://localhost:8000/ – Enter this URL into the browser its looking for the debug command in the terminal, for usual cmd ‘c’ for continue.
python flask docker container
In terminal after press ‘c’ and return (test = “Welcome ****”) as it is in, pdb to trace it.
python flask in docker output
And if you want to edit or do whatever you want with this here we just resign the variable “test” to “Hi raj”
Refresh your URL once again hit the breakpoint, in the terminal.
docker Command
The output in the browser will look like this,
python app docker
I hope this quick tutorial will help you debugging the python application inside the Docker container. And to configure docker with a simple debugger pdb.
Looking for a Python developer for your project? Hire a Python developer with a high technical skillset and get started with your development that unfolds your business potential.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions


A Senior Software developer. Specialist in Python, Django, Flask, PostgreSQL, and AWS. Currently working on Machine Learning, Raspberry Pi and NLP technologies. This ecstatic techie is always full of ideas that make life easier with machines. He is also a biker by passion.