Skip to content

Courses / GenAI & LLM / Module 14

Module 14: Deployment: FastAPI, Docker, CI/CD, cloud

12 h · theory 4 · hands-on 5 · project 2 · revision 1

You will be able to

  • Package the app as containers with health checks
  • Build a GitHub Actions pipeline: test, eval, build, deploy
  • Deploy to a VPS or Kubernetes with TLS and secrets
  • Roll back safely

Capstone increment: Capstone live at a public HTTPS URL, deployed by CI on merge to main.

Job-description skills: Docker, Kubernetes, CI/CD, FastAPI, Cloud deployment (AWS/Azure/GCP) · Lab tools: docker, docker-compose, kubectl, gh, caddy

1. Package it

Ship the capstone as a container image: a slim Python base, dependencies installed in a cached layer, the app copied last, a non-root user, and a single process (uvicorn) as the entrypoint. Tag every image with the git commit SHA so you always know exactly what's running and can return to it.

Configuration comes from environment variables, validated at startup: fail fast if a required setting is missing rather than on the first request. Secrets come from the platform's secret store, never from the image or the repository.

MergePR approvedTest + evalpytest, golden setBuildDocker image :shaRegistryGHCRDeployVPS or k8sHealth/healthz, smokeMerge to main → live, with no manual stepsunhealthy: roll back to the previous image
Every deploy is a tagged image you can return to in one command.
In production: Model weights don’t belong inside the application image. Mount them from a volume or let the serving container download them, so app deploys stay small and fast.

2. Health checks that mean something

Expose two endpoints. Liveness (/healthz) answers “is the process alive?” and must stay trivial, or a slow dependency gets your container restarted in a loop. Readiness (/readyz) answers “can I serve traffic now?” by checking the database and the model endpoint, so the load balancer stops routing to an instance that can't do useful work.

Add a smoke test after each deploy: one real request through the main endpoint, checked for a valid response. Health checks prove the process runs; the smoke test proves the product works.

3. CI/CD with GitHub Actions

On every pull request: lint, unit tests, and the eval suite (Module 8). On merge to main: build the image, push it to a registry (GitHub Container Registry is free for public repos), deploy, then smoke-test. Each stage only runs if the previous one passed.

Keep deployment credentials in GitHub environment secrets, scope them to the deploy job only, and require approval for the production environment if more than one person works on the project.

4. Where to run it

For a capstone and many real products, a single VPS with Docker Compose and Caddy (automatic HTTPS) is enough and costs a few hundred rupees a month. The model runs either through a hosted API or a separate GPU instance only when needed.

Kubernetes earns its complexity when you have several services, need autoscaling, or run GPU workloads across nodes. Learn the basics (deployments, services, probes, secrets), but don't adopt it until a simpler setup actually hurts.

Common misconception: “Real projects run on Kubernetes.” Many profitable products run on one or two well-managed VMs. Choose the platform your team can operate at 3 a.m.

5. Releasing safely and rolling back

Every deploy should be reversible in one command: redeploy the previous image tag. Practise it before you need it. For riskier changes, send a small share of traffic to the new version first (a canary), compare errors, latency and quality signals with the stable version, then promote or roll back.

Database migrations need extra care: make them backward-compatible (add columns before using them, remove only after the old code is gone) so a rollback doesn't break on the new schema.

Hands-on: coding window

The coding window and the full VS Code lab are available after you sign in.

Sign in to code

Labs

Lab 14.1: Containerise the capstone

Write a multi-stage Dockerfile (non-root, slim base, cached dependency layer) and a docker-compose.yml with the API, Postgres, Langfuse and an optional local model. Add /healthz and /readyz.

Expected: docker compose up brings everything up; the image is under 400 MB; readiness fails cleanly when the model endpoint is stopped.

Lab 14.2: Deploy by CI (capstone increment)

Build a GitHub Actions pipeline: tests and evals on pull requests; on merge, build and push an image tagged with the commit SHA to GHCR, deploy to a VPS over SSH with Docker Compose behind Caddy, and run a smoke test.

Expected: The capstone is live at a public HTTPS URL; merging a pull request deploys it with no manual steps.

Lab 14.3: Practise a rollback

Deploy a deliberately broken version (the smoke test should catch it), then roll back to the previous image tag. Time it, and write the runbook.

Expected: A rollback under 5 minutes and docs/runbook.md with the exact commands.

Go deeper

Videos

Further reading

GitHub repositories

  • docker/awesome-compose: The FastAPI and Postgres samples: compose files with health checks and volumes to copy from.
  • fastapi/fastapi: The deployment docs on Docker, HTTPS and workers: what the framework authors recommend.
  • vllm-project/vllm: The Docker deployment docs, if you serve your own model next to the app.

Checkpoint quiz

1. Why should the liveness check not call the model provider?
2. Why tag images with the commit SHA instead of only ‘latest’?
3. Where should the production API key live?
4. A canary shows a higher error rate than stable. What next?
5. When does Kubernetes start to make sense?

Interview questions

  1. Walk me through your CI/CD pipeline from pull request to production.
  2. What’s the difference between liveness and readiness checks, and what does each check in your service?
  3. How do you roll back a bad deploy, and how long does it take?
  4. How do you handle secrets across local development, CI and production?
  5. Would you use Kubernetes for this capstone? Why or why not?

Ask the professor

Sign in to ask questions about this module.

Discussion

Community discussion appears here once giscus is configured (see README).