Search code examples
dockerkeycloak

Importing realm changes in Keycloak 23


Hi I have an issue I was wondering if I could solve using only the Keycloak Docker image. The problem I am attempting to solve is that we have a realm on a lower environment that we wish to duplicate on a higher environment. Lets say that a developer team adds a client role, attribute or mapper on environment A, performs a partial export and then in environment B we add the --import-realm argument to kc.sh start and mount this exported json file to /opt/keycloak/data/import

The issue is that if the realm already exists the import will be skipped. Is there a way to "override/force" this re-import? Or will I need to use something like the bitnami/keycloak-config-cli image to perform this?


Solution

  • If you are are using the official KeyCloak Docker image then you should set the migration.strategy Java parameter to OVERWRITE_EXISTING (Default is IGNORE_EXISTING and this is the reason why skipped your import) in your Dockerfile/compose file.

    For example:

    KEYCLOAK_IMPORT=/opt/keycloak/data/import/keycloak-realm.json
    JAVA_OPTS_APPEND=-Dkeycloak.profile.feature.upload_scripts=enabled -Dkeycloak.migration.strategy=OVERWRITE_EXISTING
    

    Import/Export reference:

    migration.strategy options:

    -Dkeycloak.migration.strategy

    This property is used during import. It can be used to specify how to proceed if a realm with same name already exists in the database where you are going to import data. Possible values are:

    IGNORE_EXISTING - Ignore importing if a realm of this name already exists.

    OVERWRITE_EXISTING - Remove existing realm and import it again with new data from the JSON file. If you want to fully migrate one environment to another and ensure that the new environment will contain the same data as the old one, you can specify this.