Python course ยท Module 12: Final Project
Deployment to Production
In this lesson4
"It works on my machine" is the most common sentence in the history of programming. Your laptop has the right Python version, the installed packages and the environment variables. A cloud server has none of these. Deployment is the moment when code becomes a product, and our job is to pack the camp so that it unfolds identically anywhere on the savanna.
Docker - Containerization
Docker packs the application together with the system, Python and dependencies into an image. A container is a running image. We will use a multi-stage build: the first stage builds the packages, the second takes only the result, which makes the final image smaller. The first stage:
# Dockerfile
FROM python:3.12-slim AS builder
WORKDIR /app
COPY requirements.txt .
RUN pip wheel --no-cache-dir --no-deps --wheel-dir /wheels -r requirements.txtpip wheel turns every dependency into a ready .whl file. The --no-deps flag means pip does not pull in transitive dependencies, so requirements.txt must contain the full, pinned list (for example from pip freeze). The second stage:
FROM python:3.12-slim
WORKDIR /app
# Dependencies
COPY /wheels /wheels
RUN pip install --no-cache-dir /wheels/*
# Security
RUN useradd -m -u 1000 appuser
USER appuser
# Application
COPY . .
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]The order matters. We install the packages while still root, and only then switch to the regular user appuser, because the application should not run with root privileges. If we switched users before pip install, pip would install the packages into the home directory and the uvicorn command would not be on the path. You build the image with docker build -t app:latest . (the dot is the directory with the Dockerfile) and run it locally with docker run -p 8000:8000 app:latest. The typical order of work: Dockerfile, build, local test, push to a registry, deploy to a server.
Docker Compose manages several containers as one service. Our application needs Qdrant and Redis, and we describe everything in a single file. First, the application service:
# docker-compose.yml
services:
app:
build: .
ports:
- "8000:8000"
environment:
- OPENAI_API_KEY=${OPENAI_API_KEY}
- QDRANT_URL=http://qdrant:6333
- REDIS_URL=redis://redis:6379
depends_on:
- qdrant
- redis
healthcheck:
test: ["CMD", "python", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8000/health')"]
interval: 30s
timeout: 10s
retries: 3depends_on sets the start order, and healthcheck calls the /health endpoint every 30 seconds. We use Python from the standard library for that, because the python:3.12-slim image does not include curl. The version field at the top of the file is obsolete, current Docker Compose ignores it, so we do not write it. Next, the databases and volumes:
qdrant:
image: qdrant/qdrant:latest
volumes:
- qdrant_data:/qdrant/storage
ports:
- "6333:6333"
redis:
image: redis:alpine
volumes:
- redis_data:/data
volumes:
qdrant_data:
redis_data:Volumes make the data survive a container restart. The latest tag is convenient to start with, but in production pin a specific image version so that an update does not arrive unexpectedly.
GitHub Actions - CI/CD
CI/CD stands for Continuous Integration and Continuous Deployment: every push automatically goes through testing, building and deployment. The pipeline has consecutive stages: test, build, push to the registry, deploy. The first stage runs the tests:
# .github/workflows/deploy.yml
name: Deploy
on:
push:
branches: [main]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: "3.12"
- run: pip install -r requirements.txt
- run: pytest tests/ -v
If the tests fail, the later stages do not run. The second stage builds the image and pushes it to Docker Hub (needs: test means "only after successful tests"):
build:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Build Docker image
run: docker build -t app:latest .
- name: Push to Registry
run: |
echo ${{ secrets.DOCKER_PASSWORD }} | docker login -u ${{ secrets.DOCKER_USERNAME }} --password-stdin
docker tag app:latest ${{ secrets.DOCKER_USERNAME }}/ai-assistant:latest
docker push ${{ secrets.DOCKER_USERNAME }}/ai-assistant:latest
The password reaches docker login through --password-stdin, so it does not show up in the logs. The last stage connects to the server over SSH and replaces the containers:
deploy:
needs: build
runs-on: ubuntu-latest
steps:
- name: Deploy to server
uses: appleboy/ssh-action@v1
with:
host: ${{ secrets.SERVER_HOST }}
username: ${{ secrets.SERVER_USER }}
key: ${{ secrets.SSH_KEY }}
script: |
cd /app
docker compose pull
docker compose up -dWe pin the action to a version (@v1) rather than to the master branch, because a branch can change without warning. That matters especially when the action receives the SSH key to your server.
Cloud Deployment Options
Where do you run the container? Here are the four main routes with their pros and cons:
"""
Deployment options:
1. VPS (DigitalOcean, Hetzner)
- Full control
- Low cost (depends on the provider and machine size)
- Requires management
2. Platform as a Service
- Railway, Render, Fly.io
- Easy deployment
- Auto-scaling
3. Kubernetes
- For large systems
- High availability
- Complex configuration
4. Serverless
- AWS Lambda, Google Cloud Run
- Pay-per-use
- Cold starts
"""Prices change often, so compare them in the providers' current price lists. My advice for a first portfolio project: a PaaS platform, because you deploy a container in a few minutes and do not have to administer a server.
Monitoring
After deployment you need to know whether the application is alive and how fast it responds. The prometheus_client library provides counters (Counter) and timing histograms (Histogram), which Prometheus reads from the /metrics endpoint. app is the FastAPI application from the previous lesson:
# Prometheus metrics
from prometheus_client import CONTENT_TYPE_LATEST, Counter, Histogram, generate_latest
from fastapi import Response
REQUEST_COUNT = Counter('requests_total', 'Total requests', ['method', 'endpoint'])
REQUEST_LATENCY = Histogram('request_latency_seconds', 'Request latency')
@app.middleware("http")
async def metrics_middleware(request, call_next):
REQUEST_COUNT.labels(request.method, request.url.path).inc()
with REQUEST_LATENCY.time():
response = await call_next(request)
return response
@app.get("/metrics")
async def metrics():
return Response(generate_latest(), media_type=CONTENT_TYPE_LATEST)The middleware counts every request and measures its duration. CONTENT_TYPE_LATEST is the correct content type for the Prometheus format, safer than a hand-typed text/plain. Be careful with the path label: with URLs containing identifiers, the number of series can grow without limit.
Your project is ready for production! In the next lesson we will build a portfolio that shows it to the world.
Remember: a well-packed camp unfolds identically anywhere on the savanna, and that is exactly what a container gives you.
Docs entries used in this lesson
Spotted a mistake in this lesson?
Check yourself
Answer the questions from this lesson. Pick an answer to see right away whether it is correct.
1. What is the advantage of Docker multi-stage build?
2. What does Docker Compose allow you to do?
These are 2 of 3 questions for this lesson. Solve the rest in the game.
Hands-on tasks in the game
- Click in order
Click the elements to build a deployment pipeline in the correct order:
- Vertical ordering
Arrange the steps of working with Docker in the correct order:
- Code editor
Write a Dockerfile for a Python AI application
- Horizontal ordering
Arrange the Docker build command with tag in the correct order:
- Click in order
Click the elements to build a docker run command:
- Code editor
Create a docker-compose.yml file for an AI application with a database
- Vertical ordering
Arrange the stages of a CI/CD pipeline in the correct order: