1024 words
5 min read

Install Keycloak Using Docker Compose

By · Solutions Architect · Docker Captain · IBM Champion
Install Keycloak Using Docker Compose

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.

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

We’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.

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

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.

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:

Terminal window
docker network create traefik-network

We create a network for Keycloak using the command:

Terminal window
docker network create keycloak-network

Next, 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:

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

Navigate to the directory with the repository using the command:

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 create your own:

Terminal window
cp .env.example .env

Open .env and replace the placeholders with real values. The required fields are:

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

Now let’s start Keycloak with the command:

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

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

NOTE

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

Click on the “Administration Console” button, then 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.

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.

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.

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
    10 Real Terraform Interview Questions (and Expert Answers!) — 2025 DevOps Guide
    DevOps & Cloud · Ace your Terraform interview with 10 real questions, expert answers, and best practices on state, drift, modules, and security.
  2. 2
    Docker Desktop's Performance Odyssey Over a Year of Innovations
    Opinion & Culture · I'm thrilled to walk you through the remarkable transformation Docker Desktop has undergone over the past year.
  3. 3
    Amazon Project Dawn Cut 30,000 Jobs — Including the Head of AWS Community Builders. Here's What It Means.
    Opinion & Culture · Amazon laid off Jason Dunn, the architect of the AWS Community Builders program. This isn't the death of community — it's the signal that community must prove production value, not just engagement metrics.
  4. 4
    Infosys Deploys Devin AI Globally — And Your DevOps Career Just Became Legacy Labor
    Opinion & Culture · Infosys just deployed Devin AI globally. If you are a DevOps engineer competing on technical execution, you are now "Legacy Labor". Here is the blueprint to survive.
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