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.

FastApi in Docker shutting down after some requests

See original GitHub issue

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn’t find it.
  • I searched the FastAPI documentation, with the integrated search.
  • I already searched in Google “How to X in FastAPI” and didn’t find any information.
  • I already read and followed all the tutorial in the docs and didn’t find an answer.
  • I already checked if it is not related to FastAPI but to Pydantic.
  • I already checked if it is not related to FastAPI but to Swagger UI.
  • I already checked if it is not related to FastAPI but to ReDoc.

Commit to Help

  • I commit to help with one of those options 👆

Example Code

# Dockerfile
ENTRYPOINT [ "poetry", "run", "uvicorn", "main:app" ]
CMD [ "--host", "0.0.0.0", "--app-dir", "/app/sales_api/", "--log-config", "/logging.conf", "--debug", "true" ]

# logs 

[INFO][2022-09-1416:04:01,091] [Sales API] [sales _api] L36 : Creating the FastAPI application
1663171441105 [INFO][2022-09-1416:04:01,105] [Sales API] [sales_ api] L45 : Attaching API routes to the main application
1663171441106 [INFO][2022-09-1416:04:01,105] [Sales API] [uvicorn.error] L75 : Started server process [15]
1663171441106 (INFO][2022-09-1416:04:01,106] [Sales API] [uvicorn.error] L47 : Waiting for application startup.
1663171441106 (INFO][2022-09-1416:04:01,106] [Sales API] [uvicorn.error] L61 : Application startup complete.
1663171455907 [INFO1[2022-09-1416:04:15,9071 [Sales API1 [uvicorn.access]L474:172.31.22.229:27194-"GET/api/v1/healthcheckHTTP/1.1"307
1663171455910 [INF0][2022-09-1416:04:15,9091 [Sales API] (uvicorn.access]L474:172.31.2.217:42564-"GET/api/v1/healthcheckHTTP/1.1"307
1663171455910 [INF0][2022-09-1416:04:15,9091 [Sales API] [uvicorn.access] L474 : 172.31.33.74:16698 - "GET/api/v1/healthcheck HTTP/1.1" 307
1663171479324 [INFO1[2022-09-1416:04:39.3241 [Sales API1 [uvicorn.access]L474:172.31.33.74:42612-"GET/docsHTTP/1.1"200
1663171479379 [INFO1(2022-09-1416:04:39,3781 [Sales API] (uvicorn.access]L474:172.31.33.74:42612-"GET/api/v1/openapi.jsonHTTP/1.1"200
1663171485937 [INFO][2022-09-1416:04:45,937] [Sales API] [uvicorn.access] L474 : 172.31.22.229:5556 - "GET /api/v1/healthcheck HTTP/1.1" 307
1663171485939 [INFO][2022-09-1416:04:45,939] [Sales API] [uvicorn.access] L474 : 172.31.2.217:34220 - "GET/api/v1/healthcheck HTTP/1.1" 307
1663171485940 [INFO1(2022-09-1416:04:45.9401 [Sales API1 [uvicorn.access1 L474 : 172.31.33.74:10670 - "GET/api/v1/healthcheck HTTP/1.1" 307
1663171501825 [INFO1(2022-09-1416:05:01.8251 [Sales API1 [uvicorn.error] L253: Shuttina down
1663171501925 [INFO1(2022-09-1416:05:01,9251 [Sales API] [uvicorn.error] L66: Waiting for application shutdown.
1663171501925 (INFO1(2022-09-1416:05:01,9251 [Sales API] [uvicorn.error] L77 : Application shutdown complete.
1663171501926 [INFO1[2022-09-1416:05:01,9251 [Sales API] [uvicorn.error] L85 : Finished server process [14]
1663171502985 [INFO1[2022-09-1416:05:02,9851 [Sales API1 [uvicorn.error] L253: Shutting down
1663171503085 [INFO1(2022-09-14 16:05:03.0851 (Sales API] [uvicorn.error] L66 : Waiting for application shutdown.
1663171503086 [INFO1(2022-09-14 16:05:03.0851 Sales API1 [uvicorn.error] L77: Application shutdown complete.
1663171503086 [INFO1(2022-09-14 16:05:03.0861 [Sales API1 [uvicorn.error1 L85 : Finished server process [151]

# Package versions
Poetry == 1.2.0
FastAPI == 0.18.0
uvicorn == 0.18.3
gunicorn == 20.1.0

Description

App working correctly locally on macOS on :

  • Normal start.
  • Docker start.

When deploying under AWS app keep on shutting down on some requests as you can see it in the given logs.

Note that I got correct responses on all routes within the 10-15 first seconds and then a shutdown is forced.

Operating System

Linux

Operating System Details

Under AWS, will give some further details if required. (I do have another FastAPI within the same context and it is working properly)

FastAPI Version

0.80.0

Python Version

3.8.6

Additional Context

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
JarroVGITcommented, Sep 15, 2022

It seems the AWS service is calling the healthcheck endpoint but is redirected every time (that is the 307). I would guess the AWS service is then stopping your application.

0reactions
kaieslamiricommented, Nov 20, 2022

👌

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uvicorn not gracefully shutting down in Docker container #1215
It is expected that the Docker container is shutting down immediately gracefully, either by docker stop container_name or by Ctrl + C if...
Read more >
python 3.x - FastAPI Docker Container shuts down immediately
The installation works and since following there is no other command, it simply shuts down. The dockerfile should look as follows
Read more >
FastAPI in Containers - Docker
When deploying FastAPI applications a common approach is to build a Linux container image. It's normally done using Docker. You can then deploy...
Read more >
How to dockerize FastAPI application - YouTube
Hello guys, in today's tutorial we'll learn how to dockerize a FastAPI application using a Dockerfile and docker -compose.
Read more >
Rails samples - Docker Documentation
This Quickstart guide shows you how to use Docker Compose to set up and run a Rails/PostgreSQL app. -. Looking for more samples?...
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