question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using pdb inside a uvicorn running in docker fails, while it doesn't outside docker.

See original GitHub issue

Hey, I originally reported this issue in the fastapi repository and was informed it would be better suited here. This is the thread for anyone interested.

Summary of the thread

When running uvicorn inside a tiangolo/uvicorn-gunicorn:python3.7 docker image with reload=True set in uvicorn.run, debugging with pdb or any of the similar debuggers is not possible. The set_trace() commands trigger a bdb.BdbQuit exception instantly. I reduced the bug to those particular conditions after trying to reproduce the error in several environments as detailed in the linked thread. This is a snippet to reproduce the bug:

from fastapi import FastAPI
from starlette.requests import Request

app = FastAPI()


@app.get("/")
def read_root():
    return {"Hello": "World"}


@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
    import pdb; pdb.set_trace()
    return await call_next(request)


if __name__ == "__main__":
    import uvicorn
    uvicorn.run('script_file_name:app', host="0.0.0.0", port=80, reload=True, debug=True)

To run it just:

docker run -v /local_folder_with_script_above/:/test/ -p 80:80 tiangolo/uvicorn-gunicorn:python3.7

Then run in another terminal:

docker exec -it {container_name} bash

Finally execute the script using python in that second terminal

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
gvbgduhcommented, Nov 25, 2019

@joaqo yes, some sort of traceback would help. To be honest I find your approach a bit confusing. The docker image you provided with that command starts its own server, while you want to spin up another one programatically. Also, while it’s a bit tough for me to justify not to use the entrypoint outside the script as it makes it more convenient for deploys across different environments I tried running it programmatically:

...

if __name__ == "__main__":
    import uvicorn
    uvicorn.run('app:app', host="0.0.0.0", port=8080, reload=True, debug=True)
...
command: python app/app.py
...

and it seems to be working fine with auto reload.

As of that it seems the problem not in uvicorn and not in communicating with docker. So, I would probably recommend to try the plain setup first and/or debug the docker file you use and how it builds the environment.

1reaction
joaqocommented, Nov 25, 2019

@gvbgduh very good suggestion. I had assumed that the docker image I was using was the canonical uvicorn docker image (I don’t think there is one atm), so I didn’t suspect it, but it turns out I was wrong. Tried just using the python:3 image and just pip installed what I needed inside docker, and my script ran just fine. So I think it was related to the docker image I was using. Will do some further research to try and see what was wrong, and report here if I find anything interesting, but for now it seems that this was my fault and not not uvicorn’s.

Thanks again @gvbgduh & @tomchristie .

Read more comments on GitHub >

github_iconTop Results From Across the Web

docker run won't work but docker compose work fine
In docker-compose you are overriding the CMD with correct way of exposing the host and port to outside world. In your Dockerfile change...
Read more >
FastApi on uvicorn works good but fails to work with Docker ...
Hello there! I have a FastAPI app that works well when I launch it as a script. Unfortunately, I can't get access to...
Read more >
Debug Python within a container - Visual Studio Code
How to configure and troubleshoot debugging of Python apps running in a Docker container, using Visual Studio Code.
Read more >
Debugging - FastAPI
You can connect the debugger in your editor, for example with Visual Studio Code or PyCharm. Call uvicorn ¶. In your FastAPI application,...
Read more >
How to debug Python scripts and API code in the console and ...
To use pdb for debugging, we need to set some breakpoints in the code. ... to debug API code running inside a Docker...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found