In the last one, we create the Keycloak database in a similar fashion as the application database. Psql -U dev -c "CREATE USER keycloak WITH PASSWORD 'kc'" | grep -q 1 || psql -U dev -c "CREATE DATABASE keycloak" Psql -U dev -tc "SELECT 1 FROM pg_database WHERE datname = 'keycloak'" \ It simply initializes the application database schema. The second script is more straightforward. \c app ĬONSTRAINT fk_member FOREIGN KEY(authorId) REFERENCES Member(id) Here the double pipe means that the CREATE DATABASE psql command will be run only if the grep command does not succeed. Unlike MySQL-like databases, PostgreSQL does not have a CREATE DATABASE IF EXIST statement.Īs a workaround, we use a bash script to run a psql command that creates the database if it does not exist. | grep -q 1 || psql -U dev -c "CREATE DATABASE app" Psql -U dev -tc "SELECT 1 FROM pg_database WHERE datname = 'app'" \ The first two scripts are only necessary if you want to store application-related data in the same database as the Keycloak one. The second one mounts files to initialize the Keycloak and application databases. The first bind mount is used to persist data even if the container is stopped. This type of volume is fine for a dev environment, but using named volumes or copying files directly in the container is more advised for production environment, except if you want full control of your filesystem, and apply modifications outside docker. Here, we provide both a source and a target path, making those volumes bind mounts. We first define volumes to mount data into the container as well as to persist data on the host. We use Docker compose to ease our multi-container setup by defining the PostgreSQL instance in a docker-compose.yml file. We run our PostgreSQL instance in a Docker container, using the official PostgreSQL image provided on Docker Hub. In our setup, we use PostgreSQL as a database for Keycloak to persist data such as users, clients or realms and replace the H2 database provided by default. Let's start right off by setting up the PostgreSQL database. Check out the documentation to have a walkthrough on how to install docker on your favorite OS. To use this setup on your own machine, you will need Docker and Docker compose. This setup is mostly designed to be used in a development environment, but it is a good starting point for a production environment using a microservice architecture. In this blog post, we are going to learn how to run Keycloak inside docker, using a dedicated PostgreSQL database also running in a docker container.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |