943 words
5 min read

Install Keycloak Using Docker Compose

By · Solutions Architect · Docker Captain · IBM Champion
Cover image for the post 'Install Keycloak Using Docker Compose'

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.

TIP

Architecture 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.

NOTE

Supply 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’s SECURITY.md for the disclosure policy and the production checklist in the README before exposing this to real users.

TIP

Tested 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.

heyvaldemar
/
keycloak-traefik-letsencrypt-docker-compose
Waiting for api.github.com...
0
0
N/A
Waiting...
NOTE

Traefik 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.

CAUTION

To 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.

IMPORTANT

Docker 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

IMPORTANT

OpenSSH 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:

Terminal window
sudo apt install openssh-server
NOTE

To connect to the server from a Windows system, you can use tools like PuTTY or MobaXterm.

NOTE

This guide walks you through connecting to a server with the iTerm2 terminal emulator on macOS.

CAUTION

You 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:

Terminal window
docker network create traefik-network

And Keycloak’s:

Terminal window
docker network create keycloak-network

Now clone the repository. It carries the configuration files, and with them everything Keycloak needs to run:

Terminal window
git clone https://github.com/heyvaldemar/keycloak-traefik-letsencrypt-docker-compose.git

Move into the directory you just cloned:

Terminal window
cd keycloak-traefik-letsencrypt-docker-compose

The 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:

Terminal window
cp .env.example .env

Open .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_HOSTNAME and KEYCLOAK_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'
IMPORTANT

The .env file must be in the same directory as keycloak-traefik-letsencrypt-docker-compose.yml.

TIP

Fail-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 up fails immediately with a clear error — you cannot accidentally deploy the stack with placeholder credentials.

Start Keycloak:

Terminal window
docker compose -f keycloak-traefik-letsencrypt-docker-compose.yml -p keycloak up -d

Now 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.

NOTE

You need to specify the domain name of the service, previously defined in the .env file.

Click the “Administration Console” button. Sign in with the KEYCLOAK_ADMIN_USERNAME and KEYCLOAK_ADMIN_PASSWORD you set in .env.

CAUTION

Rotate 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.

NOTE

You need to specify the domain name of the service, previously defined in the .env file.

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.

TIP

What 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 IPAllowList middleware, or skip exposing it publicly and rely on docker compose logs. The full production checklist is in the repository README.


Vladimir Mikhalev

Docker Captain  ·  IBM Champion  ·  AWS Community Builder

The Verdict — production-tested analysis on YouTube.

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

Same category
  1. 1
    Install ownCloud Using Docker Compose
    Self-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.
  2. 2
    Install Docmost Using Docker Compose
    Self-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.
  3. 3
    Install AFFiNE Using Docker Compose
    Self-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.
  4. 4
    Install Homebox Using Docker Compose
    Self-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

Random
  1. 1
    Distinctions Between Terminal, Command Line, Shell, and Prompt
    SysAdmin & IT Pro · Learn the differences between terminal, command line, shell, and prompt in Linux. A beginner-friendly guide to essential CLI concepts and tools.
  2. 2
    Install Kubernetes on Ubuntu Server 22.04 LTS
    DevOps & 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.
  3. 3
    Install Windows Server 2012 R2
    SysAdmin & 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.
  4. 4
    Install and Configure DHCP Server on Windows Server 2012 R2
    SysAdmin & 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.
Install Keycloak Using Docker Compose
https://heyvaldemar.com/install-keycloak-using-docker-compose/
Author
Vladimir Mikhalev
Published
2023-09-01
License
CC BY-NC-SA 4.0