📝 Add note in Docker docs about ensuring graceful shutdowns and lifespan events with CMD exec form#11960
Merged
Merged
Conversation
Contributor
|
📝 Docs preview for commit 44cd326 at: https://0268584d.fastapitiangolo.pages.dev |
Contributor
|
📝 Docs preview for commit 705cc85 at: https://a2c4ccae.fastapitiangolo.pages.dev Modified Pages |
CMD exec form
Member
|
Great, thank you @GPla! 🚀 I tweaked it a bit and made it its own section so we can link to it more easily. 🤓 |
black-redoc
pushed a commit
to black-redoc/fastapi
that referenced
this pull request
Sep 12, 2024
…pan events with `CMD` exec form (fastapi#11960) Co-authored-by: svlandeg <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>
black-redoc
pushed a commit
to black-redoc/fastapi
that referenced
this pull request
Sep 12, 2024
…pan events with `CMD` exec form (fastapi#11960) Co-authored-by: svlandeg <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>
black-redoc
pushed a commit
to black-redoc/fastapi
that referenced
this pull request
Sep 12, 2024
…pan events with `CMD` exec form (fastapi#11960) Co-authored-by: svlandeg <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>
s-rigaud
pushed a commit
to s-rigaud/fastapi
that referenced
this pull request
Jan 23, 2025
…pan events with `CMD` exec form (fastapi#11960) Co-authored-by: svlandeg <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
There is a problem when using
docker composeand theshell formof theCMDcommand (CMD fastapi run app/main.py --port 80) to runfastapi/uvicorn.Because the
shell formimplicitly invokes a shell (sh -c "fastapi run ...") when starting the container, theuvicornprocess is not running as PID 1, the shellshis. Using theexec form(CMD ["fastapi", "run", "app/main.py", "--port", "80"]),uvicornis running as PID 1. When the user attempts to stop the container by pressing Ctrl + C,docker composewill send aSIGTERMto the process with PID 1. In theshell form, theSIGTERMwill not reachuvicorn/fastapi. This prevents a graceful shutdown and lifespan events from being triggered / finished. Everything works as expected when using theexec form.There have been questions on Stack Overflow of lifespan events not getting triggered and user often recommend using the
shell formin answers without being aware of this issue. Also, the first result that comes up in Google when searching forgraceful shutdown fastapiis #6912 which does not help much.