astAPI Docker container ignores SIGTERM — pod takes 30s to terminate and lifespan shutdown never runs
17:37 16 Jun 2026

I containerized a FastAPI service (myapp, Python 3.12, port 8080) and deploy it to a local k3d cluster. The app works, but every kubectl delete pod / rolling update takes ~30 seconds, and my FastAPI lifespan shutdown hook (closing the PostgreSQL pool) never runs. It looks like the process is being killed hard.

My Dockerfile ends with:

FROM python:3.12-slim
WORKDIR /code
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY ./app ./app
EXPOSE 8080
CMD fastapi run app/main.py --port 8080

Reproduce with plain Docker:

docker build -t myapp:dev .
docker run --name myapp myapp:dev
# in another terminal:
time docker stop myapp

docker stop takes the full 10-second grace period and then the container is killed. Inside the container ps shows:

PID   COMMAND
1     /bin/sh -c fastapi run app/main.py --port 8080
7     /usr/local/bin/python /usr/local/bin/fastapi run ...

Why is SIGTERM being ignored, and how do I make graceful shutdown work?

docker kubernetes fastapi sigterm