Self-host Keycloak with Docker Compose
Identity and access management with single sign-on for your applications.
keycloak
postgres
postgres:18.3-alpine3.23
postgres-postgres-data-init
busybox
postgres-postgres-socket-init
busybox
The steps below take a few minutes end to end.
Requirements
You'll need these installed on the machine you're deploying to:
Docker
Packages Keycloak and everything it depends on into an isolated container, so it runs the same way on your machine as it does everywhere else.
Install guideDocker Compose
Reads docker-compose.yml and starts everything in it together. Ships with Docker Desktop. On Linux servers it's usually a separate install.
Install guide1. Docker compose setup and file
Create a folder for Keycloak and save the file below into it as docker-compose.yml. It describes every container the stack needs — Keycloak itself and any supporting services, such as its database — along with the ports, volumes and environment variables each one uses. You'll also need an empty .env file in the same folder; Docker Compose reads it automatically and uses it to fill in the ${VARIABLE} references you'll see in the file below.
docker-compose.yml
version: "3.9"
services:
keycloak:
image: quay.io/keycloak/keycloak:latest
restart: unless-stopped
ports:
- 8080:8080
environment:
JAVA_OPTS_KC_HEAP: -XX:MaxRAMPercentage=60
KC_BOOTSTRAP_ADMIN_PASSWORD: ${SETUP_ADMIN_PASSWORD}
KC_BOOTSTRAP_ADMIN_USERNAME: admin
KC_CACHE: local
KC_DB: postgres
KC_DB_PASSWORD: ${RANDOM_PG_PASSWORD}
KC_DB_URL: jdbc:postgresql://postgres:5432/app
KC_DB_USERNAME: app
KC_HOSTNAME: http://keycloak.localhost:8080
KC_HTTP_ENABLED: "true"
KC_PROXY_HEADERS: xforwarded
PG_DBNAME: app
PG_HOST: postgres
PG_PORT: "5432"
volumes:
- quarkus:/opt/keycloak/lib/quarkus
- data:/opt/keycloak/data
- tmp:/tmp
depends_on:
postgres:
condition: service_started
command:
- start
postgres:
image: postgres:18.3-alpine3.23
restart: unless-stopped
environment:
PGDATA: /var/lib/postgresql/data
POSTGRES_PASSWORD: ${RANDOM_PG_PASSWORD}
POSTGRES_USER: app
volumes:
- postgres-data:/var/lib/postgresql/data
- postgres-socket:/var/run/postgresql
depends_on:
postgres-postgres-data-init:
condition: service_completed_successfully
postgres-postgres-socket-init:
condition: service_completed_successfully
postgres-postgres-data-init:
image: busybox
volumes:
- postgres-data:/var/lib/postgresql/data
command:
- chown
- 70:70
- /var/lib/postgresql/data
postgres-postgres-socket-init:
image: busybox
volumes:
- postgres-socket:/var/run/postgresql
command:
- chown
- 70:70
- /var/run/postgresql
volumes:
data: null
postgres-data: null
postgres-socket: null
quarkus: null
tmp: null
Volumes
Keycloak stores its data in named Docker volumes, so it survives container restarts and updates:
- quarkus mounted at /opt/keycloak/lib/quarkus: Keycloak's build output, regenerated at startup for the configured database.
- data mounted at /opt/keycloak/data: Keycloak's working directory for temporary and cache files. Realms, users and sessions are stored in Postgres.
- tmp mounted at /tmp: Temporary working files.
2. Secret generation
Keycloak needs a few randomly generated secrets — for example, database passwords or an internal session key — before it can start. These are referenced from docker-compose.yml above but don't live in it, so they need to end up in your .env file. Pick one of the two options below.
Generating secrets…
3. Start Keycloak with Docker Compose
From the folder with docker-compose.yml and .env, run:
Terminal
Start Keycloak and all its supporting services in the background.
docker compose up -dThe -d flag runs the stack in the background so it keeps running after you close the terminal. Docker will pull the images the first time, which can take a minute or two.
To check on it afterwards: docker compose ps shows whether containers are healthy, and docker compose logs -f follows their logs if something looks wrong. Once it's running, open http://localhost:8080 in your browser.