Thanks to visit codestin.com
Credit goes to github.com

Skip to content

📝 Add note in Docker docs about ensuring graceful shutdowns and lifespan events with CMD exec form#11960

Merged
tiangolo merged 7 commits into
fastapi:masterfrom
GPla:docs/docker-cmd-note
Aug 24, 2024
Merged

📝 Add note in Docker docs about ensuring graceful shutdowns and lifespan events with CMD exec form#11960
tiangolo merged 7 commits into
fastapi:masterfrom
GPla:docs/docker-cmd-note

Conversation

@GPla

@GPla GPla commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

There is a problem when using docker compose and the shell form of the CMD command (CMD fastapi run app/main.py --port 80) to run fastapi/uvicorn.

Because the shell form implicitly invokes a shell (sh -c "fastapi run ...") when starting the container, the uvicorn process is not running as PID 1, the shell sh is. Using the exec form (CMD ["fastapi", "run", "app/main.py", "--port", "80"]), uvicorn is running as PID 1. When the user attempts to stop the container by pressing Ctrl + C, docker compose will send a SIGTERM to the process with PID 1. In the shell form, the SIGTERM will not reach uvicorn/fastapi. This prevents a graceful shutdown and lifespan events from being triggered / finished. Everything works as expected when using the exec form.

There have been questions on Stack Overflow of lifespan events not getting triggered and user often recommend using the shell form in answers without being aware of this issue. Also, the first result that comes up in Google when searching for graceful shutdown fastapi is #6912 which does not help much.

@github-actions

github-actions Bot commented Aug 6, 2024

Copy link
Copy Markdown
Contributor

📝 Docs preview for commit 44cd326 at: https://0268584d.fastapitiangolo.pages.dev

@alejsdev alejsdev added the docs Documentation about how to use FastAPI label Aug 8, 2024
@alejsdev alejsdev changed the title How to ensure a graceful shutdown 📝 How to ensure a graceful shutdown Aug 8, 2024
@estebanx64 estebanx64 requested a review from tiangolo August 17, 2024 20:32
@github-actions

Copy link
Copy Markdown
Contributor

@estebanx64 estebanx64 changed the title 📝 How to ensure a graceful shutdown 📝 How to ensure a graceful shutdown at docker deploy docs Aug 22, 2024
@tiangolo tiangolo changed the title 📝 How to ensure a graceful shutdown at docker deploy docs 📝 Add note in Docker docs about ensuring graceful shutdowns and lifespan events with CMD exec form Aug 24, 2024
@tiangolo

Copy link
Copy Markdown
Member

Great, thank you @GPla! 🚀 I tweaked it a bit and made it its own section so we can link to it more easily. 🤓

@tiangolo tiangolo enabled auto-merge (squash) August 24, 2024 20:04
@tiangolo tiangolo merged commit 9656895 into fastapi:master Aug 24, 2024
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Documentation about how to use FastAPI

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants