官方文档 https://www.keycloak.org/docs/7.0/server_admin/#admin-console-export-import
If more than one realm file needs to be imported, a comma separated list of file names can be specified.
This is more appropriate than the cases before, as this will happen only after the master realm has been initialized.
Examples:
-
-Dkeycloak.import=/tmp/realm1.json
-
-Dkeycloak.import=/tmp/realm1.json,/tmp/realm2.json
使用场景:
在上线前需要在本地新建出不同的realm,然后导出成json配置文件,集成到docker-compose配置文件里,这样发布的时候就可以比较方便,直接部署完就自动新建出不同的realm。
(0) 启动: docker run -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -p 8080:8080 jboss/keycloak
登录localhost:8080, 先配置realm,新建完导出realm。这里可以新建多个realm,分别导出多个json文件。
(1)把刚才启动的docker停止,可以直接删除上一步用到的container 也可以先留着:
删除的命令是 docker rm <container ID>
(2)新建docker-compose.yml,内容如下, 然后执行启动命令:docker-compose up -d
version: "3" services: keycloak: image: jboss/keycloak:7.0.0 ports: - "8080:8080" volumes: - "./imports:/Users/luciawang/workspace/practise/docker-compose/imports/" environment: - "KEYCLOAK_USER=admin" - "KEYCLOAK_PASSWORD=admin" - "DB_VENDOR=postgres" - "DB_ADDR=postgres" - "DB_DATABASE=postgres" - "DB_USER=postgres" - "DB_PASSWORD=123456" - "PROXY_ADDRESS_FORWARDING=true" - "KEYCLOAK_IMPORT=/Users/luciawang/workspace/practise/docker-compose/imports/realms.json,/Users/luciawang/workspace/practise/docker-compose/imports/realms1.json" depends_on: - postgres postgres: image: postgres ports: - "5432:5432" environment: - "POSTGRES_PASSWORD:123456"
另一种方式:
version: "3" services: keycloak: image: jboss/keycloak:7.0.0 ports: - "8080:8080" volumes: - "./imports:/Users/lucia/workspace/practise/docker-compose/imports/" environment: - "KEYCLOAK_USER=admin" - "KEYCLOAK_PASSWORD=admin" - "DB_VENDOR=postgres" - "DB_ADDR=postgres" - "DB_DATABASE=postgres" - "DB_USER=postgres" - "DB_PASSWORD=123456" - "PROXY_ADDRESS_FORWARDING=true" command: - "-b 0.0.0.0 -Dkeycloak.import=/Users/lucia/workspace/practise/docker-compose/imports/realms.json,/Users/luciawang/workspace/practise/docker-compose/imports/realms1.json" depends_on: - postgres postgres: image: postgres ports: - "5432:5432" environment: - "POSTGRES_PASSWORD:123456"
登录localhost:8080, 进入的用户名密码都是admin,查看一下是否所有的realm都新建成功了