Run Keycloak Using Compose File To Test Localy
· 2 min
docker-composekeycloak
A local Keycloak with a real database, in three commands and no repository to clone. You need Docker and a terminal (on Windows, use Git Bash or WSL for the commands below).
1. Create the Compose file
Paste this into a terminal. It writes keycloak-compose.yml to the current folder:
cat > keycloak-compose.yml <<'EOF'
services:
keycloak_web:
image: quay.io/keycloak/keycloak:23.0.7
container_name: keycloak_web
environment:
KC_DB: postgres
KC_DB_URL: jdbc:postgresql://keycloakdb:5432/keycloak
KC_DB_USERNAME: keycloak
KC_DB_PASSWORD: password
KC_HOSTNAME: localhost
KC_HOSTNAME_PORT: 8080
KC_HOSTNAME_STRICT: false
KC_HOSTNAME_STRICT_HTTPS: false
KC_LOG_LEVEL: info
KC_METRICS_ENABLED: true
KC_HEALTH_ENABLED: true
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
command: start-dev
depends_on:
- keycloakdb
ports:
- 8080:8080
keycloakdb:
image: postgres:15
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
volumes:
postgres_data:
EOF
It starts two containers: Keycloak 23.0.7 in development mode, and a PostgreSQL 15 database that keeps its data in a Docker volume, so your realms and users survive a restart.
2. Start it and wait until it is ready
docker compose -f keycloak-compose.yml up -d
The first start downloads the images. Keycloak needs a little while after the containers are up; this command waits for it and then tells you:
until curl -sf http://localhost:8080/health/ready > /dev/null; do sleep 2; done && echo "Keycloak is ready"
To watch the log instead, run docker compose -f keycloak-compose.yml logs -f keycloak_web.

3. Open the admin console
Open http://localhost:8080 in your browser.

Click Administration Console, or go straight to http://localhost:8080/admin. Sign in with username admin and password admin, as defined in the file.

You are ready to configure your local Keycloak.

Check from the terminal
If you prefer to check without a browser, ask Keycloak for an admin token. A long access_token in the answer means the login works:
curl -s -d grant_type=password -d client_id=admin-cli -d username=admin -d password=admin \
http://localhost:8080/realms/master/protocol/openid-connect/token
Stop it
docker compose -f keycloak-compose.yml down # stop, keep the data
docker compose -f keycloak-compose.yml down -v # stop and delete the data too
If port 8080 is taken
Something else on your machine may already use port 8080, and Docker will answer port is already allocated. In the file, change the left number of 8080:8080 and the hostname port to the same new number, for example 8081, and use that port everywhere above:
KC_HOSTNAME_PORT: 8081
...
- 8081:8080
Only for your own machine
This setup is for testing. It runs in development mode (start-dev), the admin password is admin and the database password is password. Do not expose it to a network, and do not use it as it is in production.