Learn Labs
Foundations

Docker & Setup

The minimal server, the production-shaped edge, the lab CA, and the lab profile

Everything in this module lives in nginx/ in the repository.

The minimal setup

services:
nginx:
  image: nginx:1.30.5
  ports: ["8090:80"]
  volumes: ["./html:/usr/share/nginx/html:ro"]
docker compose up -d
curl -s localhost:8090 | head -5

The official image's default config serves /usr/share/nginx/html. That's a working web server in four lines, and it's the only part of this module without TLS.

The production-shaped edge

HTTPS :8443/, /assets//api/, /wsclientsbrowsers, appsnginxTLS, cache, limitshtml/static filesapp-1app-2app-3the apps speak plain HTTP on a private network; only nginx is published
certsone-shot: lab CA, server certificate, htpasswd -> volume nginx_certsapp-1..3the backend (aiohttp): whoami, slow, fail, cached, upload, /wsnginx:8090 -> 80 (redirect), :8443 -> 443 (the site); config mounted read-onlydepends_on: certs: service_completed_successfullynginx-exporterstub_status -> Prometheus metrics on :9113labsprofile labs; Docker socket mounted to stop and pause appsnetworks:nginx:ipam: { config: [{ subnet: 172.30.0.0/24 }] }fixed, so allow rules are predictable
docker compose down
docker compose -f compose_prod.yaml up -d --build
docker exec nginx nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
WhatWhere
HTTPS site and APIhttps://localhost:8443
HTTP (redirects; /healthz answers)http://localhost:8090
Admin area (admin / admin123)https://localhost:8443/admin/
Metricshttp://localhost:9113/metrics

The lab CA

scripts/certs.sh runs once in the certs container. It creates a certificate authority, then a server certificate signed by it for localhost, nginx and 127.0.0.1. Clients that trust ca.crt trust the server, exactly as browsers trust public CAs.

docker cp nginx:/etc/nginx/certs/ca.crt ./ca.crt
curl --cacert ca.crt https://localhost:8443/api/whoami
{"instance": "app-1", "path": "/api/whoami", "peer": "172.30.0.5", "host": "localhost",
"x_real_ip": "172.30.0.1", "x_forwarded_proto": "https", ...}

The lab profile

docker compose -f compose_prod.yaml --profile labs build labs
docker compose -f compose_prod.yaml run --rm labs python load_balancing.py

The labs use httpx (with HTTP/2) and trust only the lab CA. They mount the Docker socket so the failure labs can stop, pause and move app containers, and like every module's labs, they have no depends_on.

Lab

  1. Start the minimal stack and request a file that doesn't exist. What does the 404 page reveal?
  2. Start the production stack and request /api/whoami ten times with curl. Which instances answer?
  3. Request http://localhost:8090/api/whoami with curl -i. Where does it send you?

On this page