Skip to content

Instantly share code, notes, and snippets.

@MrHassanMurtaza
Last active February 9, 2022 14:33
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MrHassanMurtaza/637eae66721110a17d66e92ad8ea1954 to your computer and use it in GitHub Desktop.
Save MrHassanMurtaza/637eae66721110a17d66e92ad8ea1954 to your computer and use it in GitHub Desktop.
Keycloak Import in Docker and Exit
#!/bin/sh
# If something goes wrong, this script does not run forever but times out
TIMEOUT_SECONDS=300
# Logfile for the keycloak export instance
LOGFILE=/tmp/standalone.sh.log
# destionation export file
JSON_IMPORT_FILE=/opt/jboss/keycloak/imports/realm-export.json
rm -f ${LOGFILE} ${JSON_IMPORT_FILE}
# Start a new keycloak instance with exporting options enabled.
# Use prot offset to prevent port conflicts with the "real" keycloak instance.
timeout ${TIMEOUT_SECONDS}s \
/opt/jboss/keycloak/bin/standalone.sh \
-Dkeycloak.migration.action=import \
-Dkeycloak.migration.provider=singleFile \
-Dkeycloak.migration.strategy=OVERWRITE_EXISTING \
-Dkeycloak.migration.file=${JSON_IMPORT_FILE} \
-Djboss.socket.binding.port-offset=99 \
> ${LOGFILE} &
# Grab the keycloak export instance process id
PID="${!}"
echo "PID is ${PID}"
timeout ${TIMEOUT_SECONDS}s grep -m 1 "Import finished successfully" <(tail -f ${LOGFILE})
# Stop the keycloak export instance
kill ${PID}
# Reloading keycloak to pick up new configurations
/opt/jboss/keycloak/bin/jboss-cli.sh --connect command=:reload
@MrHassanMurtaza
Copy link
Author

MrHassanMurtaza commented Aug 20, 2020

  • Move the script to keycloak docker as a volume.
  • Get the name of keycloak container:
    docker ps
  • Run the following command:
    docker exec <keycloak_container_name_or_id> /bin/bash /tmp/keycloak-import-docker.sh

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment