10

I am running docker desktop (windows) and building docker image for keycloak 17.0.0 following the instructions at here. Build completes successfully but when I run this image in desktop I get error

ERROR [org.key.qua.run.cli.ExecutionExceptionHandler] (main) ERROR: Failed to obtain JDBC connection

ERROR [org.key.qua.run.cli.ExecutionExceptionHandler] (main) ERROR: No suitable driver found for jdbc:postgresql://postgres/keycloak

postgres is already running in docker desktop with the name "postgres" on default port 5432 and has keyclock database created.

Here is my Dockerfile:

FROM quay.io/keycloak/keycloak-x:latest as builder

ENV KC_METRICS_ENABLED=true
ENV KC_FEATURES=token-exchange
ENV KC_DB=postgres
RUN /opt/keycloak/bin/kc.sh build

FROM quay.io/keycloak/keycloak-x:latest
COPY --from=builder /opt/keycloak/lib/quarkus/ /opt/keycloak/lib/quarkus/
WORKDIR /opt/keycloak

RUN keytool -genkeypair -storepass password -storetype PKCS12 -keyalg RSA -keysize 2048 -dname "CN=server" -alias server -ext "SAN:c=DNS:localhost,IP:127.0.0.1" -keystore conf/server.keystore

ENV KEYCLOAK_ADMIN=admin
ENV KEYCLOAK_ADMIN_PASSWORD=admin

ENV KC_DB_URL='jdbc:postgresql://postgres/keycloak'
ENV KC_DB_USERNAME=postgres
ENV KC_DB_PASSWORD=postgres

ENV KC_HOSTNAME=localhost:8443
ENTRYPOINT ["/opt/keycloak/bin/kc.sh", "start"]

My understanding from the docs is, after setting "KC_DB=postgres", the build should have included postgres driver that appears to be missing.

Can somebody tell me what is wrong here? Thanks.

WSK
  • 5,949
  • 8
  • 49
  • 74
  • Does this answer your question? [No suitable driver found for 'jdbc:mysql://localhost:3306/mysql](https://stackoverflow.com/questions/8146793/no-suitable-driver-found-for-jdbcmysql-localhost3306-mysql) – rzlvmp Feb 16 '22 at 05:36
  • 1
    No. That question is about coding in java and I am asking about keycloak docket image configuration. – WSK Feb 16 '22 at 12:01

3 Answers3

4

I guess KC_DB is a runtime configuration. See build help:

# ./kc.sh build -h
...
  Change database settings:

      $ kc.sh build --db=postgres [--db-url][--db-username][--db-password]
...

So it should be build parameter --db=postgres.

BTW: I would use quay.io/keycloak/keycloak:17.0.0 image (17.0.0 is first stable Quarkus based Keycloak release).

Jan Garaj
  • 25,598
  • 3
  • 38
  • 59
  • Thank you @jan Garaj I'll try it but the code I posted is taken from their own docs that should not be wrong. Using release 17.0.0 suggestion is good, though. – WSK Feb 16 '22 at 12:22
  • by switching to stable release 17.0.0 solved my problem, there was nothing wrong in my code. Lesson learnt. Thank you for your help. – WSK Feb 16 '22 at 17:23
4

As Jan Garaj and omufeed already stated, it is a runtime configuration. So the example Dockerfile is wrong. Move the ENV KC_DB=postgres to the second FROM section so i looks like

[...]
ENV KC_DB_URL='jdbc:postgresql://postgres/keycloak'
ENV KC_DB_USERNAME=postgres
ENV KC_DB_PASSWORD=postgres
ENV KC_DB=postgres // HERE IT GOES
[...]

So you don't have to modify the ENTRYPOINT and also can provide those environment variables in a docker compose file.

LazyProphet
  • 1,184
  • 2
  • 10
  • 9
3

I battled with this issue for a while. As Jan Garaj has mentioned --db=postgres is a runtime config. So changing the ENTRYPOINT to this fixed the issue for me:

ENTRYPOINT ["/opt/keycloak/bin/kc.sh", "start-dev", "--db=postgres"]

And the following if you need to run for production with "start":

ENTRYPOINT ["/opt/keycloak/bin/kc.sh", "start", "--auto-build", "--db=postgres"]

It is also worth mentioning that the same Dockerfile works with quay.io/keycloak/keycloak:latest. I switched the image and found keycloak to be more stable compared to the keycloak-x.

omufeed
  • 144
  • 5