Keycloak 18 and Docker

I do net get how to set preconfigured postgres database!
I use docker ARG to provide the correct database connection string (and crendentials) and the are known while creating the image and container (checked with echo command).

The problem is, that always localhost is used instead of my connection string. Can anyone help me please with the configuration?

my dockerfile:

#we are using version 18y
FROM quay.io/keycloak/keycloak:18.0

#set workdir
WORKDIR /opt/keycloak

# get args
ARG KC_DB_URL
ARG KC_DB_USERNAME
ARG KC_DB_PASSWORD
ARG RENDER_EXTERNAL_HOSTNAME
ARG KEYCLOAK_PASSWORD
ARG KEYCLOAK_USER

#set hostname for production
ENV KC_HOSTNAME=$RENDER_EXTERNAL_HOSTNAME

#set admin user
ENV KEYCLOAK_ADMIN_PASSWORD=$KEYCLOAK_PASSWORD
ENV KEYCLOAK_ADMIN=$KEYCLOAK_USER

#set database
ENV KC_DB_URL=$KC_DB_URL
ENV KC_DB_USERNAME=$KC_DB_USERNAME
ENV KC_DB_PASSWORD=$KC_DB_PASSWORD 

#enable health & monitoring
ENV KC_HEALTH_ENABLED=true
ENV KC_METRICS_ENABLED=true

#FEATURES
ENV KC_FEATURES=token-exchange,admin-fine-grained-authz

# copy SPM login theme
COPY custom-theme/spm /opt/keycloak/themes/spm

# Install custom providers
#RUN curl -sL https://github.com/aerogear/keycloak-metrics-spi/releases/download/2.5.3/keycloak-metrics-spi-2.5.3.jar -o /opt/keycloak/providers/keycloak-metrics-spi-2.5.3.jar
#RUN /opt/keycloak/bin/kc.sh build

#build to configure
RUN /opt/keycloak/bin/kc.sh build --db=postgres

EXPOSE 8080

ENTRYPOINT ["/opt/keycloak/bin/kc.sh", "start"]

logging:

Jun 15 02:05:17 PM  2022-06-15 12:05:12,410 WARN  [io.agroal.pool] (main) Datasource '<default>': Ignoring property 'URL': null
Jun 15 02:05:17 PM  2022-06-15 12:05:12,410 WARN  [io.agroal.pool] (main) Datasource '<default>': Ignoring property 'URL': null
Jun 15 02:05:17 PM  2022-06-15 12:05:13,817 WARN  [io.quarkus.netty.runtime.NettyRecorder] (Thread-0) Netty DefaultChannelId initialization (with io.netty.machineId system property set to 66:8f:89:a9:f4:9c:37:b2) took more than a second
Jun 15 02:05:23 PM  2022-06-15 12:05:23,513 INFO  [org.keycloak.common.Profile] (main) Preview feature enabled: admin_fine_grained_authz
Jun 15 02:05:23 PM  2022-06-15 12:05:23,514 INFO  [org.keycloak.common.Profile] (main) Preview feature enabled: token_exchange
Jun 15 02:05:24 PM  2022-06-15 12:05:24,712 INFO  [org.keycloak.quarkus.runtime.hostname.DefaultHostnameProvider] (main) Hostname settings: FrontEnd: keycloak-q08l.onrender.com, Strict HTTPS: true, Path: <request>, Strict BackChannel: false, Admin: <request>, Port: -1, Proxied: false
Jun 15 02:05:30 PM  2022-06-15 12:05:30,411 WARN  [io.agroal.pool] (agroal-11) Datasource '<default>': Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
Jun 15 02:05:30 PM  2022-06-15 12:05:30,516 WARN  [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator] (JPA Startup Thread: keycloak-default) HHH000342: Could not obtain connection to query metadata: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.     at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:319)  at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)  at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)       at org.postgresql.Driver.makeConnection(Driver.java:400)    at org.postgresql.Driver.connect(Driver.java:259)       at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)   at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228) at org.postgresql.ds.common.BaseDataSource.getConnection(BaseDataSource.java:103)   at org.postgresql.xa.PGXADataSource.getXAConnection(PGXADataSource.java:49)     at org.postgresql.xa.PGXADataSource.getXAConnection(PGXADataSource.java:35) at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:216)        at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:513) at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:494) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)   at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:75)   at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1126)    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)        at java.base/java.lang.Thread.run(Thread.java:829)Caused by: java.net.ConnectException: Connection refused (Connection refused)     at java.base/java.net.PlainSocketImpl.socketConnect(Native Method)  at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:412)       at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:255)    at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:237)     at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) at java.base/java.net.Socket.connect(Socket.java:609)       at org.postgresql.core.PGStream.createSocket(PGStream.java:241) at org.postgresql.core.PGStream.<init>(PGStream.java:98)   at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)       at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)  ... 17 more
Jun 15 02:05:30 PM  
Jun 15 02:05:51 PM  2022-06-15 12:05:51,225 WARN  [org.infinispan.PERSISTENCE] (keycloak-cache-init) ISPN000554: jboss-marshalling is deprecated and planned for removal
Jun 15 02:05:52 PM  2022-06-15 12:05:52,126 WARN  [org.infinispan.CONFIG] (keycloak-cache-init) ISPN000569: Unable to persist Infinispan internal caches as no global state enabled
Jun 15 02:05:53 PM  2022-06-15 12:05:53,412 INFO  [org.infinispan.CONTAINER] (keycloak-cache-init) ISPN000556: Starting user marshaller 'org.infinispan.jboss.marshalling.core.JBossUserMarshaller'
Jun 15 02:05:58 PM  2022-06-15 12:05:58,509 INFO  [org.infinispan.CONTAINER] (keycloak-cache-init) ISPN000128: Infinispan version: Infinispan 'Triskaidekaphobia' 13.0.8.Final
Jun 15 02:06:00 PM  2022-06-15 12:06:00,410 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000078: Starting JGroups channel `ISPN`
Jun 15 02:06:00 PM  2022-06-15 12:06:00,411 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000088: Unable to use any JGroups configuration mechanisms provided in properties {}. Using default JGroups configuration!
Jun 15 02:06:02 PM  2022-06-15 12:06:02,524 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the send buffer of socket MulticastSocket was set to 1.00MB, but the OS only allocated 212.99KB
Jun 15 02:06:02 PM  2022-06-15 12:06:02,525 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the receive buffer of socket MulticastSocket was set to 20.00MB, but the OS only allocated 212.99KB
Jun 15 02:06:02 PM  2022-06-15 12:06:02,525 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the send buffer of socket MulticastSocket was set to 1.00MB, but the OS only allocated 212.99KB
Jun 15 02:06:02 PM  2022-06-15 12:06:02,526 WARN  [org.jgroups.protocols.UDP] (keycloak-cache-init) JGRP000015: the receive buffer of socket MulticastSocket was set to 25.00MB, but the OS only allocated 212.99KB
Jun 15 02:06:04 PM  2022-06-15 12:06:04,724 INFO  [org.jgroups.protocols.pbcast.GMS] (keycloak-cache-init) srv-cako1u319n075084fd1g-kn-00007-deployment-58d8fc7856-4dfs8-34998: no members discovered after 2005 ms: creating cluster as coordinator
Jun 15 02:06:04 PM  2022-06-15 12:06:04,812 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000094: Received new cluster view for channel ISPN: [srv-cako1u319n075084fd1g-kn-00007-deployment-58d8fc7856-4dfs8-34998|0] (1) [srv-cako1u319n075084fd1g-kn-00007-deployment-58d8fc7856-4dfs8-34998]
Jun 15 02:06:04 PM  2022-06-15 12:06:04,820 INFO  [org.infinispan.CLUSTER] (keycloak-cache-init) ISPN000079: Channel `ISPN` local address is `srv-cako1u319n075084fd1g-kn-00007-deployment-58d8fc7856-4dfs8-34998`, physical addresses are `[10.201.68.252:39992]`
Jun 15 02:06:16 PM  2022-06-15 12:06:16,316 WARN  [org.infinispan.factories.GlobalComponentRegistry] (keycloak-cache-init) ISPN000126: Attempted to stop() from FAILED state, but caught exception: java.lang.IllegalStateException: Shutdown in progress   at java.base/java.lang.ApplicationShutdownHooks.remove(ApplicationShutdownHooks.java:82)    at java.base/java.lang.Runtime.removeShutdownHook(Runtime.java:244)     at org.infinispan.factories.GlobalComponentRegistry.removeShutdownHook(GlobalComponentRegistry.java:181)    at org.infinispan.factories.AbstractComponentRegistry.internalStop(AbstractComponentRegistry.java:372)      at org.infinispan.factories.AbstractComponentRegistry.stop(AbstractComponentRegistry.java:308)      at org.infinispan.factories.AbstractComponentRegistry.start(AbstractComponentRegistry.java:265) at org.infinispan.manager.DefaultCacheManager.internalStart(DefaultCacheManager.java:766)   at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:734)   at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:405)      at org.keycloak.quarkus.runtime.storage.infinispan.CacheManagerFactory.startCacheManager(CacheManagerFactory.java:86)       at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)        at java.base/java.lang.Thread.run(Thread.java:829)
Jun 15 02:06:16 PM  
Jun 15 02:06:16 PM  2022-06-15 12:06:16,412 ERROR [io.quarkus.runtime.StartupContext] (main) Running a shutdown task failed: java.lang.RuntimeException: Failed to start caches     at org.keycloak.quarkus.runtime.storage.infinispan.CacheManagerFactory.getOrCreate(CacheManagerFactory.java:56)     at org.keycloak.quarkus.runtime.KeycloakRecorder$1.run(KeycloakRecorder.java:75)        at io.quarkus.runtime.StartupContext.runAllInReverseOrder(StartupContext.java:84)   at io.quarkus.runtime.StartupContext.close(StartupContext.java:73)      at io.quarkus.runner.ApplicationImpl.doStart(Unknown Source)        at io.quarkus.runtime.Application.start(Application.java:101)   at io.quarkus.runtime.ApplicationLifecycleManager.run(ApplicationLifecycleManager.java:103) at io.quarkus.runtime.Quarkus.run(Quarkus.java:67)      at org.keycloak.quarkus.runtime.KeycloakMain.start(KeycloakMain.java:86)    at org.keycloak.quarkus.runtime.KeycloakMain.main(KeycloakMain.java:71) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.base/java.lang.reflect.Method.invoke(Method.java:566)    at io.quarkus.bootstrap.runner.QuarkusEntryPoint.doRun(QuarkusEntryPoint.java:60)   at io.quarkus.bootstrap.runner.QuarkusEntryPoint.main(QuarkusEntryPoint.java:31)Caused by: java.util.concurrent.ExecutionException: org.infinispan.manager.EmbeddedCacheManagerStartupException: java.lang.IllegalStateException: Shutdown in progress      at java.base/java.util.concurrent.FutureTask.report(FutureTask.java:122)    at java.base/java.util.concurrent.FutureTask.get(FutureTask.java:205)   at org.keycloak.quarkus.runtime.storage.infinispan.CacheManagerFactory.getOrCreate(CacheManagerFactory.java:54)     ... 15 moreCaused by: org.infinispan.manager.EmbeddedCacheManagerStartupException: java.lang.IllegalStateException: Shutdown in progress    at org.infinispan.manager.DefaultCacheManager.internalStart(DefaultCacheManager.java:770)   at org.infinispan.manager.DefaultCacheManager.start(DefaultCacheManager.java:734)  at org.infinispan.manager.DefaultCacheManager.<init>(DefaultCacheManager.java:405)       at org.keycloak.quarkus.runtime.storage.infinispan.CacheManagerFactory.startCacheManager(CacheManagerFactory.java:86)       at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)       at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)        at java.base/java.lang.Thread.run(Thread.java:829)Caused by: java.lang.IllegalStateException: Shutdown in progress  at java.base/java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66)   at java.base/java.lang.Runtime.addShutdownHook(Runtime.java:216)    at org.infinispan.factories.GlobalComponentRegistry.addShutdownHook(GlobalComponentRegistry.java:216)       at org.infinispan.factories.AbstractComponentRegistry.internalStart(AbstractComponentRegistry.java:357) at org.infinispan.factories.AbstractComponentRegistry.start(AbstractComponentRegistry.java:250)     at org.infinispan.manager.DefaultCacheManager.internalStart(DefaultCacheManager.java:766)   ... 7 more
Jun 15 02:06:16 PM  
Jun 15 02:06:17 PM  2022-06-15 12:06:17,220 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Failed to start server in (production) mode
Jun 15 02:06:17 PM  2022-06-15 12:06:17,220 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) ERROR: Key material not provided to setup HTTPS. Please configure your keys/certificates or start the server in development mode.
Jun 15 02:06:17 PM  2022-06-15 12:06:17,221 ERROR [org.keycloak.quarkus.runtime.cli.ExecutionExceptionHandler] (main) For more details run the same command passing the '--verbose' option. Also you can use '--help' to see the details about the usage of the particular command.

Your dockerfile seems correct to me.

Just to make sure, are you setting KC_DB_URL at docker build time?

As you may know, ARG values are only available at docker build time. As you are using the ARG value to set an ENV, that should work.

You can double check your assumptions by using

ENTRYPOINT ["/bin/bash", "-c", "printenv | sort && sleep 10000"]

to print all your environment variables at docker run time and check that KC_DB_URL is correctly set.

Yeah - this fixed it! thanx a lot!!
I passed the the postgres URL in the wrong syntax!
The correct one is:
jdbc:postgresql://<server>/<database>

1 Like