将Keycloak与Postgres数据库连接

4

我正在尝试使用Docker将Keycloak(20.0.3)与Postgres数据库连接起来。以下是我配置它所采取的步骤:

1. docker network create keycloak-network
2. docker run --name postgresP -p 5432:5432 -d --net keycloak-network -e POSTGRES_PASSWORD=admin -e POSTGRES_USER=admin -e POSTGRES_DB=pdb -d postgres:latest
3. docker run -p 9090:9090 --name keycloakP --net keycloak-network -e KEYCLOAK_ADMIN=admin -e KEYCLOAK_ADMIN_PASSWORD=admin -e KC_DB=postgres -e KC_DB_URL=jdbc:postgresql://localhost:5432/pdb -e KC_DB_USERNAME=admin -e KC_DB_PASSWORD=admin quay.io/keycloak/keycloak:latest start-dev

但是我遇到了以下错误。我尝试进行故障排除,但不幸的是,Keycloak的文档并不是很好。如果有任何线索,将不胜感激。提前致谢。

2023-02-10 11:08:36,986 DEBUG [io.quarkus.bootstrap.classloading.QuarkusClassLoader] (main) Adding elements io.quarkus.bootstrap.classloading.PathTreeClassPathElement[io.quarkus:quarkus-devservices-common / runtime=false resources=null] to QuarkusClassLoader Augmentation Class Loader: PROD

2023-02-10 11:08:36,987 DEBUG [io.quarkus.bootstrap.classloading.QuarkusClassLoader] (main) Adding elements io.quarkus.bootstrap.classloading.PathTreeClassPathElement[io.quarkiverse.vault:quarkus-vault-deployment / runtime=false resources=null] to QuarkusClassLoader Augmentation Class Loader: PROD

2023-02-10 11:08:36,987 DEBUG [io.quarkus.bootstrap.classloading.QuarkusClassLoader] (main) Adding elements io.quarkus.bootstrap.classloading.PathTreeClassPathElement[org.keycloak:keycloak-quarkus-server-deployment / runtime=false resources=null] to QuarkusClassLoader Augmentation Class Loader: PROD

2023-02-10 11:08:39,257 DEBUG [io.quarkus.bootstrap.classloading.QuarkusClassLoader] (main) Adding elements io.quarkus.bootstrap.classloading.PathTreeClassPathElement[/ runtime=true resources=null] to QuarkusClassLoader Deployment Class Loader: PROD

2023-02-10 11:08:39,290 DEBUG [io.quarkus.deployment.QuarkusAugmentor] (main) Beginning Quarkus augmentation

2023-02-10 11:08:40,193 TRACE [io.quarkus.bootstrap.classloading.QuarkusClassLoader] (main) Class quarkus.properties not found in parent first load from java.net.URLClassLoader@192c3f1e

2023-02-10 11:08:40,194 TRACE [io.quarkus.bootstrap.classloading.QuarkusClassLoader] (main) Class quarkus.properties not found in parent first load from java.net.URLClassLoader@192c3f1e

ERROR: Failed to run 'build' command.
ERROR: No value present

我所尝试达成的目标是将自定义数据库用于Keycloak,而非H2,并通过此方式创建持久化用户/配置。
1个回答

14

您的KC_DB_URL设置不正确;Postgres未在localhost上运行(这意味着“在keycloak容器中”);它正在您的postgresP容器中运行,因此您需要使用该容器名称作为主机名:

KC_DB_URL=jdbc:postgresql://postgresP:5432/pdb

这不会导致工作配置,因为当您启动Keycloak容器时,您正在设置-p 9090:9090,但是Keycloak在容器内部监听端口8080,所以您需要-p 9090:8080
您不需要发布端口(-p 5432:5432)在postgres容器中,以便从keycloak容器访问它;只有当您想要从主机或网络中的其他位置访问数据库时,才需要发布端口。
一些与问题无关的评论:
  1. Using postgres:latest as your image is going to cause problems at some point when :latest unexpectedly gets you a new major version of Postgres; use an explicit version instead (e.g. postgres:15).

    The same holds true for most images -- it's almost always a good idea to pin to a specific version (or at least a specific major version).

  2. Do yourself a favor and use docker compose instead of manually running a bunch of docker run commands. Your current configuration could be represented by the following docker-compose.yaml:

    services:
      postgres:
        image: postgres:15
        environment:
          POSTGRES_PASSWORD: admin
          POSTGRES_USER: admin
          POSTGRES_DB: pdb
      keycloak:
        image: quay.io/keycloak/keycloak:20.0
        environment:
          KEYCLOAK_ADMIN: admin
          KEYCLOAK_ADMIN_PASSWORD: admin
          KC_DB: postgres
          KC_DB_URL: jdbc:postgresql://postgres/pdb
          KC_DB_USERNAME: admin
          KC_DB_PASSWORD: admin
        ports:
          - 9090:8080
        command:
          - start-dev
    

    Put the above in docker-compose.yaml and then run docker compose up.

    You'll note that I am not publishing postgres ports in this example, in line with my earlier comment.

  3. Regardless of whether you're using docker compose or just multiple docker run command lines, you probably want to use a Docker volume for your postgres data so that you don't lose everything when you restart the container.

    I haven't configured that in my example, but you'll find appropriate examples in the official docs and all over the place on this website.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接