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) | |
|---|---|---|
| HTTP | Off unless you set http-enabled=true | On |
| HTTPS | Expected, from Keycloak or a proxy in front | Not required |
| Hostname | Required (hostname-strict is on) | Taken from the request |
| Theme caching | On | Off, so theme edits show on reload |
| Intended for | Servers people rely on | Your 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:
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/readyon port 9000 inside the container. - Passwords come from
.envthrough 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
./importto import realm files on start. - It makes random passwords for
.envin 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
- Pick production mode unless you are only trying Keycloak on your own machine.
- 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.
- Enter the public address people will use, such as your own auth subdomain. It becomes
KC_HOSTNAME. - Choose how HTTPS reaches Keycloak: a proxy in front, or certificate files in
./certs. - Decide on metrics and realm import.
- Copy
compose.yamland.envinto one folder on the server, keep.envprivate, then rundocker compose up -d. - Watch
docker compose psuntil 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
.envout 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.