Keycloak is an open platform for authentication and authorization that supports OAuth2, OpenID Connect, and SAML protocols. The service completely eliminates the need to create a login and password system from scratch: it provides secure login, allows you to work with all applications in one account, and easily integrates with any services.

Keycloak can be deployed locally or in the cloud, as well as packaged in a container. The option of installing on your own VDS in conjunction with Docker is one of the most convenient: this approach allows you to quickly upgrade a working system, simplifies its updating, and, if necessary, allows you to transfer to another server without complicated settings.

In this article, we will take a step-by-step look at the Keycloak installation process in a container: from preparing a virtual server to configuring access via HTTPS and a domain name.

Server Requirements

Keycloak is a resource—intensive application: it consumes a significant amount of memory (RAM), and also works with external databases and HTTP requests, loading the processor (CPU).

System requirements for installation:

  • The minimum configuration for testing is 1 vCPU, 1 GB RAM, 10-20 GB SSD. It is only suitable for a trial run.
  • The optimal configuration for production is at least 2 vCPUs, 2 GB RAM, and 40 GB SSD.
  • The configuration for stable operation under load is from 2 vCPUs and 4 GB of RAM, which will provide a reserve of resources for the Java machine, reduce authentication delays and increase fault tolerance.

If the number of users and connected services is growing, the amount of RAM and the number of CPUs should be increased to avoid performance problems.

Installation Instructions

Connect to the server via SSH and enter the command:

sudo apt update && sudo apt upgrade -y

Install Docker

To do this, you need to update the repositories, install the necessary software, and launch the containers.

Create the infrastructure

Minimal infrastructure will be required for the service to work correctly.:

  • Keycloak is a container with the authorization server itself.
  • PostgreSQL is an external database. Keycloack has a built-in H2 database, but it is only suitable for testing and initial setup.
  • Nginx is optional, for reverse proxy and HTTPS.

Let’s look at how to deploy all dependencies inside a docker-compose file.

Create an initial project folder

To do this, enter the command:

mkdir ~/opt  ~/opt/keycloak && cd ~/opt/keycloak

In the folder you need to create:

  • .env file with passwords and environment variables;
  • docker-compose.yml description of the services;
  • nginx.conf file for the configuration of the reverse proxy.

Create a file with environment variables

Put the secrets and passwords in a separate .env file.:

nano .env

Enter the code with the main variables that will be used in the build.:

# Versions
POSTGRES_VERSION=15
KC_VERSION=26.3

# Database
POSTGRES_DB=keycloak
POSTGRES_USER=keycloak
POSTGRES_PASSWORD=supersecret

# Keycloak admin
KEYCLOAK_ADMIN=admin
KEYCLOAK_ADMIN_PASSWORD=admin123

# Domain
DOMAIN=keycloakkb.example.com

# Let's Encrypt
LETSENCRYPT_EMAIL=admin@keycloakkb.example.com

Add docker-compose.yml

Create a docker-compose file.yml for describing the launch of the database, Keycloak, and reverse proxy with the following contents:

nano  docker-compose.yml

Enter a description of the launch of the services:

version: "3.9"

services:
  postgres:
    image: postgres:${POSTGRES_VERSION}
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "pg_isready", "-U", "${POSTGRES_USER}"]
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
    networks:
      - keycloak-network

  keycloak:
    image: quay.io/keycloak/keycloak:${KC_VERSION}
    command:
      - start
      - --db=postgres
      - --hostname=${DOMAIN}
      - --proxy-headers=xforwarded
      - --health-enabled=true
      - --metrics-enabled=true
      - --http-enabled=true
    restart: unless-stopped
    environment:
      KC_DB: postgres
      KC_DB_USERNAME: ${POSTGRES_USER}
      KC_DB_PASSWORD: ${POSTGRES_PASSWORD}
      KC_DB_URL: "jdbc:postgresql://postgres:5432/${POSTGRES_DB}"
      KEYCLOAK_ADMIN: ${KEYCLOAK_ADMIN}
      KEYCLOAK_ADMIN_PASSWORD: ${KEYCLOAK_ADMIN_PASSWORD}
    depends_on:
      postgres:
        condition: service_healthy
    volumes:
      - ./keycloak_metrics:/opt/keycloak/metrics
    networks:
      - keycloak-network

  nginx:
    image: nginx:latest
    container_name: keycloak_nginx
    restart: unless-stopped
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf:ro
      - ./letsencrypt:/etc/letsencrypt
      - ./certbot-www:/var/www/certbot
    depends_on:
      - keycloak
    networks:
      - keycloak-network

  certbot:
    image: certbot/certbot:latest
    container_name: certbot
    volumes:
      - ./letsencrypt:/etc/letsencrypt
      - ./certbot-www:/var/www/certbot
    entrypoint: >
      sh -c "certbot certonly --webroot
      --webroot-path=/var/www/certbot
      --email ${LETSENCRYPT_EMAIL}
      --agree-tos
      --no-eff-email
      -d ${DOMAIN}"

networks:
  keycloak-network:
    driver: bridge

To run Keycloak, add the following flags to the code::

start

The Keycloak launch command. This is a standard entrypoint — without it, the container will not start the server itself.

--db=postgres

Specifies which database to use. In our case, PostgreSQL. Keycloak supports other databases (MySQL/MariaDB, Oracle, MSSQL), but PostgreSQL is recommended for production.

--proxy-headers=xforwarded

Instructs Keycloak to trust the X-Forwarded-* headers that the reverse proxy throws (Nginx, Traefik, etc.). Without this parameter, Keycloak may incorrectly determine the scheme (http/https), the client’s IP address, and the host.

--health-enabled=true

Enables the health endpoint (/health) to check the Keycloak status. Useful for monitoring (Prometheus, Zabbix, etc.), Kubernetes, and Docker healthchecks.

--metrics-enabled=true

Includes Prometheus metrics (/metrics). It allows you to collect statistics about sessions, logins, and workload in order to use the data for building graphs and dashboards.

--http-enabled=true

Enables Keycloak over HTTP (port 8080). It is usually used to work behind a reverse proxy (Nginx), which already handles HTTPS. If you leave only HTTPS inside the container, you will have to configure SSL directly in Keycloak — this is a less flexible approach.

Create an Nginx configuration (without HTTPS)

nano  nginx.conf

Enter the basic code for proxying:

events {}

http {
    server {
        listen 80;
        server_name keycloakkbt.fvds.ru;

        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }

        location / {
            proxy_pass http://keycloak:8080;
        }
    }
}

Launching containers

Launch the main services for operation, except for cerbot, with the command:

docker compose up -d --no-deps --build postgres keycloak nginx

After installing the containers, enter the docker ps command to see the running containers and make sure that the necessary ones are working. Expected output of the command:

Open the container logs:

docker logs keycloak-keycloak-1

Keycloak 26.3.5 on JVM (powered by Quarkus 3.20.3) started at 25.374s means that the service has started.

Install the SSL certificate using cerbot

A secure HTTPS connection is necessary for proper operation. Run the container with the command:

docker compose run --rm certbot

Replace nginx.conf with the certificate option:

events {}

http {
    server {
        listen 80;
        server_name keycloakkb.example.com;
        location /.well-known/acme-challenge/ {
            root /var/www/certbot;
        }
        location / {
            return 301 https://$host$request_uri;
        }
    }

    server {
        listen 443 ssl;
        server_name keycloakkbtest.example.com;

        ssl_certificate     /etc/letsencrypt/live/keycloakkbtest.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/keycloakkbtest.example.com/privkey.pem;

        ssl_protocols       TLSv1.2 TLSv1.3;
        ssl_ciphers         HIGH:!aNULL:!MD5;

        location / {
            proxy_pass http://keycloak:8080;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto https;
        }
    }
}

Restart the containers with the command:

docker compose restart

Go to the domain you specified. An authentication window will appear in Keycloak.

Log in to the platform using the data specified in .env, in our case, admin and admin123.

Change the password for the main account.

According to the security rules, the password must be changed immediately. To do this, go to the “Users” section:

Find the administrative account:

Go to the “Credentials” tab and click on reset password:

Installing Keycloak on a virtual server using Docker and an external database allows you to deploy a reliable and scalable authentication and authorization system. The service simplifies user management, supports modern security protocols, and integrates seamlessly with external applications. And the installation instructions will help you create a ready-made infrastructure for use in both small projects and industrial systems.