Keycloak
Keycloak Docker: how to run Keycloak in production
Run Keycloak in Docker the way its docs describe for production, with PostgreSQL, a hostname and HTTPS through a reverse proxy. Or skip the setup and get managed Keycloak on Zenith.
· $20 a month on Zenith · PostgreSQL included · OpenID Connect and SAML
Deploy with ZenithThe short version
Host it on Zenith if
- You want Keycloak in production mode without setting up PostgreSQL, TLS and a reverse proxy.
- You want a public HTTPS address your applications can reach for sign-in.
- You want backups and updates handled for you.
Run it yourself if
- Keycloak has to sit inside your private network, next to an LDAP or Active Directory server that is not on the internet.
- You want to keep the Administration Console off the public internet, as Keycloak's docs advise.
- You need a custom Keycloak build with your own providers or themes.
What Keycloak needs in production
To run Keycloak with Docker, use the quay.io/keycloak/keycloak image. start-dev runs it on your computer with one command. For production, run start with a PostgreSQL database, a hostname and HTTPS, usually with Docker Compose and a reverse proxy in front. Both are below.
Keycloak is an identity and access manager. It gives your applications a shared sign-in over OpenID Connect, OAuth 2.0 and SAML, and keeps users, roles and clients in separate realms. It ships as one container image, quay.io/keycloak/keycloak.
Keycloak has two modes. start-dev runs with HTTP, a local file database and relaxed hostname checks, and its docs say to keep it out of production. start is secure by default. It will not start until you give it:
- A hostname. The public address your applications and users reach it on.
- HTTPS. Either a certificate inside Keycloak, or a reverse proxy that handles TLS for it.
- A database. PostgreSQL 14 to 18, MySQL, MariaDB and others. The dev-file database is deprecated in production mode.
You also need a domain pointed at your server, and Docker with Compose.
Run Keycloak in Docker for testing
To look around on your own computer, run the dev-mode container from Keycloak's docs. It creates a temporary admin account from the two environment variables.
docker run --name keycloak -p 127.0.0.1:8080:8080 \
-e KC_BOOTSTRAP_ADMIN_USERNAME=admin \
-e KC_BOOTSTRAP_ADMIN_PASSWORD=change_me \
quay.io/keycloak/keycloak:26.8.2 start-devOpen http://localhost:8080 and sign in to the Administration Console. Don't put this container on the internet.
Keycloak Docker Compose setup for production
This setup runs Keycloak in production mode with PostgreSQL, behind Caddy for HTTPS. The proxy ends TLS and passes plain HTTP to Keycloak, which Keycloak's reverse proxy guide calls edge termination.
- 1
Write the compose file
Save this as
docker-compose.yml. Replaceauth.example.comwith your domain, and both passwords with long random values. Keycloak only listens on127.0.0.1, so the only way in is through the proxy.services: postgres: image: postgres:17 restart: unless-stopped environment: POSTGRES_DB: keycloak POSTGRES_USER: keycloak POSTGRES_PASSWORD: change-me-db volumes: - pgdata:/var/lib/postgresql/data healthcheck: test: ["CMD", "pg_isready", "-U", "keycloak", "-d", "keycloak"] interval: 5s retries: 10 keycloak: image: quay.io/keycloak/keycloak:26.8.2 command: start restart: unless-stopped environment: KC_DB: postgres KC_DB_URL: jdbc:postgresql://postgres:5432/keycloak KC_DB_USERNAME: keycloak KC_DB_PASSWORD: change-me-db KC_HOSTNAME: https://auth.example.com KC_HTTP_ENABLED: "true" KC_PROXY_HEADERS: xforwarded KC_HEALTH_ENABLED: "true" KC_BOOTSTRAP_ADMIN_USERNAME: admin KC_BOOTSTRAP_ADMIN_PASSWORD: change-me-admin ports: - "127.0.0.1:8080:8080" depends_on: postgres: condition: service_healthy volumes: pgdata:KC_HTTP_ENABLEDis needed because the proxy ends TLS.KC_PROXY_HEADERStells Keycloak to trust the proxy'sX-Forwarded-*headers. Without it you get 403 Forbidden responses. - 2
Start Keycloak and PostgreSQL
docker compose up -d docker compose logs -f keycloakThe first start is slow while Keycloak creates its database tables. Wait until the log says Keycloak has started.
- 3
Put HTTPS in front with Caddy
Install Caddy on the same server and add your domain to its
Caddyfile. Caddy gets a certificate for the domain and sets theX-Forwarded-*headers Keycloak reads.auth.example.com { reverse_proxy 127.0.0.1:8080 }Reload Caddy, then open
https://auth.example.com/admin. - 4
Replace the temporary admin
Sign in with the bootstrap admin. Keycloak only uses those variables when the database is first set up, and calls the account temporary. In the master realm, create a permanent administrator with two-factor sign-in, then delete the bootstrap account.
- 5
Create a realm for your applications
Leave the master realm for administrators. In the Administration Console, choose Manage realms, then Create realm, and give it a name. Under Users, create a user, then set a password on the Credentials tab with Temporary turned off.
- 6
Add a client and test sign-in
Under Clients, click Create client, choose OpenID Connect, and give it a client ID. Keep Standard flow on and set the redirect URIs and web origins to your application's address. Your application finds its OpenID Connect settings at
https://auth.example.com/realms/<realm>/.well-known/openid-configuration. To check the user can sign in, openhttps://auth.example.com/realms/<realm>/account.
To update, change the image tag, then run docker compose pull and docker compose up -d. Back up the pgdata volume, or take regular pg_dump backups, before every upgrade.
What you skip on Zenith
| On your own server | On Zenith | |
|---|---|---|
| Server | Rent a server and install Docker | Included |
| Database | Run PostgreSQL, set its password and keep it backed up | Included, with 5 GB of storage |
| Hostname and HTTPS | Point a domain, run a reverse proxy, get certificates, set forwarded headers | Your own HTTPS address, or your domain |
| First sign-in | Choose and store a bootstrap admin password | Generated for you and shown in your dashboard |
| Updates | Change the tag, pull and restart | Included |
| Backups | Dump the database yourself | Managed backups, best effort |
Zenith does not set up email for Keycloak. To send password reset and verification emails, add your SMTP provider under Realm settings, then Email.
Deploy Keycloak on Zenith
- 1
Click Deploy now on the Keycloak page
Open Keycloak in the catalogue and click Deploy now. It costs $20 a month. If you don't have a Zenith account yet, you create one after paying.
- 2
Choose an address
Pick a name, then click Use this address. Your address becomes the name, a hyphen and your username, ending in
.zh.run. Keycloak's hostname is set to it for you. - 3
Sign in to the Administration Console
When Keycloak is live, open it and sign in with the admin username and password listed under Your credentials. Keycloak takes a few minutes to start the first time.
- 4
Make a permanent admin and your first realm
Create a permanent administrator in the master realm and remove the temporary one. Then create a realm and a client for each application, as in the steps above.
Managed Keycloak without the setup
$20 a month, running in production mode with PostgreSQL, on its own HTTPS address, with managed backups and updates.
Common questions
- What is the Keycloak Docker image?
- quay.io/keycloak/keycloak, which is the image Keycloak's own docs use. It is also on Docker Hub as keycloak/keycloak. Run it with start-dev to try it, or start for production.
- What is Keycloak?
- An identity and access manager. It gives your applications single sign-on over OpenID Connect, OAuth 2.0 and SAML, and manages users, roles and clients in realms.
- How much does Keycloak cost?
- On Zenith it costs $20 a month, with PostgreSQL included. If you run it yourself, you pay for the server it runs on.
- Why does Keycloak fail when I run start?
- Production mode needs a hostname and HTTPS. Without them, Keycloak stops with an error on purpose. Set KC_HOSTNAME, and either give it a certificate or put it behind a reverse proxy with KC_HTTP_ENABLED and KC_PROXY_HEADERS.
- Which database does Keycloak need?
- For production, an external database such as PostgreSQL. Keycloak 26 supports PostgreSQL 14 to 18. The built-in dev-file database is only for development.
- Why do I get 403 Forbidden behind a reverse proxy?
- Keycloak ignores forwarded headers unless you set KC_PROXY_HEADERS to xforwarded or forwarded. Your proxy must also set and overwrite those headers.
- Is there managed Keycloak hosting?
- Yes. Zenith runs Keycloak in production mode with PostgreSQL and an HTTPS address for $20 a month. You sign in with an admin account created at deploy time.