Install Keycloak Using Docker Compose
By Vladimir Mikhalev · Solutions Architect · Docker Captain · IBM Champion
This article is for those looking for a detailed and straightforward guide on installing Keycloak using Docker Compose.
Keycloak is an open-source software that provides 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.
NOTEWe’ll use Traefik as our reverse proxy. It’ll handle obtaining cryptographic certificates from Let’s Encrypt for your domain names and route requests to the corresponding services based on those domains.
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 in order to be able to connect to the server using the SSH protocol.
To install OpenSSH on the server you can use the command:
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.
We connect to the server on which Keycloak is planned to be installed.
Now it is necessary to create networks for your services.
We create a network for Traefik using the command:
docker network create traefik-networkWe create a network for Keycloak using the command:
docker network create keycloak-networkNext, you need to clone the repository that contains the configuration files, which include all the necessary conditions for Keycloak to work.
You can clone the repository using the command:
git clone https://github.com/heyvaldemar/keycloak-traefik-letsencrypt-docker-compose.gitNavigate to the directory with the repository using the command:
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 create your own:
cp .env.example .envOpen .env and replace the placeholders with real values. The required fields are:
-
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.
Now let’s start Keycloak with the command:
docker compose -f keycloak-traefik-letsencrypt-docker-compose.yml -p keycloak up -dTo access the Keycloak management panel, go to https://keycloak.heyvaldemar.net from your workstation, where keycloak.heyvaldemar.net is the domain name of my service. Accordingly, you need to specify your domain name that points to the IP address of your server with the installed Traefik service, which will redirect the request to Keycloak.
NOTEYou need to specify the domain name of the service, previously defined in the
.envfile.
Click on the “Administration Console” button, then 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.
To access the Traefik control panel, go to https://traefik.keycloak.heyvaldemar.net from your workstation, where traefik.keycloak.heyvaldemar.net is the domain name of my service. Accordingly, you need to specify your domain name that points to the IP address of your server with the installed Traefik.
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.
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
- 1Install Ubuntu Server 18.04 LTSSysAdmin & IT Pro · Step-by-step guide to install Ubuntu Server 18.04 LTS. Learn disk setup, OpenSSH installation, user configuration, and post-installation steps for server deployment.
- 2Git Cheat SheetDevOps & Cloud · Fast and practical Git cheat sheet for developers. Learn essential Git commands for setup, commits, branching, merging, and more in one convenient guide.
- 3Install Minecraft Server Using Docker ComposeSelf-Hosting · Learn how to install a Minecraft Server using Docker Compose. Set up your own secure multiplayer server on Ubuntu with ease using this step-by-step guide.
- 4Install Bitbucket on Ubuntu ServerSelf-Hosting · Complete guide to install Bitbucket on Ubuntu Server with Apache, PostgreSQL, and SSL using Let's Encrypt. Ideal for secure Git-based team collaboration.