iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🩺

Why FastAPI Crashes in Production but Works Locally: Typical Issues Only Seen in Docker

に公開
2026/02/15

Introduction

After containerizing your FastAPI app with Docker, have you ever encountered a situation like this?

  • It runs perfectly on your local machine.
  • It also starts up fine in Docker.
  • But it throws errors or silently crashes only in the production environment.

It is manageable if it throws an error. The truly troublesome cases are those where "it behaves differently without saying a word."

This is not really a problem with FastAPI itself, but rather an incident where the underlying assumptions are rewritten by Docker.


Docker is not for "reproducing the execution environment"

A common misconception is that:

Using Docker = Having the exact same environment as local

People often think this, but the reality is the opposite.

The moment you switch to Docker, the following things are all swapped out at once:

  • How environment variables are loaded
  • The user executing the process
  • The destination for standard output and logs
  • The execution command (uvicorn / gunicorn)

In other words, containerizing with Docker is more like a process of changing the environment rather than a process of aligning it.


Typical patterns where "Local OK, Production NG" occurs

A common combination that leads to these incidents looks like this:

  • You are loading .env directly on your local machine.
  • You are relying on ENV or --env-file in Docker.
  • However, the application starts even if the keys are undefined.

Since FastAPI itself starts successfully, it "appears to be working." However, behind the scenes, features like:

  • Authentication
  • External API integrations
  • Logging configurations

are actually broken.


It is also linked to why logging "silently dies"

The logging incidents I have mentioned so far often happen in conjunction with this Docker discrepancy.

  • Logs you intended to send to standard output are not appearing outside the Docker container.
  • Or, the moment you start it via gunicorn, the assumptions about log levels and handlers change.

As a result, you end up in a state where:

An error is occurring, but there is nothing to observe in any readable location.


Perspectives for isolation

In these types of incidents, the important thing is to suspect the environmental assumptions before suspecting the "FastAPI code" itself.

Specifically:

  • What command is being used to start it?
  • Are the environment variables loaded in local and Docker the same?
  • Where is the logging designed to output to?

Unless you can verbalize these, simply fixing the code will not stop it from recurring.


Summary

Docker incidents are less about the "code being bad" and more about "an incident where you didn't realize the prerequisites had changed."


A parting thought

Even with the same code, if the environment in which it is placed changes, the meaning easily changes.

I perform similar cause isolation for authentication troubles. Contact information is provided at the end of the article.


🛠️ FastAPI Incident Analysis Series

I am publishing articles pattern-by-pattern on difficult-to-isolate cases such as "401 / JWT / Only breaks in Docker."

🔐 Authentication & JWT Troubles

Why /me returns 401 even though /token passes
[https://zenn.dev/fastapier/articles/0022f125547300]

Why SECRET_KEY becomes None even though environment variables are set
[https://zenn.dev/fastapier/articles/e93b522cc0acb3]

If you confuse 401 / 403, you will lose sight of the cause
[https://zenn.dev/fastapier/articles/41f9e2e10e7c19]

Why Depends silently breaks and results in 401 / 403
[https://zenn.dev/fastapier/articles/efba40f5bcbbda]

🐳 Docker & Production Environment Troubles

Why logging stopped appearing after containerizing with Docker
[https://zenn.dev/fastapier/articles/cd530c54b6e47c]

🏗️ Design & Operations Troubles

Why things fall apart when main.py exceeds 1000 lines
[https://zenn.dev/fastapier/articles/a2a9a5209dedac]


🩺 FastAPI Incident Consultation Window (Cause Identification & Structural Analysis)

For authentication troubles around FastAPI / JWT / Docker, such as:

  • /me returns 401 even though /token passes
  • InvalidSignatureError occurs only in the production environment
  • Authentication fails only after containerizing with Docker

I handle the isolation and cleanup of strategy for "incidents where the layers of causes are difficult to see."

If you have spent more than 3 hours investigating and cannot identify the cause, it is highly likely that the problem is not in the code, but in the "structure."

📩 How to consult

Please contact me at the email address listed on my GitHub profile.
[https://github.com/hiro-kuroe]

Please include only the following three points in the body of the email (short sentences are fine):

① Symptoms
(e.g., /me is 401 / Signature error only in production / Authentication failure after Dockerization)

② Environment
(e.g., Local OK, Production NG / Using Docker / Using Gunicorn, etc.)

③ What you changed immediately before
(e.g., Changed SECRET_KEY / Added .env / Updated dependency packages, etc.)

I will proceed with the analysis phase (cause identification and structural organization) first. I will propose a fix plan after the cause becomes clear.

Discussion