8

I have two different realms and I've to import different configs by realm JSON for both of them(i.e import both realm).

I created Docker compose for that above said.

here is the code.

version: "3"
services:
  keycloak:
    image: jboss/keycloak
    ports:
      - "8080:8080"
    volumes:
      - "/Users/msn/Downloads/:/Users/msn/Downloads/"
    environment:
      - "KEYCLOAK_USER=admin"
      - "KEYCLOAK_PASSWORD=admin"
      - "KEYCLOAK_IMPORT=/Users/msn/Downloads/realm-export.json,/Users/msn/Downloads/realm-expor.json"

but it imports only first JSON not the second please suggest the solutions to import both of the realms.

5 Answers 5

4
version: "3"
services:
  keycloak:
    image: jboss/keycloak
    ports:
      - "8080:8080"
    volumes:
      - "/Users/msn/Downloads/:/Users/msn/Downloads/"
    environment:
      - "KEYCLOAK_USER=admin"
      - "KEYCLOAK_PASSWORD=admin"
      #- "KEYCLOAK_IMPORT=/Users/msn/Downloads/realm-export.json,/Users/msn/Downloads/realm-expor.json"
    command:
      - "-b 0.0.0.0"
      - "-Dkeycloak.migration.action=import"
      - "-Dkeycloak.migration.provider=singleFile"
      - "-Dkeycloak.migration.file=/Users/msn/Downloads/realm-export.json"
      - "-Dkeycloak.migration.strategy=IGNORE_EXISTING"

note: all your realms have to be in the same file "realm-export.json".

1
  • Thankyou haythem but I need to import realms of two different .json file. May 17, 2020 at 12:41
3

put both of your realms to one .json file where content is array. So:

[
{...realm1...},
{...realm2...}
]

EDIT: Maybe you will need to adjust some parameters. I'm working only with Dockerfile so here is content of it. I hope it will help you.

FROM jboss/keycloak:9.0.2
COPY "src/main/jib/opt/jboss/keycloak/imports/realm.json" "/opt/jboss/keycloak/imports/realm.json"
CMD ["-Dkeycloak.migration.action=import", "-Dkeycloak.migration.provider=singleFile", "-Dkeycloak.migration.file=/opt/jboss/keycloak/imports/realm.json", "-Dkeycloak.migration.strategy=IGNORE_EXISTING"]
2

Working on Keycloak 12.0.4 (import multiple realms in separated files)

This is from a Dockerfile, but you can easily use the same configuration in a docker-compose.

Dockerfile

First the realm files must be copied into the container

COPY keycloak-files/realm-config/* /opt/jboss/keycloak/realm-config/

Now I can configure the JAVA_OPTS environment variable, to include:

ENV JAVA_OPTS -server \
 -Xms1303m \
 -Xmx1303m \
 -XX:MetaspaceSize=96M \
 -XX:MaxMetaspaceSize=256m \
 -Djboss.modules.system.pkgs=org.jboss.byteman \
 -Djava.awt.headless=true \
 -Djava.net.preferIPv4Stack=true \
 -Dkeycloak.migration.action=import \
 -Dkeycloak.migration.provider=dir \
 -Dkeycloak.migration.dir=/opt/jboss/keycloak/realm-config \
 -Dkeycloak.migration.strategy=OVERWRITE_EXISTING 

The important lines are prefixed by keycloak.migration.- Here we set the migration dir (where files will be imported from) to be equal as the one where we copied files before.

Docker-compose

version: '3.8'
services:
  keycloak:
    image: jboss/keycloak:12.0.4
    command:
      [
        '-b',
        '0.0.0.0',
        '-Dkeycloak.migration.action=import',
        '-Dkeycloak.migration.provider=dir',
        '-Dkeycloak.migration.dir=/opt/jboss/keycloak/realm-config',
        '-Dkeycloak.migration.strategy=OVERWRITE_EXISTING',
      ]
    volumes:
      - ./keycloak-files/realm-config:/opt/jboss/keycloak/realm-config

...

Basically it's the same.

You can copy any json file inside realm-config and it should be imported.

It's possible to use IGNORE_EXISTING migration strategy to avoid overwriting.

1
0

The following suggestions are tested for Keycloak 16.1.1 (jboss/keycloak image) and based on the Importing and exporting the database chapter from the official Keycloak docs.

Naming convention

As we can read in the docs:

When importing from a directory, the filenames must follow this naming convention:

  • <REALM_NAME>-realm.json. For example, "acme-roadrunner-affairs-realm.json" for the realm named "acme-roadrunner-affairs".

In my example project I have two realms: Example-Realm and keep-growing. Therefore, the import files are named respecitvely: Example-Realm-realm.json and keep-growing-realm.json.

Volume mapping

Make sure that the volume that maps files from your machine contains all the import files. My example volume config from the docker-compose.yml file:

version: '3.3'
services:
  keycloak:
    …
    volumes:
      - ./keycloak/realms/import:/tmp/import
    …

The ./keycloak/realms/import on my machine contains: enter image description here

As a result, after restarting the docker instance, I can see all the files from the aforementioned location on my machine available in the container: enter image description here

Minimal import config

I added the following commands to my docker-compose.yml file:

version: '3.3'
services:
  keycloak:
    …
    volumes:
       …
    command:
      - "-Dkeycloak.migration.action=import"
      - "-Dkeycloak.migration.provider=dir"
      - "-Dkeycloak.migration.dir=/tmp/import"

This will import realms on the container startup and overwrite existing ones.

Verify logs

You should see the following entries in the container logs when starting the container:

INFO  [org.keycloak.exportimport.dir.DirImportProvider] (ServerService Thread Pool -- 62) Importing from directory /tmp/import
…
INFO  [org.keycloak.services] (ServerService Thread Pool -- 62) KC-SERVICES0050: Initializing master realm
INFO  [org.keycloak.services] (ServerService Thread Pool -- 62) KC-SERVICES0030: Full model import requested. Strategy: OVERWRITE_EXISTING
INFO  [org.keycloak.exportimport.util.ImportUtils] (ServerService Thread Pool -- 62) Realm 'keep-growing' imported
INFO  [org.keycloak.exportimport.util.ImportUtils] (ServerService Thread Pool -- 62) Realm 'Example-Realm' imported
INFO  [org.keycloak.services] (ServerService Thread Pool -- 62) KC-SERVICES0032: Import finished successfully

Verify imported realms in the Keycloak Admin Console

All imported realms are available in the admin console: enter image description here

For reference

The complete docker-compose.yml file:

version: '3.3'
services:
  keycloak:
    image: jboss/keycloak:${KEYCLOAK_VERSION}
    ports:
      - "8024:8080"
    environment:
      - KEYCLOAK_USER=${KEYCLOAK_USER}
      - KEYCLOAK_PASSWORD=${KEYCLOAK_PASSWORD}
    volumes:
      - ./keycloak/realms/import:/tmp/import
    command:
      - "-Dkeycloak.migration.action=import"
      - "-Dkeycloak.migration.provider=dir"
      - "-Dkeycloak.migration.dir=/tmp/import"

The .env file:

KEYCLOAK_VERSION=16.1.1
KEYCLOAK_USER=keycloak
KEYCLOAK_PASSWORD=keycloak

EDIT: I wrote more on Keycloak directory import in the Keycloak in Docker #6 - How to import realms from a directory post.

0

Complementing answer https://stackoverflow.com/a/61794091/6039697

  1. Export all your realms into json files
  2. Put them in a directory e.g. realms
  3. Mount realms directory inside the container using a volume
  4. Put all mounted files absolute paths into the env KEYCLOAK_IMPORT separating them by a comma.
.
├── docker-compose.yaml
└── realms
    ├── realm-1.json
    └── realm-2.json

1 directory, 3 files
version: '2.2'

services:
  keycloak:
    image: jboss/keycloak:6.0.1
    hostname: keycloak
    environment:
      KEYCLOAK_USER: admin
      KEYCLOAK_PASSWORD: admin
      DB_VENDOR: h2
      DB_DATABASE: keycloak
      KEYCLOAK_IMPORT: /tmp/realms/realm-1.json,/tmp/realms/realm-2.json
    volumes:
      - ./realms:/tmp/realms
    ports:
      - "8080:8080"
    expose:
      - 8080

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.