Skip to content
October 21, 2024

How do you setup Keycloak with an existing PostgreSQL database and Docker ?

· 3 min read · Docker, OAuth 2.0
  • Keycloak is an open-source identity and access management (IAM) solution that provides authentication and authorization services.
  • Docker simplifies the deployment process by allowing you to set up and manage your Keycloak environment in containers
  • In this blog, we will share details on setting up Keycloak using docker and making it use an existing PostgreSQL running on host machine
  • YAML configuration which describes the Keycloak service within a Docker Compose environment.
services:
  keycloak:
    image: bitnami/keycloak:latest
    container_name: inv_keycloak
    environment:
      KEYCLOAK_ADMIN_USER: admin
      KEYCLOAK_ADMIN_PASSWORD: admin
      KEYCLOAK_DATABASE_VENDOR: postgresql
      KEYCLOAK_DATABASE_HOST: host.docker.internal 
      KEYCLOAK_DATABASE_PORT: 5432
      KEYCLOAK_DATABASE_NAME: keycloak
      KEYCLOAK_DATABASE_USER: postgres
      KEYCLOAK_DATABASE_PASSWORD: admin
      KEYCLOAK_DATABASE_SCHEMA: public
    ports:
      - 9090:8080
  • Service Definition (keycloak):
    • This section defines the Keycloak service, which is the main service to be deployed in Docker. It uses the Bitnami Keycloak image (bitnami/keycloak:latest), which includes pre-configured Keycloak functionalities.
  • Image (image):
    • bitnami/keycloak:latest: Specifies the latest version of the Keycloak image from Bitnami. This is an optimized image to run Keycloak in Docker containers.
  • Container Name (container_name):
    • inv_keycloak: This is the name given to the Keycloak container. It helps identify the container for future interactions.

Environment Variables (environment): Environment variables define the configuration of Keycloak. Here’s a breakdown of the variables:

  • KEYCLOAK_ADMIN_USER: Username for the Keycloak admin. In this case, it’s set to admin.
  • KEYCLOAK_ADMIN_PASSWORD: Password for the Keycloak admin, also set to admin.
  • KEYCLOAK_DATABASE_VENDOR: Specifies the database type, which is postgresql in this case.
  • KEYCLOAK_DATABASE_HOST: Hostname of the database. host.docker.internal allows Docker containers to access services running on the host machine.
  • KEYCLOAK_DATABASE_PORT: The port number where PostgreSQL is running (5432 is the default).
  • KEYCLOAK_DATABASE_NAME: Name of the database used by Keycloak, here it’s keycloak.
  • KEYCLOAK_DATABASE_USER: Username to connect to the PostgreSQL database (postgres).
  • KEYCLOAK_DATABASE_PASSWORD: Password for the PostgreSQL database (admin).
  • KEYCLOAK_DATABASE_SCHEMA: The schema in the PostgreSQL database where Keycloak will store its data, in this case, the default schema public.
  • Ports (ports):
    • 9090:8080: Exposes port 8080 of the container on port 9090 of the host machine. This allows you to access Keycloak on http://localhost:9090 from your host.
  • Running the setup:
    • Create the docker-compose.yaml file: Save the YAML configuration in a file named docker-compose.yaml in your project directory.
    • Start the Docker Containers: Open your terminal, navigate to the project directory, and run docker compose up -d to start the containers
    • Access Keycloak: Once the containers are up and running, you can access Keycloak by opening your browser and navigating to http://localhost:9090. Use the admin username and password provided in the environment variables to log in.

Thanks

Tags: , , ,

Leave a Reply