Deploying a Keycloak App
Introduction
Keycloak is an open-source identity and access management solution that provides SSO, OAuth2, and OIDC. Deploying Keycloak with a Dockerfile on Klutch.sh gives you reproducible builds, managed secrets, and persistent storage for realms and user data—all configured from klutch.sh/app. This guide covers installation, repository prep, a production-ready Dockerfile, deployment steps, Nixpacks overrides, and best practices for secure authentication.
Prerequisites
- A Klutch.sh account (create one)
- A GitHub repository containing your Keycloak configuration (GitHub is the only supported git source)
- Docker familiarity and basic Keycloak/Quarkus knowledge
- Database credentials (PostgreSQL recommended)
For onboarding help, see the Quick Start.
Architecture and ports
- Keycloak serves HTTP on port
8080; set the internal container port to8080in Klutch.sh. - PostgreSQL should run as a separate Klutch.sh TCP app, exposed on port
8000, connected internally on5432. - Persistent storage is recommended for
/opt/keycloak/datato retain themes, exports, and stateful data.
Repository layout
keycloak/├── themes/ # Custom themes (optional, mount as volume)├── providers/ # Custom SPI providers (optional)├── Dockerfile # Must be at repo root for auto-detection├── import/ # Realm export files (optional)└── README.mdKeep secrets out of Git; store them in Klutch.sh environment variables.
Installation (local) and starter commands
Quick local run with the official image:
docker run --rm -p 8080:8080 \ -e KEYCLOAK_ADMIN=admin \ -e KEYCLOAK_ADMIN_PASSWORD=change_me \ quay.io/keycloak/keycloak:24.0.5 start-devOptional helper start.sh for portability and Nixpacks fallback:
#!/usr/bin/env bashset -euo pipefailexec /opt/keycloak/bin/kc.sh start --optimizedMake it executable with chmod +x start.sh.
Dockerfile for Keycloak (production-ready)
Place this Dockerfile at the repo root; Klutch.sh auto-detects it (no Docker selection in the UI):
FROM quay.io/keycloak/keycloak:24.0.5
WORKDIR /opt/keycloak
# Copy custom themes or providers if neededCOPY themes /opt/keycloak/themesCOPY providers /opt/keycloak/providersCOPY import /opt/keycloak/data/import
EXPOSE 8080CMD ["/opt/keycloak/bin/kc.sh", "start", "--optimized"]Notes:
- Pin the Keycloak image tag for reproducible builds.
- If you import realms at startup, keep files under
/opt/keycloak/data/importand set the appropriate env vars.
Environment variables (Klutch.sh)
Set these in the Klutch.sh app settings (Secrets tab) before deploying:
PORT=8080KEYCLOAK_ADMIN=<admin-user>KEYCLOAK_ADMIN_PASSWORD=<admin-password>KC_DB=postgresKC_DB_URL=jdbc:postgresql://<host>:<port>/<db>KC_DB_USERNAME=<db-user>KC_DB_PASSWORD=<db-password>KC_HOSTNAME=example-app.klutch.shKC_HEALTH_ENABLED=trueKC_METRICS_ENABLED=true(optional)
If you deploy without the Dockerfile and need Nixpacks overrides:
NIXPACKS_BUILD_CMD="echo Keycloak uses prebuilt image"NIXPACKS_START_CMD=/opt/keycloak/bin/kc.sh start --optimizedNIXPACKS_JDK_VERSION=17
These keep Keycloak compatible with Nixpacks defaults when a Dockerfile is absent.
Attach persistent volumes
In Klutch.sh storage settings, add mount paths and sizes (no names required):
/opt/keycloak/data— for imports, themes, and stateful data./opt/keycloak/providers— optional if you add custom SPI providers at runtime.
Ensure these paths are writable inside the container.
Deploy Keycloak on Klutch.sh (Dockerfile workflow)
- Push your repository (with the Dockerfile at the root) to GitHub.
- Open klutch.sh/app, create a project, and add an app.
- Connect the GitHub repository; Klutch.sh automatically detects the Dockerfile.
- Choose HTTP traffic for Keycloak.
- Set the internal port to
8080. - Add the environment variables above (admin credentials, DB settings, hostname, and any
NIXPACKS_*overrides if you temporarily deploy without the Dockerfile). - Attach persistent volumes for
/opt/keycloak/data(and/opt/keycloak/providersif used), selecting sizes that fit your realm exports and themes. - Deploy. Your Keycloak admin console will be reachable at
https://example-app.klutch.sh; attach a custom domain if desired.
Sample admin API call
Obtain a token and list realms (replace placeholders and secure secrets):
export KEYCLOAK_URL=https://example-app.klutch.shexport ADMIN_USER=adminexport ADMIN_PASS=change_me
TOKEN=$(curl -s -X POST "$KEYCLOAK_URL/realms/master/protocol/openid-connect/token" \ -H "Content-Type: application/x-www-form-urlencoded" \ -d "username=$ADMIN_USER" -d "password=$ADMIN_PASS" -d "grant_type=password" -d "client_id=admin-cli" \ | jq -r .access_token)
curl -s -H "Authorization: Bearer $TOKEN" "$KEYCLOAK_URL/admin/realms" | jq .Health checks and production tips
- Enable health endpoints (
KC_HEALTH_ENABLED=true) and probe/healthvia your reverse proxy. - Enforce HTTPS at the edge; forward HTTP to port 8080 internally.
- Rotate admin credentials and DB passwords regularly; store them only in Klutch.sh secrets.
- Monitor disk usage on
/opt/keycloak/dataand resize volumes before they fill. - Keep image tags pinned; upgrade intentionally alongside database migrations.
Keycloak on Klutch.sh combines reproducible Docker builds with managed secrets, persistent storage, and flexible HTTP/TCP routing. With the Dockerfile at the repo root and port 8080 configured, you can deliver secure identity and SSO without extra YAML or workflow overhead.