Launch Keycloak with Postgres DB in Dev Mode

Launch Keycloak with Postgres DB in Dev Mode

With Docker Compose on Ubuntu on Localhost

This tutorial goes through launching Keycloak using Postgres as the database in separate containers using Docker compose. This tutorial assumes the following: 

Create the Compose.Yaml File

We will first create a docker compose file like the following (feel free to copy for your own purposes).

							
							
					nano compose.yaml				
			

This will enter the nano editor in the terminal, enter:

							
							
					version: '3.8' #latest version of docker compose

services:
postgres:
image: postgres:latest
environment:
POSTGRES_DB: keycloak
POSTGRES_USER: keycloak
POSTGRES_PASSWORD: password
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- keycloak_network

keycloak:
image: quay.io/keycloak/keycloak:latest
environment:
KC_DB: postgres
KC_DB_URL_HOST: postgres
KC_DB_URL_DATABASE: keycloak
KC_DB_USERNAME: keycloak
KC_DB_PASSWORD: password
KEYCLOAK_ADMIN: admin
KEYCLOAK_ADMIN_PASSWORD: admin
KC_LOG_LEVEL: ALL
ports:
- 8080:8080
depends_on:
- postgres
networks:
- keycloak_network
command: ["start-dev"]

networks:
keycloak_network:

volumes:
postgres_data:

#this is not the cleanest compose file I realize :)				
			

Then after you enter the above file into your compose.yaml file CTRL + O and then Enter saves the file and CTRL + X exits.

Run the Compose File

Next we will run the compose.yaml file via the following command:

							
							
					docker-compose up -d				
			

Installation Verification & Logging

You can verify the compose.yaml file executed successfully and view logs via the following commands:

							
							
					docker ps # this will display all active docker containers

docker-compose logs keycloak # displays logs related to keycloak

docker-compose logs postgres # displays logs related to the postgres db				
			

Verify Keycloak Initialized the DB successfully

Next we will log into the PostgreSQL server and ensure that keycloak successfully initialized the db

							
							
					docker exec -it <keycloak_ps_name> psql -U keycloak # find the <keycloak_ps_name> by running docker ps and finding the name of the keycloak instance
				
			
							
							
					\d /* returns the databases including templates, default postgres, and the keycloak db we created */

\dt /* lists the tables */				
			

If when you run \dt it returns a list of tables then keycloak has initialized the db correctly and you are good to go!

Walter Miely is a tech entrepreneur and CEO of Phoenix Ignited Tech You can find him on Linkedin. This material is licensed under the CC BY 4.0 License LEGAL DISCLAIMER: The content provided here is provided AS IS, and part of, or the entirety of this content may be incorrect. Please read the entireLegal Disclaimer here.