Docker, CI/CD and Deployment
In a small team, deployment and monitoring are the developer's job too: you own the outcome in production.
Core concepts
| Concept | Description |
|---|---|
| Multi-stage builds | One stage installs dependencies / compiles, and the final image copies only what's needed at runtime, making it smaller and safer |
| Docker Compose | Defines several services (web, api, worker, postgres, redis) in one YAML file |
| Health checks | Container HEALTHCHECK / the orchestrator's liveness and readiness probes |
| CI/CD | Automatically test, build images and deploy after each commit (e.g. GitHub Actions) |
| Next.js standalone | output: 'standalone' bundles only the files needed at runtime, well suited to Docker |
How it works
- Layer caching: each Dockerfile instruction is a layer, and unchanged earlier layers are reused from cache. So copy the dependency manifest and install dependencies first, then copy the source.
- Process model: one main process per container; the API and the worker use the same image with different start commands.
- Configuration: injected through environment variables (12-Factor); never bake secrets into the image.
In practice
FastAPI image (dependencies managed with uv)
dockerfile
FROM python:3.13-slim AS base
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 # dependency layer: source changes don't invalidate it
COPY . .
ENV PATH="/app/.venv/bin:$PATH"
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]docker-compose.yml (excerpt)
yaml
services:
api:
build: ./backend
env_file: .env
depends_on:
db: { condition: service_healthy }
worker:
build: ./backend
command: python -m app.worker
env_file: .env
web:
build: ./frontend
ports: ["3000:3000"]
db:
image: pgvector/pgvector:pg17
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
interval: 5s
redis:
image: redis:7Go-live checklist
- Health check endpoints
/healthz(liveness) and/readyz(dependencies ready) - Database migrations (Alembic) run as a separate step in the deploy pipeline
- Logs to stdout as structured JSON with a trace_id
- Alerts: error rate, P95 latency, LLM call failure rate, task backlog, human queue length
- Rollback plan: version-tag images so you can return to the previous release in one step
Common pitfalls
- COPYing
.envor secrets into the image: exclude them with.dockerignore. depends_ononly waits for the container to start, not for the service to be ready: pair it withcondition: service_healthy.- Graceful worker shutdown: on SIGTERM, finish or release the current task, or it stays stuck in running.
- Tagging images only as latest: no way to roll back or trace.
Common interview questions
- What are the benefits of multi-stage builds? How do you write a Dockerfile to make the most of the cache?
- How is your project deployed? How do you roll back when something goes wrong?
- What's the difference between liveness and readiness probes?
- When a container is stopped, what happens to the task it's running?
- Which monitoring alerts would you set up for an LLM application?
To be verified / may go stale
- The image versions in the examples (
python:3.13-slim,pgvector/pgvector:pg17,redis:7) are only illustrative. Choose based on the current stable versions and license requirements.
