Skip to content

Instantly share code, notes, and snippets.

Last active January 26, 2022 21:19
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save MrHassanMurtaza/73922ce2ec5e965feec2194e309c2e6d to your computer and use it in GitHub Desktop.
Keycloak export in Docker and Exit
# If something goes wrong, this script does not run forever but times out
# Logfile for the keycloak export instance
# destionation export 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/ \
-Dkeycloak.migration.action=export \
-Dkeycloak.migration.provider=singleFile \
-Dkeycloak.migration.file=${JSON_EXPORT_FILE} \
-Djboss.socket.binding.port-offset=99 \
> ${LOGFILE} &
# Grab the keycloak export instance process id
# Wait for the export to finish
timeout ${TIMEOUT_SECONDS}s \
grep -m 1 "Export finished successfully" <(tail -f ${LOGFILE})
# Stop the keycloak export instance
kill ${PID}
# Optional reload the keycloak instance
/opt/jboss/keycloak/bin/ --connect command=:reload
Copy link

Move the script to the 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/


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