Deploy Keycloak version 22.0.4 with docker compose

04/09/2023 - 1 phút

Follow  on Google News

Step 1: Edit the file on Linux and Mac /etc/hosts

127.0.0.1 keycloak.localhost

On Windows, the file path is usually:

c:\Windows\System32\Drivers\etc\hosts

Step 2: Clone the code

git clone https://github.com/akitectio/keycloak-compose
version: "3.9"
services:
  postgres:
    container_name: db
    image: "postgres:14.4"
    healthcheck:
      test: ["CMD", "pg_isready", "-q", "-d", "postgres", "-U", "root"]
      timeout: 45s
      interval: 10s
      retries: 10
    volumes:
      - postgres_data:/var/lib/postgresql/data
      - ./sql:/docker-entrypoint-initdb.d/:ro # turn it on, if you need run init DB
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: keycloak
      POSTGRES_HOST: postgres
    networks:
      - local
    ports:
      - "5432:5432"

  pgadmin:
    container_name: pgadmin
    image: "dpage/pgadmin4:5.1"
    environment:
      PGADMIN_DEFAULT_EMAIL: postgres@domain.local
      PGADMIN_DEFAULT_PASSWORD: postgres
    ports:
      - "5050:80"
    networks:
      - local

  keycloak:
    container_name: keycloak
    build:
      context: .
      args:
        KEYCLOAK_VERSION: 22.0.4
    command: ["start", "--optimized"]
    environment:
      JAVA_OPTS_APPEND: -Dkeycloak.profile.feature.upload_scripts=enabled
      KC_DB_PASSWORD: postgres
      KC_DB_URL: jdbc:postgresql://postgres/keycloak
      KC_DB_USERNAME: postgres
      KC_HEALTH_ENABLED: "true"
      KC_HTTP_ENABLED: "true"
      KC_METRICS_ENABLED: "true"
      # KC_HOSTNAME: keycloak.localhost
      # KC_HOSTNAME_PORT: 8180
      KC_HOSTNAME_URL: http://keycloak.localhost:8180
      KC_PROXY: reencrypt
      KEYCLOAK_ADMIN: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
    ports:
      - "8180:8080"
      - "8787:8787" # debug port
    networks:
      - local

networks:
  local:
    name: local
    driver: bridge

volumes:
  postgres_data:

Step 3: Run the docker compose command

docker compose build --no-cache keycloak

docker compose up -d

Step 4: Check the running containers

docker compose ps

Once it starts successfully, go to http://keycloak.localhost:8180 to test.

Log in to the admin console with the admin username and password.

Username: admin

Password: admin

By following these steps, you can run Keycloak with an external PostgreSQL database using Docker.

References