I am running a fastapi service inside a docker container and running that through…
- User
I am running a fastapi service inside a docker container and running that through kubernetes. However, after uvicorn shows the 'application started' I cannot see any more logs (through different logging configs, even through logfire). This does not happen if I run it through docker directly. What could it be?
- Assistant
Here are the usual suspects when “my app starts (you see Uvicorn’s banner) but then absolutely nothing else” in k8s—yet everything works in
docker run -it ….-
Python stdout/stderr buffering
– When you rundocker run -it …you get a TTY, so stdout is line-buffered and eachprint()or log line immediately shows.
– In k8s (no-t), Docker attaches a pipe, and Python’s stdio becomes block-buffered: nothing appears until the buffer fills or the process exits.
– Fix:
• In your Dockerfile or manifest, set
yaml env: - name: PYTHONUNBUFFERED value: "1"
or launch withpython -u …
• Alternatively, configure your FastAPI logger to use aStreamHandler(sys.stdout)and callflush()on each record. -
You’re not emitting any logs at the level Uvicorn is configured for
– By default Uvicorn’s log‐level is INFO and access-logs are off. If your app is only doing DEBUG logs (or access logs), you won’t see them.
– Fix:
• In your k8scommand:or Docker CMD, add
bash uvicorn app.main:app \ --host 0.0.0.0 \ --port 80 \ --log-level debug \ --access-log
• Or setlog_level="debug"when callinguvicorn.run(). -
You’ve accidentally dropped stdout/stderr into a file
– Check your logging config (orgunicornconfig) for any file handlers. In cloud/k8s you almost always want to ship logs to stdout.
– Fix:
• Replace anyFileHandler(...)withStreamHandler(sys.stdout)
• Make sure no sidecar or init-container is re-routing your logs. -
You’re tailing the wrong container or the pod crashed & restarted
– Runkubectl get podsand thenkubectl logs -f your-pod-name -c your-container
– If you have a CrashLoopBackOff you may need--previousto see the earlier logs.
Putting it all together a minimal k8s Deployment snippet might look like:
apiVersion: apps/v1 kind: Deployment metadata: name: fastapi spec: replicas: 1 selector: matchLabels: { app: fastapi } template: metadata: labels: { app: fastapi } spec: containers: - name: web image: your-image:latest env: - name: PYTHONUNBUFFERED value: "1" command: ["uvicorn", "app.main:app"] args: - "--host=0.0.0.0" - "--port=80" - "--log-level=debug" - "--access-log" ports: - containerPort: 80After rolling that out, do:
kubectl rollout restart deploy/fastapi kubectl logs -f deploy/fastapiYou should now see every log line as it happens. Let me know if that fixes it!
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.