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,
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.
command: /usr/local/bin/gunicorn -w 2 -t 3600 -b :8000 app:app --reload
- 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
app = Flask(__name__)
test = "Welcome **** "
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.
RUN mkdir -p /usr/src/app
COPY dockerize/requirements.txt /usr/src/app
RUN pip install --no-cache-dir -r requirements.txt
To install Docker in Ubuntu, click here.
#Update the apt package index:
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
#Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
#Install the Docker Repository
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(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
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
Let’s see the output in the browser.
In terminal after press ‘c’ and return (test = “Welcome ****”) as it is in app.py, pdb to trace it.
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.
The output in the browser will look like this,
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.
Latest posts by Rajasekar (see all)
- How To Debug Your Python Flask App In Docker Container - December 10, 2019
- How To Use Python Lambda Functions With Examples - November 13, 2019
- Introduction To Deep Learning (Neural Network) And Its Mathematical Computations - September 10, 2019