@app.on_event('startup') does not appear to run in docker container - global created on startup should not be None #321
-
I am attempting to set up a motor AsyncIOMotorClient to talk to my mongo database. I'm using a docker-compose.yaml setup, which will be posted at the end of this issue. Here is the relevant main.py code snippet:
When later in my code, in another module database/validation.py, I run the following to validate a user's cryptographic master key. I get an error (posted after the code below):
The error:
This would suggest that @app.on_event('startup') isn't being run, as that should set the MONGO_CLIENT global to be the client connection in our settings. Am I doing something wrong, or is there an issue with the underlying docker container? The docker-compose.yaml file:
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
I have a similar setting and the exact same issue. I have a function that must be triggered via @app.on_event('startup'), but within the docker container, that does not happen. EDIT: I found this issue fastapi/fastapi#1480 and followed the discussion there and on the linked bug reports. |
Beta Was this translation helpful? Give feedback.
-
spent hours on the same issue wondering why my app was working locally when running without docker but not when deploying to test environment with docker. Any resolution for this issue ?? |
Beta Was this translation helpful? Give feedback.
-
Now that Uvicorn supports managing workers with Because of that, I deprecated this Docker image: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#-warning-you-probably-dont-need-this-docker-image That would probably solve most of these types of issues. 🤓 |
Beta Was this translation helpful? Give feedback.
Now that Uvicorn supports managing workers with
--workers
, including restarting dead ones, there's no need for Gunicorn. That also means that it's much simpler to build a Docker image from scratch now, I updated the docs to explain it.Because of that, I deprecated this Docker image: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#-warning-you-probably-dont-need-this-docker-image
That would probably solve most of these types of issues. 🤓