Install Keycloak Using Docker Compose
By Vladimir Mikhalev · Solutions Architect · Docker Captain · IBM Champion
Want a straight, detailed walkthrough of getting Keycloak running under Docker Compose? This is it.
Keycloak is open-source software that handles single sign-on, identity, and access management for modern applications and services.
TIPArchitecture Context
Choose self-hosted Keycloak when your architecture requires an on-premises identity provider with full control over authentication flows, SAML/OIDC configuration, and user federation. Auth0 or Okta provide managed alternatives with faster setup and built-in compliance certifications. Self-hosting is justified when data residency rules prohibit external identity providers or when per-user SaaS pricing becomes prohibitive at scale.
NOTESupply chain posture
The template pins all three upstream images (Traefik, PostgreSQL, Keycloak) to immutable
@sha256:...digests in.env.example, rebuilds weekly via CI to catch upstream drift, and carries an OpenSSF Scorecard badge. See the repo’sSECURITY.mdfor the disclosure policy and the production checklist in the README before exposing this to real users.
TIPTested on every push
The deployment-verification workflow runs end-to-end backup/restore tests on every push, every pull request, and weekly. The tests boot the full compose stack and exercise backup creation, integrity (
gunzip -t), restore roundtrip, and prune logic. If you deploy this template literally and hit an issue, the green CI run is the evidence that the template itself works — most “doesn’t work” cases trace to DNS propagation, firewall rules, or hostname mismatches.
💾 You can find the repository used in this guide on GitHub.
NOTETraefik is our reverse proxy. It pulls cryptographic certificates from Let’s Encrypt for your domain names and routes incoming requests to the right service based on the domain.
CAUTIONTo obtain cryptographic certificates, you will need A-type records in the external DNS zone, which point to the IP address of your server where Traefik is installed. If you have created these records recently, you should wait before starting the installation of the services. Full replication of these records between DNS servers can take from a few minutes to 48 hours or even longer in rare cases.
IMPORTANTDocker Engine and Docker Compose must be installed on the server.
For a step-by-step guide on installing Docker Engine on Ubuntu Server, see Install Docker Engine and Docker Compose on Ubuntu Server
IMPORTANTOpenSSH must be installed on the server, and port 22 must be open to be able to connect to the server using the SSH protocol.
Need OpenSSH? Install it with:
sudo apt install openssh-serverNOTETo connect to the server from a Windows system, you can use tools like PuTTY or MobaXterm.
NOTEThis guide walks you through connecting to a server with the iTerm2 terminal emulator on macOS.
CAUTIONYou will need to open the following TCP ports for access to the services:
- TCP port 80 - to obtain a free cryptographic certificate through the Let’s Encrypt certification center.
- TCP port 443 - to access the Keycloak web interface.
Connect to the server where Keycloak is going to live.
First, the networks. Keycloak and Traefik each get their own.
Traefik’s network:
docker network create traefik-networkAnd Keycloak’s:
docker network create keycloak-networkNow clone the repository. It carries the configuration files, and with them everything Keycloak needs to run:
git clone https://github.com/heyvaldemar/keycloak-traefik-letsencrypt-docker-compose.gitMove into the directory you just cloned:
cd keycloak-traefik-letsencrypt-docker-composeThe repository ships a .env.example template with documented variables and change_me_* placeholders for credentials. The real .env file is gitignored. Copy the template to make your own:
cp .env.example .envOpen .env and swap the placeholders for real values. You need to fill in:
-
TRAEFIK_ACME_EMAIL— your email for Let’s Encrypt renewal notices. -
TRAEFIK_HOSTNAMEandKEYCLOAK_HOSTNAME— your real domain names. Both must resolve to this server’s public IP for the Let’s Encrypt TLS-ALPN challenge to succeed. -
KEYCLOAK_DB_PASSWORD— PostgreSQL password. Generate with:Terminal window openssl rand -base64 24 | tr -d '/+=' | head -c 32 -
KEYCLOAK_ADMIN_PASSWORD— Keycloak bootstrap admin password. Same generation command. -
TRAEFIK_BASIC_AUTH— BCrypt hash for the Traefik dashboard login. Generate with:Terminal window docker run --rm httpd:2.4 htpasswd -nbB traefikadmin 'YOUR_STRONG_PASSWORD' | sed 's/\$/\$\$/g'
IMPORTANTThe
.envfile must be in the same directory askeycloak-traefik-letsencrypt-docker-compose.yml.
TIPFail-fast protection
The compose file uses
${VAR:?...}syntax for every required variable. If any placeholder is left unchanged or any required variable is empty,docker compose upfails immediately with a clear error — you cannot accidentally deploy the stack with placeholder credentials.
Start Keycloak:
docker compose -f keycloak-traefik-letsencrypt-docker-compose.yml -p keycloak up -dNow open the management panel. From your workstation, go to https://keycloak.heyvaldemar.net. That domain is mine. Use your own, the one that points at the IP of your Traefik server, and Traefik forwards the request to Keycloak.
NOTEYou need to specify the domain name of the service, previously defined in the
.envfile.
Click the “Administration Console” button. Sign in with the KEYCLOAK_ADMIN_USERNAME and KEYCLOAK_ADMIN_PASSWORD you set in .env.
CAUTIONRotate the bootstrap admin
The bootstrap admin is intended to get you into the Keycloak UI on first start. Once inside, create your real admin users (ideally through Keycloak’s user federation or a second-factor-protected account), then disable or delete the bootstrap admin from the Keycloak UI. Leaving the bootstrap admin active in production is the single most common misconfiguration in self-hosted Keycloak deployments.
Traefik has its own panel. From your workstation, go to https://traefik.keycloak.heyvaldemar.net. Again, that one is mine, so point your own domain at the IP of your Traefik server.
NOTEYou need to specify the domain name of the service, previously defined in the
.envfile.
Authenticate with the Traefik dashboard credentials. The username (traefikadmin by default) and the plaintext password you passed to htpasswd when you generated the TRAEFIK_BASIC_AUTH BCrypt hash — not the hash itself. Browsers send the plaintext; Traefik verifies it against the stored BCrypt.
TIPWhat to do next
The Traefik dashboard is basic-auth-protected but basic auth is basic. For production, consider restricting the dashboard router to specific source IPs via Traefik’s
IPAllowListmiddleware, or skip exposing it publicly and rely ondocker compose logs. The full production checklist is in the repository README.
The Verdict
Inconvenient truths about shipping in the AI era
Container security, platform engineering, and the agentic shift — tested in production, argued without the hype. The verdict reaches your inbox the moment there's one worth sending.
Related Posts
- 1Install ownCloud Using Docker ComposeSelf-Hosting · Learn how to install ownCloud with Docker Compose on Ubuntu using Traefik and Let's Encrypt. Secure, scalable file storage and sharing for your server.
- 2Install Docmost Using Docker ComposeSelf-Hosting · Learn how to install Docmost using Docker Compose with Traefik and Let's Encrypt. Step-by-step guide for self-hosting a modern documentation platform.
- 3Install AFFiNE Using Docker ComposeSelf-Hosting · Step-by-step guide to install AFFiNE using Docker Compose with Traefik and Let's Encrypt. Build your open-source productivity platform in minutes.
- 4Install Homebox Using Docker ComposeSelf-Hosting · Step-by-step guide to install Homebox with Docker Compose and Traefik. Secure your home inventory system with HTTPS using Let's Encrypt.
Random Posts
- 1Distinctions Between Terminal, Command Line, Shell, and PromptSysAdmin & IT Pro · Learn the differences between terminal, command line, shell, and prompt in Linux. A beginner-friendly guide to essential CLI concepts and tools.
- 2Install Kubernetes on Ubuntu Server 22.04 LTSDevOps & Cloud · Step-by-step guide to install Kubernetes on Ubuntu Server 22.04 LTS using kubeadm. Learn how to set up master and worker nodes with containerd and Calico.
- 3Install Windows Server 2012 R2SysAdmin & IT Pro · Step-by-step guide to install Windows Server 2012 R2 using official media with a GUI setup. Get a clean, secure server installation.
- 4Install and Configure DHCP Server on Windows Server 2012 R2SysAdmin & IT Pro · Step-by-step guide to install and configure a DHCP server on Windows Server 2012 R2. Learn to assign IPs, set exclusions, and reserve addresses with ease.