Skip to content
TheChatArchiveThe Chat Archive
o4-mini-2025-04-16· OpenAIHugging Face LMArena Human Preference 140K

I am running a fastapi service inside a docker container and running that through…

From a Chatbot Arena battleThe same person sent this prompt to two anonymous models at once. Compare it with Geminigemini-2.0-flash-001’s answer.
  1. 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?

  2. 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 ….

    1. Python stdout/stderr buffering
      – When you run docker run -it … you get a TTY, so stdout is line-buffered and each print() 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 with python -u …
      • Alternatively, configure your FastAPI logger to use a StreamHandler(sys.stdout) and call flush() on each record.

    2. 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 k8s command: or Docker CMD, add
      bash uvicorn app.main:app \ --host 0.0.0.0 \ --port 80 \ --log-level debug \ --access-log
      • Or set log_level="debug" when calling uvicorn.run().

    3. You’ve accidentally dropped stdout/stderr into a file
      – Check your logging config (or gunicorn config) for any file handlers. In cloud/k8s you almost always want to ship logs to stdout.
      – Fix:
      • Replace any FileHandler(...) with StreamHandler(sys.stdout)
      • Make sure no sidecar or init-container is re-routing your logs.

    4. You’re tailing the wrong container or the pod crashed & restarted
      – Run kubectl get pods and then kubectl logs -f your-pod-name -c your-container
      – If you have a CrashLoopBackOff you may need --previous to 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: 80
    

    After rolling that out, do:

    kubectl rollout restart deploy/fastapi
    kubectl logs -f deploy/fastapi
    

    You should now see every log line as it happens. Let me know if that fixes it!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.