Part X. Linux, DevOps and Deployment¶
An application that runs only on a developer's laptop is not production. This part covers Linux basics, Docker, Docker Compose, Nginx and CI.
Prerequisites: a general understanding of the project. Level: Intermediate → Production.
Sections¶
Linux¶
| Document | What we will learn |
|---|---|
| The Linux mental model | kernel, userspace, files as an abstraction, PID, fd |
| Terminal and shell | bash, zsh, stdin/stdout/stderr, pipes, > >>, $? |
| The file system | FHS, ls, find, tree, pwd, cd, symlinks |
| Files and permissions | chmod, chown, umask, sudo, users/groups |
| Processes and ports | ps, top, kill, lsof, netstat, ss, systemctl |
| Package managers | apt, pip, pyenv, nvm |
| SSH and servers | ssh, scp, rsync, authorized_keys, jump host |
| Environment variables | export, .env, os.environ, secrets management |
| Bash scripts | shebang, variables, conditions, loops, functions, exit codes |
| Makefile | targets, phony, make test, make build |
Docker¶
| Document | What we will learn |
|---|---|
| Docker and Runtime | image, container, layer cache, volumes, networks |
| Docker: the complete guide | Dockerfile, build, run, exec, logs, compose integration |
Deployment¶
| Document | What we will learn |
|---|---|
| Django on Linux | virtualenv, gunicorn, systemd service, collectstatic |
| Nginx + Gunicorn/Uvicorn | proxy_pass, WebSocket upgrade, SSL, static files |
| Logs and monitoring | journalctl, docker logs, LOGGING in Django |
| Docker basics | image layers, registry, ENTRYPOINT, multi-stage |
| Docker Compose | services, volumes, networks, health checks, depends_on |
| DevOps workflow | CI/CD pipeline, blue-green deploy, rollback |
| Kubernetes overview | Pod, Deployment, Service, Ingress — conceptually |
| Roadmap | what to learn next after the course |
Key concepts¶
The Notes Chat App Docker Compose stack:
# docker-compose.yml (simplified)
services:
db:
image: postgres:16-alpine
volumes: [postgres_data:/var/lib/postgresql/data]
redis:
image: redis:7-alpine
web:
build: .
command: ["sh", "./entrypoint.sh"]
environment:
DATABASE_URL: postgres://user:pass@db:5432/notes
REDIS_URL: redis://redis:6379/0
depends_on: [db, redis]
ports: ["8001:8001"]
nginx:
image: nginx:1.27-alpine
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- staticfiles:/staticfiles:ro
ports: ["80:80"]
depends_on: [web]
celery-worker:
build: .
command: ["celery", "-A", "notes_project", "worker", "--loglevel=info"]
depends_on: [db, redis]
celery-beat:
build: .
command: ["celery", "-A", "notes_project", "beat", "--loglevel=info"]
depends_on: [db, redis]
selenium: # profiles: ["test"]
image: selenium/standalone-chrome
ports: ["4444:4444"]
selenium-tests: # profiles: ["test"]
build: .
command: ["python", "manage.py", "test", "notes_app.tests.test_selenium", "-v", "2"]
depends_on: [web, nginx, selenium]
volumes:
postgres_data:
redis_data:
staticfiles:
celerybeat_data:
entrypoint.sh — startup sequence:
#!/bin/sh
set -eu
# 1. PostgreSQL and Redis are already healthy: depends_on + healthcheck in docker-compose.yml guarantee it
# 2. Apply migrations
if [ "${RUN_MIGRATIONS:-1}" = "1" ]; then
python manage.py migrate --noinput
fi
# 3. Collect static files
if [ "${RUN_COLLECTSTATIC:-1}" = "1" ]; then
python manage.py collectstatic --noinput
fi
# 4. Seed demo data (if SEED_DEMO_DATA=1)
if [ "${SEED_DEMO_DATA:-0}" = "1" ]; then
python manage.py seed_demo_data ${seed_args} # --force or --force --reset
fi
# 5. Start the Uvicorn ASGI server (with --reload in dev)
exec python -m uvicorn notes_project.asgi:application --host 0.0.0.0 --port 8001 --proxy-headers
Nginx configuration for WebSocket:
# nginx/nginx.conf
upstream django {
server web:8001;
}
server {
listen 80;
location / {
proxy_pass http://django;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# WebSocket upgrade
location /ws/ {
proxy_pass http://django;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # ← required for WS
proxy_set_header Connection "upgrade";
}
# Static files — Nginx serves them directly (not Django)
location /static/ {
alias /staticfiles/;
}
}
Environment variables — secrets:
# .env (never commit it to git)
SECRET_KEY=django-prod-secret-key-here
DATABASE_URL=postgres://user:pass@db:5432/notes
REDIS_URL=redis://redis:6379/0
DEBUG=False
ALLOWED_HOSTS=notes.example.com,www.notes.example.com
# In Python (the production approach):
import os
SECRET_KEY = os.environ['SECRET_KEY'] # ← Exception if missing (no default!)
# The teaching project currently uses os.environ.get("SECRET_KEY", "django-insecure-...")
# with a dev default — remove that default in production.
Production checklist:
✓ DEBUG=False
✓ SECRET_KEY — from env, not in the code
✓ DATABASE_URL — missing → Exception (no SQLite fallback!)
✓ ALLOWED_HOSTS — specific domains
✓ HTTPS — CSRF_TRUSTED_ORIGINS, SESSION_COOKIE_SECURE
✓ Static files — Nginx, not Django
✓ DebugExceptionMiddleware — only if DEBUG
✓ Logrotate / log aggregation
✓ Health check endpoint
✓ CI: tests green before deploying
Where this is in Notes Chat App¶
| File | What it shows |
|---|---|
docker-compose.yml |
the full stack: db, redis, web, nginx, celery-worker, celery-beat, selenium, selenium-tests, ngrok |
Dockerfile |
single-stage build: FROM python:3.12-slim |
entrypoint.sh |
startup: migrate → collectstatic → seed_demo_data → uvicorn |
nginx/nginx.conf |
proxy_pass, WebSocket upgrade, static alias |
.github/workflows/django-tests.yml |
GitHub Actions CI |
notes_project/settings.py |
SECRET_KEY = os.environ.get("SECRET_KEY", ...), DATABASE_URL parsing |
Teaching connection¶
Linux basics → Docker → Docker Compose → Nginx → GitHub Actions CI
Part X covers the entire Notes Chat App deployment stack (Step 8 of Zero to Hero)
Zero to Hero: Step 8 — Docker Compose, Nginx, GitHub Actions.
Practice¶
- Run
docker compose ps— review the state of all services. - Run
docker compose logs web --tail=50— find the startup sequence. - Run
docker compose exec web python manage.py shell→from django.conf import settings; print(settings.DEBUG). - Change
SECRET_KEYin.env→docker compose up -d→ make sure the application restarted. - Run
docker compose exec web python manage.py check --deploy— go through the production checklist.
Review questions¶
- What are a Docker image and a Docker container? What is the difference between them?
- Why do you need
volumes:in Docker Compose? What happens to the database data without a volume? - Why does Nginx serve
/static/directly rather than through Django? - What is
ENTRYPOINTin a Dockerfile? - Why should a missing
DATABASE_URLraise anExceptionrather than fall back to SQLite? - What is
proxy_set_header Upgrade $http_upgrade? Why is it needed for WebSocket? - What does
python manage.py check --deploycheck?
Next → Notes Chat App Deep Dive — the final architectural walkthrough.