Debugging Flask under gunicorn in docker container

Answered

Hi,

My projects is starting up multiple containers with docker-compose the flask app is running under gunicorn, now is it possible to use pycharm debugger to set breakpoints and use rest of the IDE?

Currently the only way to debug the project is to run containers with docker-compose up command and use pdb.set_trace() to set a breakpoint.

 

10 comments

Hi Elizabeth

Thank you for your reply! Unfortunately it did not work since pycharm is throwing error message which state the following: "Failed to find free socket port". This is happening because i am trying to connect remote debugger to docker container which is up on my local machine and uses 5009 port. Could you please comment on this?

0

Streak Flash Are you using Remote Interpreter or Remote Debug Server?

1

I am using local virtual environment and a  remote debug server. i've put the .egg file in my docker container and included required library in my python file. When i hit the debug button debugger starts up and waits for connection to my given port. but then i am unable to start the docker container since the port is locked by pychamr debugger

0

Hi Streak Flash! Is it possible to access host from inside your container? Could you please provide more details about network and port configuration you made? Doesn't selecting another port for Remote Debug Server work? I made a quick setup on mac (btw what is your OS?):

  • pydevd.settrace('docker.for.mac.localhost', port=1234, stdoutToServer=True, stderrToServer=True) inside a Python script
  • interpreter based on the python:latest image (no specific port or network configuration made)
  • run Remote Debug Server
  • run Python script in PyCharm
  • successful connection
0

Hi Pavel,

Sorry for late response!

I am also running MacOS, please see below a part of my Dockerfile. 


COPY pycharm-debug-py3k.egg /app
EXPOSE 80
WORKDIR /app
# Execute the app
ENTRYPOINT ["/usr/local/bin/newrelic-admin", "run-program"]
CMD ["/usr/local/bin/gunicorn", "app:application", "-b", "0.0.0.0:80", "--reload"]


And a slice from docker-compose.yml

services:
my-service:
build: .
image: my-service
ports:
- "5009:80"
volumes:
- .:/app
environment:
...
links:
- mongo
networks:
- default
- my_network

networks:
my_network:
external: true
0

I have figured this out!

 

I just had to set "docker-compose" as remote interpreter for my project and then create a python runnable where Script path="/usr/local/bin/gunicorn" and Script parameters="app:application -t 0 <and so on>"

 

1

Streak Flash,

Could you provide your complete config you ended up with? I am running into a similar issue and would like to see if I can reproduce what you did when you got it working.

Sincerely,

Robert

0

Robert - Here is complete flow to set up the debugger

1. Pycharm -> Preferences -> Build, Execution, Deployment -> Docker
* Press + (Add)
* Connect to Docker daemon with: Docker for Mac
* Navigate to Docker -> Tools
* Docker Machine executable: `/usr/local/bin/docker-machine`
* Docker Compose executable: `/usr/local/bin/docker-compose`
2. Navigate to Preferences -> Project -> Project Interpreter
3. Locate “gear” icon -> Add remote -> Docker Compose
1. Server: Choose your previously created Docker server
2. Configuration file: Locate your project’s `docker-compose.yml` file
3. Service: Set your main service here
4. If not set, select newly created Remote Python interpreter in “Project Interpreter” field.
5. Run -> Edit Configurations - Add -> Python
6. Setup appropriate fields as follows
* Script path: `/usr/local/bin/gunicorn`
* Parameters: `app:application -b 0.0.0.0:80 -t 0`
7. Press OK button
8. Now you should be able to use PyCharm debugger

2

Streak Flash--

Thank you for taking the time to get me that info. I will take a look and see if I can get that to to work on my box today!

Sincerely,

Robert

0

Please sign in to leave a comment.