Fastapi: [QUESTION] Workflow with docker-compose in pycharm

Created on 10 Apr 2019  路  7Comments  路  Source: tiangolo/fastapi

How should I set up the run command so the debugger works, when the interpreter is in a docker image?

question

Most helpful comment

As i'm currently not using jango, the part for defining a cli command didn't work:

@cli.command()
def rundev():
    uvicorn.run("app.app:app", host="0.0.0.0", logger=logging.getLogger("uvicorn"), reload=True)

Debugging did also not work with starting the uvicorn server from the docker-compose.yml
entrypoint: "start-reload.sh"
or something like
command: ["uvicorn", "main:app", "--host", "0.0.0.0", "--debug", "--port", "80", "--reload-dir", "/app"]
(source)

For me debug was only working when doing it as explained above by euri10, but in manage.py:

import uvicorn

if __name__ == '__main__':
    uvicorn.run("app.main:app",
                host="0.0.0.0",
                port=80,
                log_level="debug",
                reload=True,
                reload_dirs=["/app"])

Then (if your mappings are correct) run manage.py in debug mode.

All 7 comments

you have loads of possibilities depending of what you want to achieve, but overall you need for all of those to have your python interpreter set to the python inside docker, here's an example, you setup that in Settings > Project > Project Interpreter > + > Docker-Compose

Imgur

Then you may need to setup the path mappings if breakpoint don't get hit

Imgur

then you may have for instance a manage.py that runs uvicorn that way (reload is important):

@cli.command()
def rundev():
    uvicorn.run("app.app:app", host="0.0.0.0", logger=logging.getLogger("uvicorn"), reload=True)

and you hit the debug button running the rundev command of manage.py script :

Imgur

So the main trick is setting up the manage.py file. Maybe this should be specified in the documentation, and a minimal manage.py file could be included.

I am able to run django in docker compose, but not successful running fastapi. I suppose some path mappings might be missing, but none were necessary for django. I am getting on run or debug.

Aborting on container exit...

Process finished with exit code 0

So the main trick is setting up the manage.py file. Maybe this should be specified in the documentation, and a minimal manage.py file could be included.

not at all, this is just one of the endless possibilities, it's just an example, you may run it trough you __main__ function, you may run it through another script, your may run it through uvicorn directly...as I said the most important is getting your interpreter right as described above

I am able to run django in docker compose, but not successful running fastapi. I suppose some path mappings might be missing, but none were necessary for django. I am getting on run or debug.

Aborting on container exit...

Process finished with exit code 0

maybe a more detailed explanation of what you're doing would be a good the first step in getting help, not clear at all with the info provided

Thanks for your help @euri10 !

@Euphorbium for a ready-made project generator with Docker integrated, check: https://github.com/tiangolo/full-stack-fastapi-postgresql/

How are you connecting your debugger to Django inside of the container?

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

As i'm currently not using jango, the part for defining a cli command didn't work:

@cli.command()
def rundev():
    uvicorn.run("app.app:app", host="0.0.0.0", logger=logging.getLogger("uvicorn"), reload=True)

Debugging did also not work with starting the uvicorn server from the docker-compose.yml
entrypoint: "start-reload.sh"
or something like
command: ["uvicorn", "main:app", "--host", "0.0.0.0", "--debug", "--port", "80", "--reload-dir", "/app"]
(source)

For me debug was only working when doing it as explained above by euri10, but in manage.py:

import uvicorn

if __name__ == '__main__':
    uvicorn.run("app.main:app",
                host="0.0.0.0",
                port=80,
                log_level="debug",
                reload=True,
                reload_dirs=["/app"])

Then (if your mappings are correct) run manage.py in debug mode.

Was this page helpful?
0 / 5 - 0 ratings