Time to run something real. The pattern below works for most web applications regardless of language.
A project directory
mkdir -p ~/apps/myapp && cd ~/apps/myapp
The Compose file
nano compose.yml
services:
app:
image: yourregistry/yourapp:latest
restart: unless-stopped
ports:
- "127.0.0.1:8080:8080"
environment:
DATABASE_URL: postgres://app:${DB_PASSWORD}@db:5432/app
depends_on:
db:
condition: service_healthy
db:
image: postgres:17
restart: unless-stopped
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: ${DB_PASSWORD}
POSTGRES_DB: app
volumes:
- dbdata:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U app"]
interval: 10s
timeout: 5s
retries: 5
volumes:
dbdata:
Four things in there are doing real work:
127.0.0.1:8080:8080— the app is reachable by Caddy and not by the internet.- No ports on the database at all. Containers reach each other by service name on Docker's internal network. Publishing 5432 would be a mistake.
volumes: dbdata— without this, your database is deleted when the container is recreated. This is the single most expensive Docker mistake.healthcheckwithdepends_on— the app waits for a database that is actually accepting connections, not merely started.
Secrets in a file, not in the Compose file
nano .env
DB_PASSWORD=use-something-long-and-random
chmod 600 .env
Compose reads .env automatically. Keep it out of version
control — add it to .gitignore before the first commit, not
after, because removing a committed secret from history is considerably more
work than never committing it.
Start it
docker compose up -d
docker compose ps
docker compose logs -f app
Then point Caddy at 127.0.0.1:8080 as in the last chapter.
Updating
docker compose pull
docker compose up -d
docker image prune -f
Compose recreates only what changed. Database untouched, app replaced, seconds of downtime.
Make it repeatable
nano ~/apps/myapp/deploy.sh
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
echo "Backing up the database first..."
docker compose exec -T db pg_dump -U app app | gzip > "backup-$(date +%F-%H%M).sql.gz"
echo "Pulling and restarting..."
docker compose pull
docker compose up -d
echo "Done. Current state:"
docker compose ps
chmod +x deploy.sh
set -euo pipefail is the important line: the script stops at
the first failure instead of carrying on and leaving you in a half-deployed
state. And taking the backup before the deploy, not after, is what
makes a failed migration survivable.