composebuilder

Docker Compose: Directus + PostgreSQL

Run Directus 11 headless CMS on PostgreSQL 16 in Docker Compose, with required keys, admin bootstrap, persistent uploads, a health check and a checklist.

When to use this stack

This stack runs Directus 11, an open source headless CMS and data platform that puts a database behind a no-code admin app and a REST and GraphQL API. Directus works with an existing PostgreSQL schema or creates its own tables, so it suits teams that want an editing interface on top of data they already keep in SQL. Compose starts the database first, waits until it accepts connections, then starts Directus with the admin account bootstrapped from environment variables. Uploaded files and extensions sit in named volumes, so updates do not remove them.

Choose it when you need a visual admin for content, product data or internal records, plus a generated API that a website or app can consume without writing a backend. It is a strong fit for small teams that want role-based permissions and a data model they can edit in the browser. Do not choose it when your logic is mostly custom workflows that need full code control, or when you expect massive traffic that needs a dedicated caching tier. Directus also stores data in tables with its own metadata, so treat the schema as part of your application and back it up accordingly.

Directus does not require a Dockerfile, because the official image already contains the application. The compose file sets the database connection with the DB_* variables and the security keys with KEY and SECRET. The admin account is created only on the first run, so keep ADMIN_EMAIL and ADMIN_PASSWORD in .env as the initial values and change the password in the admin panel afterwards.

docker-compose.yml

services:
  directus:
    image: directus/directus:11.9.0
    restart: unless-stopped
    depends_on:
      db:
        condition: service_healthy
    ports:
      - "8055:8055"
    environment:
      KEY: ${DIRECTUS_KEY}
      SECRET: ${DIRECTUS_SECRET}
      DB_CLIENT: pg
      DB_HOST: db
      DB_PORT: 5432
      DB_DATABASE: ${POSTGRES_DB}
      DB_USER: ${POSTGRES_USER}
      DB_PASSWORD: ${POSTGRES_PASSWORD}
      ADMIN_EMAIL: ${ADMIN_EMAIL}
      ADMIN_PASSWORD: ${ADMIN_PASSWORD}
      PUBLIC_URL: ${PUBLIC_URL}
    volumes:
      - directus_uploads:/directus/uploads
      - directus_extensions:/directus/extensions
    healthcheck:
      test: ["CMD-SHELL", "wget --no-verbose --tries=1 --spider http://localhost:8055/server/health || exit 1"]
      interval: 15s
      timeout: 5s
      retries: 5
      start_period: 30s

  db:
    image: postgres:16-alpine
    restart: unless-stopped
    environment:
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
      POSTGRES_DB: ${POSTGRES_DB}
    volumes:
      - postgres_data:/var/lib/postgresql/data
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
      interval: 10s
      timeout: 5s
      retries: 5

volumes:
  postgres_data:
  directus_uploads:
  directus_extensions:

.env

.env
# Project key (a UUID) and secret used to sign tokens. Generate once and keep them stable.
DIRECTUS_KEY=CHANGE_ME_uuid_project_key
DIRECTUS_SECRET=CHANGE_ME_random_secret_at_least_32_chars
# PostgreSQL database, user and password
POSTGRES_DB=directus
POSTGRES_USER=directus
POSTGRES_PASSWORD=CHANGE_ME_postgres_password
# Initial administrator, created only on the first bootstrap
ADMIN_EMAIL=[email protected]
ADMIN_PASSWORD=CHANGE_ME_admin_password
# Public address of Directus, with scheme and no trailing slash
PUBLIC_URL=http://localhost:8055

Start and verify

Shell
docker compose config --quiet

# Start PostgreSQL and then Directus
docker compose up -d

# db and directus must both show "healthy" after the start period
docker compose ps

# Read the log for the bootstrap messages and "Server started"
docker compose logs --tail=80 directus

# The health endpoint returns JSON with status ok
curl http://localhost:8055/server/health

# The admin login page should answer
curl -I http://localhost:8055/admin/login

Production checklist

  • Put Directus behind HTTPS with a reverse proxy and set PUBLIC_URL to the HTTPS address, so file links and redirects are correct.
  • Generate KEY and SECRET with a random generator, store them outside Git, and never change SECRET on a running system without a plan.
  • Change the admin password right after the first login, and create named users with the smallest roles they need.
  • Restrict public role permissions to the collections and fields that must be readable by anonymous visitors.
  • Keep PostgreSQL unpublished, which this file does, and use a strong POSTGRES_PASSWORD.
  • Back up postgres_data with pg_dump and archive directus_uploads, because files live outside the database.
  • Pin directus/directus and postgres:16-alpine, and read the Directus upgrade notes before each minor release, since schema migrations run on start.
  • Enable the rate limiter and cache settings in Directus if the API is public.

Common problems

  • Symptom: Directus exits with “Please provide a KEY” or a SECRET error. Cause: One of the required keys is empty in .env. Fix: Generate both values, put them in .env and run docker compose up -d directus.
  • Symptom: The admin login fails with the password you put in .env. Cause: ADMIN_PASSWORD only applies on the first bootstrap of an empty database. Fix: Reset the password from the Directus user settings, or recreate the database on a test install with docker compose down -v.
  • Symptom: Uploaded files return 404. Cause: The uploads volume is missing or the file storage settings point elsewhere. Fix: Confirm that directus_uploads is mounted at /directus/uploads and that the storage location is local.
  • Symptom: Links in the admin area point to localhost after deployment. Cause: PUBLIC_URL was not updated. Fix: Change PUBLIC_URL in .env and recreate the directus container.
  • Symptom: The directus container shows unhealthy during the first start. Cause: Migrations are still running, and the start period is too short for a slow server. Fix: Wait, read the logs, and increase start_period if the migrations finish after the default window.

Variations

  • Caddy with HTTPS: Add a caddy:2 service that listens on 80 and 443 and proxies to directus:8055. Remove the public 8055 mapping so only the proxy is exposed.
  • S3 storage for uploads: Set STORAGE_LOCATIONS to s3 and the matching S3 variables from the Directus documentation, then remove the directus_uploads volume. This is better when you need several Directus containers.
  • Existing PostgreSQL schema: Point DB_DATABASE at a database that already has tables, then run Directus introspection in the admin area. Test on a copy first, because Directus adds its own system tables.

Questions

Is there a folder I need to build from?

No. Directus runs from the official directus/directus image, so this compose file builds nothing. Custom extensions go in a named volume mounted at /directus/extensions.

What do KEY and SECRET do?

KEY identifies the project and SECRET signs tokens and sessions. Generate both once with a random generator and keep them stable, because changing SECRET signs everyone out.

Do ADMIN_EMAIL and ADMIN_PASSWORD change later?

They are used on the first bootstrap of an empty database. Changing them later does not update the existing admin account, so change the password in the Directus admin panel.

Adapt this stack

Load these files into the builder to rename services, change ports, add a cache or a proxy, and generate fresh passwords for the .env.

Open in builder
  • Django + PostgreSQL + Celery

    Run a Django app with Gunicorn, PostgreSQL 16, Redis 7 as the Celery broker and a Celery worker in Docker Compose, with healthchecks and a production checklist.

    • web
    • worker
    • db
    • redis
    Difficulty: Advanced
  • Laravel + PostgreSQL + Redis

    Run a Laravel app on Apache with PostgreSQL 16 and Redis 7 in Docker Compose, with a Dockerfile, queue and cache settings, volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Next.js + PostgreSQL + Redis

    Containerize a Next.js app with PostgreSQL 16 and Redis 7 in Docker Compose, using a minimal Dockerfile, healthchecks, named volumes and a production checklist.

    • app
    • db
    • redis
    Difficulty: Intermediate
  • Nuxt + PostgreSQL

    Run a Nuxt 3 app built with Nitro and PostgreSQL 16 in Docker Compose, with a Node 22 Dockerfile, a database healthcheck, persistent data and a checklist.

    • app
    • db
    Difficulty: Intermediate

Browse more Web & Frameworks stacks or the full library.