Capstone: ship an app end to end
This chapter is a project, not a lesson. You'll take a small web app from an empty folder to a live HTTPS address, with tests, a container image, an automated pipeline and basic monitoring, using every topic in these Docs. Do it once on your own and you'll have something real to show, and you'll know where your gaps are.
What you'll build
A tiny Python API with two endpoints, running in Docker on a cloud server behind HTTPS, deployed automatically whenever you merge to main. Budget a weekend. Each step names the chapter to revisit if you get stuck.
Step 1: the project and Git
$ mkdir capstone && cd capstone
$ git init
$ python3 -m venv .venv && source .venv/bin/activate
$ printf '.venv/\n__pycache__/\n.env\n' > .gitignore
Two dependency files: requirements.txt for the app and requirements-dev.txt for development:
# requirements.txt
flask>=3.0,<4
gunicorn>=22,<24
# requirements-dev.txt
-r requirements.txt
pytest>=8,<9
$ pip install -r requirements-dev.txt
Revisit: Git chapters 1 and 2, Python chapter 7.
Step 2: the app and a test
# app.py
import os
import socket
from flask import Flask, jsonify
app = Flask(__name__)
@app.get("/")
def index():
return jsonify(message="Hello from my capstone",
host=socket.gethostname(),
version=os.environ.get("APP_VERSION", "dev"))
@app.get("/health")
def health():
return jsonify(status="ok")
# test_app.py
from app import app
def test_health():
client = app.test_client()
r = client.get("/health")
assert r.status_code == 200
assert r.get_json() == {"status": "ok"}
$ pytest -q
1 passed
$ flask --app app run # try it on http://127.0.0.1:5000, then Ctrl+C
$ git add . && git commit -m "App with a health check and a test"
Revisit: Python chapters 3, 6 and 8.
Step 3: put it in a container
# Dockerfile
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app.py .
RUN useradd --create-home appuser
USER appuser
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "--workers", "2", "app:app"]
# .dockerignore
.venv
.git
__pycache__
.env
$ docker build -t capstone:dev .
$ docker run --rm -p 8000:8000 capstone:dev
$ curl localhost:8000/health # from a second terminal
{"status":"ok"}
Revisit: Docker chapters 2 and 3.
Step 4: GitHub and the pipeline
Create an empty repository on GitHub, push to it, and add the workflow from the CI/CD pipeline chapter as .github/workflows/pipeline.yml. Leave out the deploy job for now; push, open the Actions tab, and check that test and build go green and that the image appears under your profile's Packages.
$ git remote add origin [email protected]:you/capstone.git
$ git branch -M main
$ git push -u origin main
Revisit: Git chapters 5 and 6, DevOps chapters 8 and 9.
Step 5: a server
Start a small Ubuntu server (an EC2 instance on the free tier works; Cloud chapter 3 walks through it) and allow inbound ports 22, 80 and 443 in its security group. Then secure it and install Docker:
$ ssh ubuntu@<server-ip>
$ sudo apt update && sudo apt upgrade -y
$ curl -fsSL https://get.docker.com | sudo sh # Docker's official install script
$ sudo adduser --disabled-password --gecos "" deploy # the user CI will log in as
$ sudo usermod -aG docker deploy
$ sudo mkdir -p /srv/app && sudo chown deploy: /srv/app
$ sudo ufw allow OpenSSH && sudo ufw allow 80 && sudo ufw allow 443 && sudo ufw enable
Give the deploy user the public half of your CI deploy key (a key pair made just for CI, see the pipeline chapter), and turn off password logins as shown in Linux chapter 6:
$ sudo mkdir -p /home/deploy/.ssh
$ sudo nano /home/deploy/.ssh/authorized_keys # paste the public key, save
$ sudo chown -R deploy: /home/deploy/.ssh
$ sudo chmod 700 /home/deploy/.ssh && sudo chmod 600 /home/deploy/.ssh/authorized_keys
One trap: ports that Docker publishes skip ufw's rules, so only publish what you really want public (here, just 80 and 443).
Revisit: Linux chapters 2, 4 and 6, Cloud chapters 3 and 4.
Step 6: HTTPS and the first deploy
Point a domain (or a free subdomain) at the server with an A record, then run the app behind Caddy, a web server that gets and renews Let's Encrypt certificates by itself. Do this step as the deploy user (sudo -iu deploy), since that's the account the pipeline will use, and work in /srv/app:
# compose.yaml
services:
web:
image: ghcr.io/you/capstone:${TAG:-latest}
restart: unless-stopped
environment:
APP_VERSION: ${TAG:-dev}
caddy:
image: caddy:2
restart: unless-stopped
ports:
- "80:80"
- "443:443"
volumes:
- ./Caddyfile:/etc/caddy/Caddyfile:ro
- caddy_data:/data
volumes:
caddy_data:
# Caddyfile
app.example.com {
reverse_proxy web:8000
}
$ docker login ghcr.io -u you # a token with read:packages, if the package is private
$ docker compose up -d
$ curl https://app.example.com/health
{"status":"ok"}
Caddy reaches the app by its service name, web, because Compose puts both on one network. Only Caddy publishes ports. Revisit: Networking chapters 2 and 7, Docker chapter 4.
Step 7: automatic deploys
Add the deploy job back into the workflow, create the production environment and the SSH_KEY, DEPLOY_HOST and DEPLOY_USER secrets, and protect main. Now make a change on a branch, open a pull request, merge it, approve the deploy, and watch / report the new commit SHA as its version.
Step 8: know when it breaks
- Logs:
docker compose logs -f webon the server. - Uptime: point a free uptime checker at
/health, or add a cron job that curls it and alerts you when it fails (Linux chapter 9). - Disk: old images pile up with every deploy, so schedule
docker image prune -af --filter "until=168h"weekly. - Backups: this app keeps no data, but the moment yours does, snapshot the volume or use a managed database (Cloud chapter 5).
Revisit: DevOps chapter 7.
Going further
- Create the server with Terraform instead of clicking (DevOps chapter 5), and install Docker on it with an Ansible playbook (DevOps chapter 6).
- Run the same image on your kind cluster with a Deployment and a Service (Kubernetes chapter 6), then package it as a Helm chart (Kubernetes chapter 7).
- Add a database with Compose, and a second test that uses it.
- Write a README that explains the architecture with a small diagram. A finished project with a clear README and a green pipeline says more than any certificate.
When you get stuck
- Work from the inside out: does
curl localhost:8000/healthwork inside the container (docker compose exec web ...), then from the server, then from outside? The first step that fails tells you which layer to look at (Networking chapter 6). - Caddy can't get a certificate: DNS doesn't point at the server yet, or ports 80 and 443 are blocked in the security group.
docker compose logs caddysays which. - 502 Bad Gateway: Caddy is fine but can't reach
web; check that the web container is running and listening on 8000.