Self-Hosting with Docker Swarm
Docker Swarm provides native container orchestration with declarative rolling updates, integrated secrets management, and zero-downtime service rollouts without the overhead of Kubernetes.
This topology powers KoAkademy’s official one-line Linux installer.
Swarm Topology Overview
Section titled “Swarm Topology Overview”graph TD Internet((Public Internet)) -->|80/443| Caddy[Caddy Edge Ingress]
subgraph Swarm Overlay Network: koakademy_net Caddy -->|Internal 8000| App[KoAkademy FrankenPHP Service] App -->|Internal 5432| Postgres[(PostgreSQL 18)] App -->|Internal 6379| Redis[(Redis 8)] App -->|Internal 3000| Gotenberg[Gotenberg 8 PDF Service] end
subgraph Docker Secrets Sec1[koakademy_app_key] -.-> App Sec2[koakademy_db_password] -.-> App Sec2 -.-> Postgres Sec3[koakademy_redis_password] -.-> App Sec3 -.-> Redis endPrerequisites
Section titled “Prerequisites”- Linux host running Docker Engine 24+.
- Initialized Docker Swarm cluster (single-node manager or multi-node cluster):
Terminal window docker swarm init - Public domain pointing to the manager node IP.
Step 1: Create Overlay Network and Secrets
Section titled “Step 1: Create Overlay Network and Secrets”Create an attachable overlay network for the services:
docker network create --driver overlay --attachable koakademy_overlayCreate secure Docker Secrets for sensitive credentials:
# Generate App KeyAPP_KEY=$(docker run --rm ghcr.io/yukazakiri/koakademy:latest-franken php artisan key:generate --show)echo "$APP_KEY" | docker secret create koakademy_app_key -
# Database and Redis Passwordsopenssl rand -base64 32 | docker secret create koakademy_db_password -openssl rand -base64 32 | docker secret create koakademy_redis_password -Step 2: Define stack.yaml
Section titled “Step 2: Define stack.yaml”Create /opt/koakademy/app.env. The application, scheduler, and release migration
job share this file, and start-container automatically loads /run/secrets/koakademy_*
(or *_FILE paths) into the runtime environment:
cat >/opt/koakademy/app.env <<'ENV'APP_NAME="KoAkademy"APP_ENV="production"APP_DEBUG="false"APP_URL="https://school.example.com"PORTAL_HOST="school.example.com"ADMIN_HOST="school.example.com"TRUSTED_PROXIES="*"APP_KEY_FILE="/run/secrets/koakademy_app_key"OCTANE_SERVER="frankenphp"AUTO_MIGRATE="false"RUN_OPTIMIZE="foreground"
# Database ConnectionDB_CONNECTION="pgsql"DB_HOST="postgres"DB_PORT="5432"DB_DATABASE="koakademy"DB_USERNAME="koakademy"DB_PASSWORD_FILE="/run/secrets/koakademy_db_password"
# Redis ConnectionCACHE_STORE="redis"SESSION_DRIVER="redis"QUEUE_CONNECTION="redis"REDIS_HOST="redis"REDIS_PORT="6379"REDIS_PASSWORD_FILE="/run/secrets/koakademy_redis_password"
# PDF EngineGOTENBERG_URL="http://gotenberg:3000"FILESYSTEM_DISK="public"ENVCreate /opt/koakademy/stack.yaml:
version: "3.8"
services: caddy: image: caddy:2-alpine ports: - target: 80 published: 80 mode: host - target: 443 published: 443 mode: host volumes: - caddy_data:/data - caddy_config:/config - /opt/koakademy/Caddyfile:/etc/caddy/Caddyfile:ro networks: - koakademy_overlay deploy: replicas: 1 placement: constraints: - node.role == manager restart_policy: condition: on-failure
app: image: ghcr.io/yukazakiri/koakademy:latest-franken env_file: - /opt/koakademy/app.env environment: HORIZON_IN_HTTP_CONTAINER: "false" secrets: - koakademy_app_key - koakademy_db_password - koakademy_redis_password volumes: - app_storage:/app/storage networks: - koakademy_overlay deploy: replicas: ${APP_REPLICAS:-2} update_config: parallelism: 1 delay: 15s order: start-first failure_action: rollback rollback_config: parallelism: 1 order: start-first restart_policy: condition: on-failure
horizon: image: ghcr.io/yukazakiri/koakademy:latest-franken env_file: - /opt/koakademy/app.env environment: CONTAINER_MODE: "horizon" AUTO_MIGRATE: "false" RUN_OPTIMIZE: "false" RUN_SCOUT_SETTINGS: "false" secrets: - koakademy_app_key - koakademy_db_password - koakademy_redis_password volumes: - app_storage:/app/storage networks: - koakademy_overlay deploy: replicas: ${WORKER_REPLICAS:-1} restart_policy: condition: on-failure
scheduler: image: ghcr.io/yukazakiri/koakademy:latest-franken env_file: - /opt/koakademy/app.env environment: CONTAINER_MODE: "scheduler" AUTO_MIGRATE: "false" RUN_OPTIMIZE: "false" RUN_SCOUT_SETTINGS: "false" secrets: - koakademy_app_key - koakademy_db_password - koakademy_redis_password volumes: - app_storage:/app/storage networks: - koakademy_overlay deploy: replicas: ${WORKER_REPLICAS:-1} placement: constraints: - node.role == manager restart_policy: condition: on-failure
postgres: image: postgres:18-alpine environment: POSTGRES_DB: "koakademy" POSTGRES_USER: "koakademy" POSTGRES_PASSWORD_FILE: "/run/secrets/koakademy_db_password" secrets: - koakademy_db_password volumes: - postgres_data:/var/lib/postgresql networks: - koakademy_overlay deploy: replicas: 1 placement: constraints: - node.role == manager restart_policy: condition: on-failure
redis: image: redis:8-alpine command: ["sh", "-c", "exec redis-server --appendonly yes --requirepass \"$$(cat /run/secrets/koakademy_redis_password)\""] secrets: - koakademy_redis_password volumes: - redis_data:/data networks: - koakademy_overlay deploy: replicas: 1 restart_policy: condition: on-failure
gotenberg: image: gotenberg/gotenberg:8 networks: - koakademy_overlay deploy: replicas: 1 restart_policy: condition: on-failure
secrets: koakademy_app_key: external: true koakademy_db_password: external: true koakademy_redis_password: external: true
volumes: caddy_data: caddy_config: app_storage: postgres_data: redis_data:
networks: koakademy_overlay: external: trueStep 3: Configure Caddyfile
Section titled “Step 3: Configure Caddyfile”Create /opt/koakademy/Caddyfile:
school.example.com { reverse_proxy app:8000 { header_up Host {host} header_up X-Real-IP {remote_host} header_up X-Forwarded-For {remote_host} header_up X-Forwarded-Proto {scheme} }}Step 4: Run the Release Migrations
Section titled “Step 4: Run the Release Migrations”Migrations are a release step, not a container boot. The application starts only after the schema is prepared, so the application service is deployed without replicas first.
APP_REPLICAS=0 WORKER_REPLICAS=0 docker stack deploy -c /opt/koakademy/stack.yaml koakademyRun the migration as a one-shot job with the same environment, secrets and network as the application:
docker service rm koakademy_migrate >/dev/null 2>&1 || true
# The job runs on the image the rollout starts, so the image is pulled# first. A cached image would run the migrations of an older release.docker pull ghcr.io/yukazakiri/koakademy:latest-franken
docker service create \ --name koakademy_migrate \ --mode replicated-job \ --restart-condition on-failure \ --restart-max-attempts 3 \ --network koakademy_overlay \ --env-file /opt/koakademy/app.env \ --secret koakademy_app_key \ --secret koakademy_db_password \ --secret koakademy_redis_password \ --mount type=volume,source=koakademy_app_storage,target=/app/storage \ ghcr.io/yukazakiri/koakademy:latest-franken \ php artisan migrate --force --no-interactionWait for the job to finish, and check that it completed successfully:
docker service ps koakademy_migratedocker service logs koakademy_migrateStep 5: Deploy Stack
Section titled “Step 5: Deploy Stack”Deploy the stack to the Swarm with the application and worker replicas:
APP_REPLICAS=2 WORKER_REPLICAS=1 docker stack deploy -c /opt/koakademy/stack.yaml koakademyCheck the status of running tasks:
docker stack ps koakademydocker service logs -f koakademy_appZero-Downtime Rolling Upgrades
Section titled “Zero-Downtime Rolling Upgrades”To update KoAkademy to a new release without dropping traffic:
# 1. Pull the target release image and run the release migration job first.docker service rm koakademy_migrate >/dev/null 2>&1 || truedocker pull ghcr.io/yukazakiri/koakademy:v1.30.0-franken
docker service create \ --name koakademy_migrate \ --mode replicated-job \ --restart-condition on-failure \ --restart-max-attempts 3 \ --network koakademy_overlay \ --env-file /opt/koakademy/app.env \ --secret koakademy_app_key \ --secret koakademy_db_password \ --secret koakademy_redis_password \ --mount type=volume,source=koakademy_app_storage,target=/app/storage \ ghcr.io/yukazakiri/koakademy:v1.30.0-franken \ php artisan migrate --force --no-interaction
docker service ps koakademy_migratedocker service logs koakademy_migrate
# 2. Roll out the new image with start-first ordering once the migration job completes.docker service update \ --image ghcr.io/yukazakiri/koakademy:v1.30.0-franken \ --update-order start-first \ koakademy_app
docker service update \ --image ghcr.io/yukazakiri/koakademy:v1.30.0-franken \ koakademy_horizon
docker service update \ --image ghcr.io/yukazakiri/koakademy:v1.30.0-franken \ koakademy_schedulerSwarm will launch new tasks, wait for health check verification at /up, and cleanly drain connections from older containers before stopping them.
