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:

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:

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:

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.