VPSFolio

2026-11-28 · directus · docker · cms · postgresql

Self-Host Directus with Docker Compose and PostgreSQL

Run Directus headless CMS in Docker with PostgreSQL and Redis, bind to localhost, terminate TLS with Caddy, and back up the database and uploads.

Bench run · 2026-11-28 Verified
directus/directus
Total RAM
262.3 MB
Startup
93.8 s
Port
8055
HTTP
302
Per container directus 205.6 MB · database 44.7 MB · cache 12.0 MB
directus/directus:11 · 3 containers · directus/directus

What Directus does and what it needs

Directus is a data platform that sits on top of an existing SQL database. It introspects your schema and generates a REST and GraphQL API, an admin app, and a permissions layer without copying your data into a proprietary store. You point it at PostgreSQL, MySQL, MariaDB, MS SQL, Oracle, or CockroachDB and it works with the tables you already have.

The runtime is a single Node service. It needs three things to be useful in production:

  • A SQL database (PostgreSQL is the safest default and the one this guide uses).
  • A cache and pub/sub backend. Redis is optional but strongly recommended once you run more than one instance or use the realtime/websocket features.
  • Persistent storage for uploaded files. Local disk works for a single node; S3-compatible object storage is the right answer for anything that scales.

The upstream project does not publish a minimum memory figure. Practical sizing depends on workload: an idle instance with a handful of collections runs comfortably in a small container, while heavy import jobs, large file uploads, and many concurrent API clients push memory and CPU up. Watch container RSS after a week of real traffic rather than trusting a number.

Prerequisites, ports, and volume constraints

You need:

  • A Linux VPS with Docker Engine 24+ and the Compose v2 plugin (docker compose version should print v2.x).
  • A domain name pointed at the server if you want HTTPS. You can test over an SSH tunnel without one.
  • Ports 80 and 443 free for the reverse proxy. Directus itself listens on 8055 inside the container; we bind it to 127.0.0.1:8055 so it is never reachable unencrypted from the public internet.

Volumes used by this stack:

  • directus_database — PostgreSQL data directory (/var/lib/postgresql/data).
  • directus_uploads — Directus local file storage (/directus/uploads), mounted into the app container.
  • directus_redis — Redis append-only file, only needed if you enable persistence.

Do not mount the Docker socket or arbitrary host paths. Everything the app needs lives in named volumes.

Production Docker Compose setup

Create a project directory and generate secrets first. Never ship the secret/Test@123 values from the upstream debug compose file.

mkdir -p /opt/directus && cd /opt/directus
openssl rand -hex 32   # KEY
openssl rand -hex 32   # SECRET
openssl rand -hex 24   # DB_PASSWORD

Write the environment file. Replace the three generated values and set PUBLIC_URL to your real HTTPS origin.

cat <<'EOF' > .env
DOMAIN=admin.example.com
PUBLIC_URL=https://admin.example.com
KEY=paste_generated_key_here
SECRET=paste_generated_secret_here
DB_PASSWORD=paste_generated_db_password_here
[email protected]
ADMIN_PASSWORD=paste_a_strong_admin_password_here
DB_CLIENT=pg
DB_HOST=database
DB_PORT=5432
DB_DATABASE=directus
DB_USER=directus
CACHE_ENABLED=true
CACHE_STORE=redis
REDIS=redis://cache:6379
STORAGE_LOCATIONS=local
STORAGE_LOCAL_DRIVER=local
STORAGE_LOCAL_ROOT=./uploads
TZ=UTC
EOF
chmod 600 .env

Now the compose file. It runs Directus, PostgreSQL, and Redis with healthchecks and named volumes, and binds the app to localhost only.

cat <<'EOF' > compose.yml
services:
  database:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_DB: ${DB_DATABASE:-directus}
      POSTGRES_USER: ${DB_USER:-directus}
      POSTGRES_PASSWORD: ${DB_PASSWORD}
    volumes:
      - directus_database:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${DB_USER:-directus} -d ${DB_DATABASE:-directus}"]
      interval: 10s
      timeout: 5s
      retries: 10

  cache:
    image: redis:7-alpine
    restart: unless-stopped
    command: ["redis-server", "--appendonly", "yes"]
    volumes:
      - directus_redis:/data
    healthcheck:
      test: ["CMD", "redis-cli", "ping"]
      interval: 10s
      timeout: 5s
      retries: 10

  directus:
    image: directus/directus:11
    restart: unless-stopped
    depends_on:
      database:
        condition: service_healthy
      cache:
        condition: service_healthy
    ports:
      - "127.0.0.1:8055:8055"
    environment:
      KEY: ${KEY}
      SECRET: ${SECRET}
      ADMIN_EMAIL: ${ADMIN_EMAIL}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}
      PUBLIC_URL: ${PUBLIC_URL}
      DB_CLIENT: ${DB_CLIENT:-pg}
      DB_HOST: ${DB_HOST:-database}
      DB_PORT: ${DB_PORT:-5432}
      DB_DATABASE: ${DB_DATABASE:-directus}
      DB_USER: ${DB_USER:-directus}
      DB_PASSWORD: ${DB_PASSWORD}
      CACHE_ENABLED: ${CACHE_ENABLED:-true}
      CACHE_STORE: ${CACHE_STORE:-redis}
      REDIS: ${REDIS:-redis://cache:6379}
      STORAGE_LOCATIONS: ${STORAGE_LOCATIONS:-local}
      STORAGE_LOCAL_DRIVER: ${STORAGE_LOCAL_DRIVER:-local}
      STORAGE_LOCAL_ROOT: ${STORAGE_LOCAL_ROOT:-./uploads}
      TZ: ${TZ:-UTC}
    volumes:
      - directus_uploads:/directus/uploads

volumes:
  directus_database:
  directus_uploads:
  directus_redis:
EOF

Bring it up and confirm the healthchecks pass:

docker compose up -d
docker compose ps
docker compose logs -f directus

Directus runs its schema bootstrap on first boot using ADMIN_EMAIL and ADMIN_PASSWORD. Once you see the startup banner, the API is live on 127.0.0.1:8055. To test without a domain, tunnel it from your laptop:

ssh -L 8055:127.0.0.1:8055 user@your-server
# then open http://localhost:8055

Reverse proxy, HTTPS, and first-run hardening

Directus serves both the admin SPA and the API from the same origin, so a single virtual host is enough. It also uses websockets for realtime updates, so the proxy must forward Upgrade and Connection headers. Uploads can be large; raise the body limit or file uploads will fail with a 413.

Install Caddy on the host (not in Compose) and point it at the localhost port:

sudo apt install -y caddy
sudo tee /etc/caddy/Caddyfile >/dev/null <<'EOF'
admin.example.com {
    encode zstd gzip
    request_body {
        max_size 100MB
    }
    reverse_proxy 127.0.0.1:8055 {
        header_up X-Forwarded-Proto {scheme}
        header_up X-Forwarded-Host {host}
    }
}
EOF
sudo systemctl reload caddy

Caddy handles ACME certificates automatically and passes websocket upgrades through by default. If you use Nginx instead, add proxy_set_header Upgrade $http_upgrade;, proxy_set_header Connection "upgrade";, client_max_body_size 100M;, and proxy_set_header X-Forwarded-Proto $scheme; to the location block.

First-run hardening checklist:

  • PUBLIC_URL must match the HTTPS origin exactly. Password reset links, file URLs, and OAuth redirects are built from it, and a mismatch produces broken emails and login loops.
  • KEY and SECRET are not interchangeable. KEY encrypts stored credentials; SECRET signs tokens. Rotating SECRET invalidates every session; rotating KEY can break stored secrets. Back both up.
  • After the first login, change the admin password in the UI and remove ADMIN_PASSWORD from .env. It is only read when the admin user is created.
  • Restrict the admin panel by IP at the proxy if it is internal-only, and keep the public API behind Directus role permissions rather than relying on network controls.

Updating, backups, and common pitfalls

Update the app image and dependencies in place:

cd /opt/directus
docker compose pull
docker compose up -d
docker compose logs -f directus

Directus runs database migrations on boot. Take a backup before every update and read the release notes for breaking changes between major versions.

Back up the database and uploads. The volume names are prefixed with the Compose project name, which defaults to the directory name (directus):

docker compose exec -T database pg_dump -U directus directus | gzip > directus-db-$(date +%F).sql.gz
docker run --rm -v directus_directus_uploads:/data -v "$PWD":/backup alpine \
  tar czf /backup/directus-uploads-$(date +%F).tar.gz -C /data .

Restore by piping the dump back into psql and extracting the uploads tarball into the volume. Test the restore on a staging box; an untested backup is a guess.

Two pitfalls that bite people in production:

  1. Local storage and multiple replicas. STORAGE_LOCAL_ROOT=./uploads writes to the container’s volume. If you scale Directus to more than one container, each replica sees only its own files. Move to an S3-compatible driver before scaling horizontally.
  2. Redis without persistence or auth. The default redis://cache:6379 is fine on a private Compose network, but never publish that port. If you enable appendonly, back up directus_redis too, or accept that cache and realtime state reset on restart.

Measured in our sandbox run: total memory 262.3 MB (sum of per-container peaks over a 30s idle window after startup) across 3 containers; largest container 205.6 MB; published port(s) 8055; HTTP probe returned 302. The run took 93.8s including the image download. Required secret(s) ADMIN_EMAIL, ADMIN_PASSWORD, DB_PASSWORD, KEY, PUBLIC_URL, SECRET were generated for the test run, as the tutorial instructs. Container breakdown: directus 205.6 MB, database 44.7 MB, cache 12.0 MB.


Verification: we ran this docker-compose stack in a disposable Docker environment and recorded the measurements above. They describe that run, not a guarantee for your host.