How to export realm file into keycloak docker container?

Slim picture Slim · Mar 2, 2018 · Viewed 7.8k times · Source

I'm trying to export a realm file into keycloak docker container, I'm not able to do that because the server is runing when I execute this command:

bin/standalone.sh -Dkeycloak.migration.action=export
-Dkeycloak.migration.provider=dir -Dkeycloak.migration.dir=<DIR TO EXPORT TO>

I tried to modify the docker-entrypoint.sh and I delete the command which executes the server to launch:

#!/bin/bash

if [ $KEYCLOAK_USER ] && [ $KEYCLOAK_PASSWORD ]; then
    keycloak/bin/add-user-keycloak.sh --user $KEYCLOAK_USER --password $KEYCLOAK_PASSWORD
fi

if [ "$DB_VENDOR" == "POSTGRES" ]; then
  databaseToInstall="postgres"
elif [ "$DB_VENDOR" == "MYSQL" ]; then
  databaseToInstall="mysql"
elif [ "$DB_VENDOR" == "H2" ]; then
  databaseToInstall=""
else
    if (printenv | grep '^POSTGRES_' &>/dev/null); then
      databaseToInstall="postgres"
    elif (printenv | grep '^MYSQL_' &>/dev/null); then
      databaseToInstall="mysql"
    fi
fi

if [ "$databaseToInstall" != "" ]; then
    echo "[KEYCLOAK DOCKER IMAGE] Using the external $databaseToInstall database"
    /bin/sh /opt/jboss/keycloak/bin/change-database.sh $databaseToInstall
else
    echo "[KEYCLOAK DOCKER IMAGE] Using the embedded H2 database"
fi

exit $?

However I got a caschLoopBack when I run the pod of keycloak. Is there any solution to make the export inside the docker container and stop the server from running?

Answer

Mat picture Mat · May 3, 2018

You can start a temporary container. I'm using swarm and attachable network, but replacing the --network flag with some --link to the DB container should do it for a vanilla docker container :

docker run --rm --network=naq\
    --name keycloak_exporter\
    -v /tmp:/tmp/keycloak-export\
    -e POSTGRES_DATABASE=keycloak\
    -e POSTGRES_PASSWORD=password\
    -e POSTGRES_USER=keycloak\
    -e DB_VENDOR=POSTGRES\
    -e POSTGRES_PORT_5432_TCP_ADDR=keycloakdb\
    jboss/keycloak:3.4.3.Final\
    -Dkeycloak.migration.action=export\
    -Dkeycloak.migration.provider=dir\
    -Dkeycloak.migration.dir=/tmp/keycloak-export\
    -Dkeycloak.migration.usersExportStrategy=SAME_FILE\
    -Dkeycloak.migration.realmName=Naq\

You'll then find export files in the /tmp dir on your host.