Export a Keycloak realm by using the version 20.0.1

This blog post is about how export an example development realm using the Keycloak in version 20.0.1. I wanted to ensure that the export contains all information including users. You can find the relevant information in Keycloak documentation ‘Exporting a realm to a file.’. I did some demo configurations in the new version and I can’t reuse my older exported examples from Keycloak.

Step 1: Define the needed environment variables

export KEYCLOAK_ADMIN=admin
export KEYCLOAK_ADMIN_PASSWORD=admin
export KEYCLOAK_PORT="8080:8080"
export KEYCLOAK_IMAGE="quay.io/keycloak/keycloak:20.0.1"
export KEYCLOAK_MODE="start-dev"
export SOURCE_VOLUME=$(pwd)
export DESTINATION_VOLUME=tmp
export KEYCLOAK_URL=http://localhost:8080

Step 2: Start the Keycloak container in the developer mode

KEYCLOAK_MODE="start-dev"

docker run -it -e KEYCLOAK_ADMIN=$KEYCLOAK_ADMIN \
               -e KEYCLOAK_ADMIN_PASSWORD=$KEYCLOAK_ADMIN_PASSWORD \
               -p $KEYCLOAK_PORT $KEYCLOAK_IMAGE $KEYCLOAK_MODE \ 
               -v $SOURCE_VOLUME:$DESTINATION_VOLUME 

Step 3: Connect to second session to container

Open a second terminal and execute following command:

docker ps

  • Example output:
CONTAINER ID   IMAGE                              COMMAND                  CREATED          STATUS          PORTS                              NAMES
96bc9b5aea5e   quay.io/keycloak/keycloak:20.0.1   "/opt/keycloak/bin/k…"   50 minutes ago   Up 50 minutes   0.0.0.0:8080->8080/tcp, 8443/tcp   sharp_elbakyan

Step 4: Open third terminal session

Access the running Keycloak container in an interactive mode.

export CONTAINER_ID=aa7f1d717b9e
docker exec -it $CONTAINER_ID bash

  • Example output:
bash-4.4$

Step 5: Navigate to the Keycloak bin folder

cd ./opt/keycloak/bin

Step 6: Search for kc.sh file

ls kc.sh | grep kc.s*

  • Example output:
kc.sh

Step 7. Export the realm to a file

With USERS_STRATEGY=realm_file we export all need configurations also the passwords.

export REALM_NAME=your-realmname
export EXPORT_FILE_NAME=your-realmname.json
export USERS_STRATEGY=realm_file
./kc.sh export --realm $REALM_NAME --file ./$EXPORT_FILE_NAME --users $USERS_STRATEGY

Step 8. Verify the output of the exported file

cat ./$EXPORT_FILE_NAME

Step 9. Copy the file to your local computer or just select the cat output and save it to a JSON file.

Summary

It’s awesome, how good it works to export and reuse example realms for Keycloak. It worked perfectly in my situation. Maybe I can update the bash automation related to the Keycloak REST API change made since version 18.


I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

#keycloak, #docker, #export

One thought on “Export a Keycloak realm by using the version 20.0.1

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

Blog at WordPress.com.

Up ↑

%d bloggers like this: