googleads
How to Debug A Python Flask Application In A Docker Container?
loader image
How to debug your Python app in docker
Python Web Development

How To Debug Your Python Flask App In Docker Container

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 “app.py” file and “requriments.txt” for install dependency packages. 

docker-compose.yml

  • 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. 

  • Volumes – Named volumes,  – volumename:path/to/volume
  • And enable the feature for debugging  stdin_open,tty

app.py

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.

requirements.txt

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

Table of Contents hide

Dockerfile

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

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.

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

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 app.py, 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
The following two tabs change content below.
Rajasekar

Rajasekar

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.

Leave a Reply

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