Keycloak
Open source identity and access management.
Overview
Keycloak is an open-source identity and access management solution originally developed by Red Hat that provides enterprise-grade authentication and authorization services. Born from the need to simplify complex identity management across distributed applications, Keycloak supports modern protocols like OpenID Connect, OAuth 2.0, and SAML 2.0, making it a cornerstone for implementing single sign-on (SSO) and centralized user management in modern architectures.
This stack combines Keycloak with PostgreSQL to create a robust identity management platform where PostgreSQL serves as the production-grade database backend for storing user credentials, sessions, and configuration data. Unlike Keycloak's default H2 database which is suitable only for development, PostgreSQL provides the reliability, performance, and scalability needed for production identity workloads with proper ACID compliance and concurrent user support.
This combination is ideal for organizations transitioning from development to production identity management, enterprises implementing SSO across multiple applications, and development teams building microservices that require centralized authentication. The PostgreSQL backend ensures data integrity for critical identity data while Keycloak's flexible realm and client model allows you to manage multiple applications and user groups from a single control plane.
Key Features
- Multi-realm architecture for tenant isolation and organizational boundaries
- OpenID Connect and OAuth 2.0 support for modern application integration
- Social login integration with Google, Facebook, GitHub, and other providers
- User federation with LDAP and Active Directory for enterprise environments
- Fine-grained role-based access control with custom attributes and policies
- Customizable login themes and user registration flows
- PostgreSQL backend with ACID compliance for reliable identity data storage
- Built-in admin console for realm management and user administration
Common Use Cases
- 1Enterprise SSO implementation across internal web applications and services
- 2Microservices authentication with JWT token validation and user context
- 3Customer identity management for SaaS platforms with multi-tenant requirements
- 4API security with OAuth 2.0 client credentials and access token validation
- 5Legacy application modernization by adding OIDC authentication layers
- 6Development environment identity services for testing authentication flows
- 7Homelab central authentication for self-hosted services like Grafana and GitLab
Prerequisites
- Minimum 1.5GB RAM available (1GB for Keycloak, 512MB for PostgreSQL)
- Port 8080 available for Keycloak web interface and API access
- Basic understanding of OAuth 2.0/OIDC concepts and token-based authentication
- Familiarity with realm and client configuration in identity management systems
- Docker Compose environment with persistent volume support for database storage
For development & testing. Review security settings, change default credentials, and test thoroughly before production use. See Terms
docker-compose.yml
docker-compose.yml
1services: 2 keycloak: 3 image: quay.io/keycloak/keycloak:latest4 container_name: keycloak5 command: start-dev6 environment: 7 KC_DB: postgres8 KC_DB_URL: jdbc:postgresql://postgres:5432/${DB_NAME}9 KC_DB_USERNAME: ${DB_USER}10 KC_DB_PASSWORD: ${DB_PASSWORD}11 KEYCLOAK_ADMIN: ${ADMIN_USER}12 KEYCLOAK_ADMIN_PASSWORD: ${ADMIN_PASSWORD}13 ports: 14 - "8080:8080"15 depends_on: 16 - postgres17 networks: 18 - keycloak1920 postgres: 21 image: postgres:16-alpine22 container_name: keycloak-postgres23 environment: 24 POSTGRES_DB: ${DB_NAME}25 POSTGRES_USER: ${DB_USER}26 POSTGRES_PASSWORD: ${DB_PASSWORD}27 volumes: 28 - postgres_data:/var/lib/postgresql/data29 networks: 30 - keycloak3132volumes: 33 postgres_data: 3435networks: 36 keycloak: 37 driver: bridge.env Template
.env
1DB_NAME=keycloak2DB_USER=keycloak3DB_PASSWORD=changeme4ADMIN_USER=admin5ADMIN_PASSWORD=changemeUsage Notes
- 1Docs: https://www.keycloak.org/documentation
- 2Admin console at http://localhost:8080/admin
- 3Create realm for each app/org, then clients within realms
- 4For production: use 'start' instead of 'start-dev', add KC_HOSTNAME
- 5OIDC endpoint: /realms/{realm}/.well-known/openid-configuration
- 6Export realm: docker exec keycloak /opt/keycloak/bin/kc.sh export
Individual Services(2 services)
Copy individual services to mix and match with your existing compose files.
keycloak
keycloak:
image: quay.io/keycloak/keycloak:latest
container_name: keycloak
command: start-dev
environment:
KC_DB: postgres
KC_DB_URL: jdbc:postgresql://postgres:5432/${DB_NAME}
KC_DB_USERNAME: ${DB_USER}
KC_DB_PASSWORD: ${DB_PASSWORD}
KEYCLOAK_ADMIN: ${ADMIN_USER}
KEYCLOAK_ADMIN_PASSWORD: ${ADMIN_PASSWORD}
ports:
- "8080:8080"
depends_on:
- postgres
networks:
- keycloak
postgres
postgres:
image: postgres:16-alpine
container_name: keycloak-postgres
environment:
POSTGRES_DB: ${DB_NAME}
POSTGRES_USER: ${DB_USER}
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- keycloak
Quick Start
terminal
1# 1. Create the compose file2cat > docker-compose.yml << 'EOF'3services:4 keycloak:5 image: quay.io/keycloak/keycloak:latest6 container_name: keycloak7 command: start-dev8 environment:9 KC_DB: postgres10 KC_DB_URL: jdbc:postgresql://postgres:5432/${DB_NAME}11 KC_DB_USERNAME: ${DB_USER}12 KC_DB_PASSWORD: ${DB_PASSWORD}13 KEYCLOAK_ADMIN: ${ADMIN_USER}14 KEYCLOAK_ADMIN_PASSWORD: ${ADMIN_PASSWORD}15 ports:16 - "8080:8080"17 depends_on:18 - postgres19 networks:20 - keycloak2122 postgres:23 image: postgres:16-alpine24 container_name: keycloak-postgres25 environment:26 POSTGRES_DB: ${DB_NAME}27 POSTGRES_USER: ${DB_USER}28 POSTGRES_PASSWORD: ${DB_PASSWORD}29 volumes:30 - postgres_data:/var/lib/postgresql/data31 networks:32 - keycloak3334volumes:35 postgres_data:3637networks:38 keycloak:39 driver: bridge40EOF4142# 2. Create the .env file43cat > .env << 'EOF'44DB_NAME=keycloak45DB_USER=keycloak46DB_PASSWORD=changeme47ADMIN_USER=admin48ADMIN_PASSWORD=changeme49EOF5051# 3. Start the services52docker compose up -d5354# 4. View logs55docker compose logs -fOne-Liner
Run this command to download and set up the recipe in one step:
terminal
1curl -fsSL https://docker.recipes/api/recipes/keycloak/run | bashTroubleshooting
- Keycloak fails to start with database connection errors: Verify PostgreSQL container is healthy and database credentials match between services
- Admin console shows 'Invalid credentials' after setup: Check KEYCLOAK_ADMIN and KEYCLOAK_ADMIN_PASSWORD environment variables are properly set
- Applications can't reach OIDC endpoints: Ensure Keycloak is accessible at the hostname configured in your client applications, not just localhost
- User sessions not persisting after container restart: Verify postgres_data volume is properly mounted and PostgreSQL is storing session data
- High memory usage and slow performance: Increase Keycloak container memory limit and tune PostgreSQL shared_buffers for your user load
- Realm export/import fails with permission errors: Run export commands with proper user permissions inside the container using docker exec
Community Notes
Loading...
Loading notes...
Download Recipe Kit
Get all files in a ready-to-deploy package
Includes docker-compose.yml, .env template, README, and license
Ad Space
Shortcuts: C CopyF FavoriteD Download