如果需要提前将已有keycloak的realm导出并通过docker-compose导入,用下图的方式导出,将导出的文件命名为realms.json
新建文件夹并创建文件: docker-compose.yml
version: "3" services: keycloak: image: jboss/keycloak:7.0.0 ports: - "8080:8080" volumes: - "./imports:/Users/lucia/workspace/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/docker-compose/imports/realms.json" depends_on: - postgres postgres: image: postgres ports: - "5432:5432" environment: - "POSTGRES_PASSWORD:123456"
如果不需要导入realm,可以直接用下面的方式:
version: "3" services: keycloak: image: jboss/keycloak:7.0.0 ports: - "8080:8080" 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" depends_on: - postgres postgres: image: postgres ports: - "5432:5432" environment: - "POSTGRES_PASSWORD:123456"
后台启动: docker-compose up -d
前台启动:docker-compose up
停止:docker-compose stop
查看docker启动中的容器:
$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 71ae958e3ffd postgres "docker-entrypoint.s…" 21 minutes ago Up 21 minutes 0.0.0.0:5432->5432/tcp docker-compose_postgres_1 4f63a236e573 jboss/keycloak "/opt/jboss/tools/do…" 21 minutes ago Up 21 minutes 0.0.0.0:8080->8080/tcp, 8443/tcp docker-compose_auth_1
删除某个容器
docker rm <容器CONTAINER id>
进入容器内部
$ docker exec -it 71ae958e3ffd bash (进入postgresql的容器内部) root@71ae958e3ffd:/# psql postgres -U postgres psql (12.0 (Debian 12.0-1.pgdg100+1)) Type "help" for help. postgres=# c postgres You are now connected to database "postgres" as user "postgres".
打开网页,用户名密码就是我们在docker-compose.yml 文件里配置的 admin
KEYCLOAK_USER=admin
KEYCLOAK_PASSWORD=admin