Docker Compose Keycloak Postgres [2 Steps]

In this article We are going to perform docker compose keycloak postgres ( Docker compose for Keycloak with Postgres Database).

Introduction

Keycloak is free and open source which is used to single sign on with identity and access management.

Postgres is free and Open Source Relational Database Management system (RDBMS).

Prerequisites

  • Ubuntu 20.04/18.04/16.04 LTS with minimum Installation
  • SSH access with sudo privileges
  • Firewall Port: 8080,5432

Here We are going to perform to create docker compose file for keycloak with postgres.

Install Docker on Ubuntu

First Install the docker on Ubuntu using below article.

How to Install Docker on Ubuntu 19.10/18.04/16.04 LTS

Step 1: Install Docker Compose on Ubuntu

Download the docker compose binary in /usr/local/bin directory.

sudo curl -L "https://github.com/docker/compose/releases/download/1.23.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Change the permission of docker compose binary

$ sudo chmod +x /usr/local/bin/docker-compose

Verify the docker compose version

$ docker-compose --version

Output:

docker-compose version 1.23.1, build a133471

Create the directory to save docker compose file

$ sudo mkdir keycloak

Navigate to keycloak directory

$ cd keycloak

Now create the docker compose file named docker-compose.yml

$ sudo nano docker-compose.yml

Step 2: Docker Compose Keycloak Postgres

Paste the below lines into it,

# Docker Compose Keycloak Postgres ( Docker compose for Keycloak with Postgres Database).

version: '3.1'


volumes:
  postgres_data:
      driver: local


services:
  postgres:
      image: postgres
      volumes:
        - postgres_data:/var/lib/postgresql/data
      environment:
        POSTGRES_DB: keycloak
        POSTGRES_USER: keycloak
        POSTGRES_PASSWORD: password
  keycloak:
      image: jboss/keycloak:10.0.0
      environment:
        DB_VENDOR: POSTGRES
        DB_ADDR: postgres
        DB_DATABASE: keycloak
        DB_USER: keycloak
        DB_PASSWORD: password
        KEYCLOAK_USER: fosstechnix
        KEYCLOAK_PASSWORD: [email protected]
        
      ports:
        - 80:8080
      depends_on:
        - postgres

Run the docker compose file using below command

$ sudo docker-compose up

Check docker containers running status

$ sudo docker ps

Output:

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                            NAMES
11d2c4d25cc7        jboss/keycloak   "/opt/jboss/tools/do…"   8 days ago          Up 7 days           8443/tcp, 0.0.0.0:80->8080/tcp   ubuntu_keycloak_1
82b86757c6b6        postgres                "docker-entrypoint.s…"   8 days ago          Up 8 days           5432/tcp                         ubuntu_postgres_1

Once docker compose is run successfully, Open your browser and type SERVER_IP , port number not required as it forwarded to port 80.

$ http://SERVER_IP/

Now you can see the below keycloak UI screen

keycloak UI

Now click on “Administration Console” to login with User Name and Password .

keycloak https required

If you are getting above error as keycloak- https required. Login to docker container shell using below command

$ sudo docker exec -it CONTAINER_ID /bin/bash

once logged into postgres docker container,  Login to Postgre databases using below command

 [email protected]:/# psql -U keycloak

Then Disable the ssl in using below postgres SQL query

update REALM set ssl_required='NONE' where id = 'master';

Restart the postgres and keycloak docker containers

$ sudo docker restart postgres_Container_ID
$ sudo docker restart keycloak_Container_ID

Finally login to Keycloak UI using server IP

$ http://SERVER_IP/

Now you can see the below keycloak UI screen

keycloak UI

Now click on “Administration Console” to login with User Name and Password as mentioned in docker compose yml file.

keycloak admin login

We have successfully covered docker compose keycloak postgres ( Docker compose for Keycloak with Postgres Database).

Conclusion:

In this article, We have performed , we have covered docker compose keycloak postgres ( Docker compose for Keycloak with Postgres Database).

Related Articles:

How to Create Docker Image for Node JS Application [2 Steps]

Reference:

Keycloak GitHub Containers

Alok Kanakeri

I am Alok Kanakeri working as Senior Site Reliability Engineer(Cloud and DevOps) Likes to share knowledge.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link