Skip to content

Keycloak Docker Compose generator

Generate a Docker Compose file for Keycloak with PostgreSQL, in production mode with start, a pinned image, a health check on the management port and secrets kept in a .env file.

Runs in your browser. Nothing you paste is sent to us.

Settings

Mode

26.8.1 is the version we run. Edit it to the version you want; avoid latest.

Becomes KC_HOSTNAME. A made-up address is filled in.

compose.yaml

compose.yaml
# Keycloak and PostgreSQL, production mode.
# Pin the image tags to the versions you have tested.
#   docker compose up -d
services:
  postgres:
    image: postgres:17
    restart: unless-stopped
    environment:
      POSTGRES_DB: keycloak
      POSTGRES_USER: keycloak
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}
    volumes:
      - postgres-data:/var/lib/postgresql/data
    healthcheck:
      test: ['CMD-SHELL', 'pg_isready -U keycloak -d keycloak']
      interval: 5s
      timeout: 3s
      retries: 20

  keycloak:
    image: quay.io/keycloak/keycloak:26.8.1
    restart: unless-stopped
    command: ['start']
    environment:
      KC_DB: 'postgres'
      KC_DB_URL_HOST: 'postgres'
      KC_DB_URL_DATABASE: 'keycloak'
      KC_DB_USERNAME: 'keycloak'
      KC_DB_PASSWORD: '${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}'
      KC_HOSTNAME: 'https://auth.example.com'
      KC_HTTP_ENABLED: 'true'
      KC_PROXY_HEADERS: 'xforwarded'
      KC_HEALTH_ENABLED: 'true'
      KC_METRICS_ENABLED: 'true'
      KC_BOOTSTRAP_ADMIN_USERNAME: '${KC_BOOTSTRAP_ADMIN_USERNAME:-temp-admin}'
      KC_BOOTSTRAP_ADMIN_PASSWORD: '${KC_BOOTSTRAP_ADMIN_PASSWORD:?set KC_BOOTSTRAP_ADMIN_PASSWORD}'
    ports:
      - '127.0.0.1:8080:8080'
    depends_on:
      postgres:
        condition: service_healthy
    healthcheck:
      test:
        [
          'CMD-SHELL',
          "exec 3<>/dev/tcp/127.0.0.1/9000 && printf 'GET /health/ready HTTP/1.1\\r\\nHost: localhost\\r\\nConnection: close\\r\\n\\r\\n' >&3 && head -1 <&3 | grep -q 200",
        ]
      interval: 10s
      timeout: 5s
      retries: 30
      start_period: 60s

volumes:
  postgres-data:
.env
# .env next to compose.yaml. Random values made in your browser; keep this file private.
POSTGRES_PASSWORD=
KC_BOOTSTRAP_ADMIN_USERNAME=temp-admin
KC_BOOTSTRAP_ADMIN_PASSWORD=
Behind a proxy
Keycloak listens on HTTP 8080 and trusts X-Forwarded headers, so only your proxy may reach it, and the proxy must overwrite those headers. The reverse proxy tool writes a matching config.
The bootstrap admin is temporary: sign in, create a permanent admin, then delete it. Port 9000 (health and metrics) is not published; the health check uses it inside the container.

How to use it

  1. Pick production or development mode.
  2. Set the image tag to the Keycloak version you want.
  3. Enter the public address and how HTTPS reaches Keycloak.
  4. Copy compose.yaml and .env, then run docker compose up -d.

What to know

Production mode (kc.sh start) needs a hostname, a database and HTTPS, either from Keycloak itself or from a proxy in front. Development mode (start-dev) skips those checks and is for your own machine only.

The generated file pins the image, waits for PostgreSQL to be healthy, and checks Keycloak’s /health/ready on the management port 9000. The image has no curl, so the check speaks HTTP over bash.

The bootstrap admin it creates is temporary. Sign in, create a permanent administrator, then remove the temporary one.

Why a Compose file is a good first home for Keycloak

Docker Compose is the shortest path from nothing to a Keycloak server that behaves like a real one. One file describes Keycloak, its PostgreSQL database, the volume that keeps the data, and the order in which they start. You can read it in a minute, keep it in Git, and run the same thing on a laptop and on a single server.

That matters because Keycloak is easy to start badly. The quick start-dev command runs with an embedded development database, plain HTTP and relaxed hostname checks. It is perfect for a first look and wrong for anything users depend on. A Compose file that uses production mode from day one avoids the painful move later, when realms, users and clients already live in a database you did not plan for.

This generator writes that file for you: Keycloak in production mode with start, PostgreSQL as the database, a pinned image tag, a health check on the management port, and secrets kept in a separate .env file.

Production mode and development mode in Keycloak 26

Keycloak has two ways to start. They use the same image and differ in the defaults they apply.

start (production)start-dev (development)
HTTPOff unless you set http-enabled=trueOn
HTTPSExpected, from Keycloak or a proxy in frontNot required
HostnameRequired (hostname-strict is on)Taken from the request
Theme cachingOnOff, so theme edits show on reload
Intended forServers people rely onYour own machine

Production mode refuses to start without a hostname, unless you turn hostname-strict off, which you should only do in development. It also expects HTTPS. Behind a reverse proxy that ends TLS, you set http-enabled=true and proxy-headers=xforwarded so Keycloak listens on HTTP 8080 and trusts the forwarded headers from the proxy.

Since Keycloak 25, health checks and metrics are served on a separate management interface, port 9000 by default. They are off until you set health-enabled=true and metrics-enabled=true.

What the generated file contains

The file has two services and one named volume. Here is the shape of the Keycloak service for a server behind a proxy, with the made-up address the tool fills in:

compose.yaml (excerpt)
keycloak:
  image: quay.io/keycloak/keycloak:26.8.1
  restart: unless-stopped
  command: ['start']
  environment:
    KC_DB: 'postgres'
    KC_DB_URL_HOST: 'postgres'
    KC_DB_URL_DATABASE: 'keycloak'
    KC_DB_USERNAME: 'keycloak'
    KC_DB_PASSWORD: '${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD}'
    KC_HOSTNAME: 'https://auth.example.com'
    KC_HTTP_ENABLED: 'true'
    KC_PROXY_HEADERS: 'xforwarded'
    KC_HEALTH_ENABLED: 'true'
  ports:
    - '127.0.0.1:8080:8080'
  depends_on:
    postgres:
      condition: service_healthy
  • PostgreSQL runs with its own health check (pg_isready), and Keycloak waits until it passes.
  • Keycloak gets a health check that asks /health/ready on port 9000 inside the container.
  • Passwords come from .env through Compose variable substitution, so the YAML can live in Git.
  • Behind a proxy, the file publishes port 8080 on 127.0.0.1 only, unless you choose otherwise, so only a proxy on the same host can reach it.
  • Port 9000 is never published. Health and metrics stay inside the container network.

Why the health check uses bash instead of curl

The official Keycloak image does not ship curl or wget. The generated check opens a TCP connection with bash’s /dev/tcp, writes a minimal HTTP request for /health/ready, and looks for a 200 status line. It needs health-enabled=true, which the file always sets.

The temporary bootstrap admin

Keycloak 26 creates a first administrator from KC_BOOTSTRAP_ADMIN_USERNAME and KC_BOOTSTRAP_ADMIN_PASSWORD when the master realm has no admin yet. That account is meant to be temporary. Sign in, create a permanent administrator, then delete the bootstrap one.

What this tool does and does not do

The generator builds text in your browser from the choices you make. It does not contact your server, check that your image tag exists, or test the file.

  • It writes production mode (start) or development mode (start-dev).
  • It supports two HTTPS setups: a reverse proxy in front, or Keycloak serving HTTPS from certificate files you mount.
  • It can turn on metrics and mount ./import to import realm files on start.
  • It makes random passwords for .env in your browser with the Web Crypto API.
  • It does not write the reverse proxy config. Use the reverse proxy config tool for that.
  • It does not build an optimized image with kc.sh build, set up clustering across hosts, or plan backups.

How to use it

  1. Pick production mode unless you are only trying Keycloak on your own machine.
  2. Set the Keycloak image to the exact version you want to run. The tool fills in 26.8.1, the version we run; edit it to yours.
  3. Enter the public address people will use, such as your own auth subdomain. It becomes KC_HOSTNAME.
  4. Choose how HTTPS reaches Keycloak: a proxy in front, or certificate files in ./certs.
  5. Decide on metrics and realm import.
  6. Copy compose.yaml and .env into one folder on the server, keep .env private, then run docker compose up -d.
  7. Watch docker compose ps until Keycloak shows as healthy, then sign in with the bootstrap admin and replace it.

Where it pays off

A Compose file fits more situations than people expect. These are the common ones.

A first production deploy on one server

For a small product, an internal tool or a staging environment, one server with Keycloak, PostgreSQL and a proxy is a sound start. Production mode, a pinned image and database backups cover most of what goes wrong early.

Behind an existing proxy or load balancer

If nginx, Caddy, Traefik or a cloud load balancer already ends TLS for your other services, choose the proxy option. Keycloak listens on HTTP and trusts the X-Forwarded headers, so the proxy must overwrite them on every request.

A local copy of production

Developers often need a Keycloak that matches production closely enough to test sign-in flows. Use the same image tag as production, import a sanitized realm export from ./import, and switch to development mode if you want theme changes to show without a restart.

Trying an upgrade

Before you move production to a new Keycloak version, copy the database, change the image tag in a copy of the Compose file, and start it. Keycloak migrates the database schema on start, so test on a copy and keep a backup of the original.

Common errors and how to fix them

Keycloak exits saying the hostname is not configured

Production mode needs KC_HOSTNAME. Set it to the public URL, including https. Turning hostname-strict off makes the error go away but lets Keycloak build URLs from the Host header, which is unsafe outside development.

Keycloak cannot reach the database

Check that KC_DB_URL_HOST matches the Compose service name (postgres) and that the password in .env is the one PostgreSQL was created with. PostgreSQL only reads POSTGRES_PASSWORD when its volume is empty, so changing it later does not change the real password.

Redirect loops or http links behind the proxy

This usually means Keycloak does not see the original scheme. Make sure the proxy sends X-Forwarded-Proto and KC_PROXY_HEADERS is set to xforwarded. The hostname settings helper walks through the combinations.

The container stays unhealthy

Keycloak can take a while on first start, which is why the check has a start period. If it never turns healthy, read docker compose logs keycloak. A wrong option name or a database error shows up there.

Security notes

  • Keep .env out of Git and readable only by the account that runs Docker.
  • Never publish port 9000. Health and metrics are for your monitoring, not the internet.
  • With the proxy setup, bind 8080 to 127.0.0.1 or a private network. Anyone who reaches it directly can send forged X-Forwarded headers.
  • Expose only /realms/ and /resources/ through the proxy unless people truly need the admin console from outside.
  • Delete the bootstrap admin once a permanent administrator exists.

Best practices

  • Pin an exact version tag. Avoid latest, which can move you to a new major version on restart.
  • Back up the PostgreSQL volume on a schedule and test a restore.
  • Upgrade on a copy first, then in production, one version step at a time if the release notes ask for it.
  • Keep the realm configuration in a sanitized export as well, so you can review changes over time.
  • Use the configuration converter when you move these settings to another format.

When Compose is no longer enough

Compose runs on one host. When you need several Keycloak nodes, rolling upgrades without downtime, or automatic failover, Kubernetes with the Keycloak Operator is the usual next step. The operator resource generator writes the matching custom resource.

If you would rather not run Keycloak at all, we run upstream Keycloak on a dedicated cluster per customer. See the plans on pricing.

Questions

Frequently asked questions

How do I run Keycloak with Docker Compose in production?

Use the `start` command, not `start-dev`, with a PostgreSQL service, `KC_HOSTNAME` set to the public URL, and HTTPS from Keycloak or a proxy. Pin the image version and keep passwords in an `.env` file.

What is the difference between start and start-dev?

`start` is production mode: it expects a hostname and HTTPS and caches themes. `start-dev` listens on plain HTTP, relaxes the hostname checks and turns off theme caching. Use it only on your own machine.

Which database should Keycloak use in Docker Compose?

PostgreSQL is a common choice and the one this generator writes. Keycloak also supports MySQL, MariaDB, Microsoft SQL Server and Oracle. Do not use the development database for anything you need to keep.

How do I add a health check for Keycloak in Docker Compose?

Set `KC_HEALTH_ENABLED=true` and check `/health/ready` on port 9000. The image has no curl, so the generated check sends the HTTP request with bash’s `/dev/tcp`.

How do I create the first admin user?

Set `KC_BOOTSTRAP_ADMIN_USERNAME` and `KC_BOOTSTRAP_ADMIN_PASSWORD`. Keycloak creates a temporary admin in the master realm on first start. Replace it with a permanent administrator.

How do I import a realm on startup?

Mount the realm JSON files into `/opt/keycloak/data/import` and add `--import-realm` to the command. A realm that already exists is skipped, so the import only takes effect the first time.

Should I use the latest tag for the Keycloak image?

No. Pin an exact version so a restart or a pull cannot move you to a new release without testing. Change the tag on purpose when you upgrade.

Why does Keycloak redirect to http behind my proxy?

Keycloak is not reading the original scheme. Set `KC_PROXY_HEADERS=xforwarded` and make sure the proxy sends `X-Forwarded-Proto`. Setting `KC_HOSTNAME` to a full https URL also fixes the address in links and tokens.

Rather not run Keycloak yourself?

We run it for you on a cluster of your own, with backups, upgrades and written support. Starter and Premium begin with a 7-day trial.