Why this matters
Containers make "works on my machine" work everywhere: CI, staging and production run the same image. Nginx (or a cloud load balancer) sits in front of almost every web service, terminating TLS and routing requests.
Core ideas
- Image: a read-only filesystem snapshot plus a start command. Container: a running instance of an image, isolated with its own processes and network.
- Layers and caching: each Dockerfile instruction makes a layer; order them so rarely-changing steps (installing dependencies) come before often-changing ones (copying source).
- Multi-stage builds: build in a full image, copy only the result into a small runtime image.
- Docker Compose: declares several containers (API, Postgres, Redis, worker) and their networks and volumes for local development.
- Reverse proxy: receives client requests and forwards them to app servers; handles TLS, compression, static files, timeouts and buffering.
- systemd: the Linux service manager that keeps a process running and restarts it if it dies (for non-container deploys on a VM).
How to do it
# syntax=docker/dockerfile:1
FROM python:3.12-slim AS build
COPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv
WORKDIR /app
COPY pyproject.toml uv.lock ./
RUN uv sync --frozen --no-dev --no-install-project
COPY src ./src
RUN uv sync --frozen --no-dev
FROM python:3.12-slim
RUN useradd --create-home app
WORKDIR /app
COPY --from=build --chown=app:app /app /app
USER app
ENV PATH="/app/.venv/bin:$PATH"
EXPOSE 8000
CMD ["uvicorn", "myservice.main:app", "--host", "0.0.0.0", "--port", "8000"]
server {
listen 443 ssl;
server_name api.example.com;
client_max_body_size 25m;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 120s;
}
location /v1/chat/stream {
proxy_pass http://127.0.0.1:8000;
proxy_buffering off; # stream tokens as they arrive
proxy_read_timeout 300s;
}
}
Why this way
Small, non-root images reduce attack surface and pull time. Multi-stage builds keep compilers and caches out of production. Turning off proxy buffering only on streaming routes keeps the benefits of buffering elsewhere.
Common mistakes
- Running containers as root.
- Copying the whole repository (including
.envand.git) into the image; use.dockerignore. - Baking secrets into images; inject them at runtime.
- Default proxy timeouts (often 60 s) cutting off long model responses.
Interview follow-ups
- "Why is my Docker image 2 GB?" Build tools and caches in the final image, a fat base image, copying the whole repo. Fix with multi-stage builds, slim bases,
.dockerignoreand cleaning package caches in the same layer. - "How do containers differ from VMs?" Containers share the host kernel with isolated processes, filesystems and networks (fast, light); VMs virtualise hardware with their own kernel (stronger isolation, heavier).
- "How do you do zero-downtime deploys on one VM?" Two app instances behind Nginx, start the new one, health-check it, switch the upstream, drain the old one.
Check yourself
Why copy the lockfile and install dependencies before copying source code?
So the dependency layer is cached and only rebuilt when dependencies change; source edits then rebuild only the final, fast layers.
Your SSE endpoint streams locally but not behind Nginx. What do you change?
Disable buffering for that location (proxy_buffering off; or the app sends X-Accel-Buffering: no), raise proxy_read_timeout, and make sure compression isn't buffering the stream.
Build it
Containerise your FastAPI service with the Dockerfile above and run it with Postgres, Redis and a worker in Docker Compose. Put Nginx in front and stream through it.