# Configure additional datasource while RUNNING (not building) Docker container

**URL:** <https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009>\
**Category:** Configuring the server\
**Created:** [October 20, 2023, 10:30am UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009 "2023-10-20T10:30:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![martinleim](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/martinleim/32/1766_2.png) [@martinleim](https://forum.keycloak.org/u/martinleim)\
**Post date:** [October 20, 2023, 10:30am UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/1 "2023-10-20T10:30:11Z")

</div>

Dear community,

I have created a custom UserStorageProvider that uses a separate database. I followed the qickstart from [https://github.com/keycloak/keycloak-quickstarts/tree/latest/extension/user-storage-jpa](https://github.com/keycloak/keycloak-quickstarts/tree/latest/extension/user-storage-jpa) by adding a custom persistence.xml and specifying the URL, user and password in `quarkus.properties`.

Now, I want to provide a Docker image of Keycloak with my extension pre-installed. Since the database URL and credentials are dynamic, they should not be included in the Docker image. Instead, I want to specify them while **running** the container.

For the Keycloak default data source. this can be done by specifying environment variables, as documented in [Running Keycloak in a container](https://www.keycloak.org/server/containers). Unfortunately, I cannot find an equivalent solution for custom data source properties. There is a similar discussion on GitHub ([Define Quarkus Properties from env vars · keycloak/keycloak · Discussion #22798 · GitHub](https://github.com/keycloak/keycloak/discussions/22798)) which does not provide a solution.

Additionally, since I am using an unsupported database (with `db-kind=other`), I have to specify a driver class. Since `quarkus.datasource."datasource-name".jdbc.driver` seems to be a build time option, I would like to do this during Docker build time. Is it possible to have some raw Quarkus properties in the Docker image and some others (the URL, user and password, which are not build time properties) to be set at runtime? [Configuring Keycloak](https://www.keycloak.org/server/configuration#_format_for_raw_quarkus_properties) only speaks of `quarkus.properties` as a single file.

I would be glad if anyone could point me to a solution.  
Best regards, Martin

---

<div class="post-metadata">

**Author:** ![dasniko](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/dasniko/32/2969_2.png) [@dasniko](https://forum.keycloak.org/u/dasniko)\
**Post date:** [October 20, 2023, 3:03pm UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/3 "2023-10-20T15:03:26Z")

</div>

In one of my projects, I’ve set (or at least overwritten) Quarkus props from environment variables startup.

---

<div class="post-metadata">

**Author:** ![xgp](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/xgp/32/2589_2.png) [@xgp](https://forum.keycloak.org/u/xgp)\
**Post date:** [October 20, 2023, 4:07pm UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/4 "2023-10-20T16:07:24Z")

</div>

If you are trying to define separate database connection properties, specifically for use in your extension, you can use the `spi-*` variables as described here [Configuring providers - Keycloak](https://www.keycloak.org/server/configuration-provider) and then load them in your provider’s `init` method from the `org.keycloak.Config.Scope`

---

<div class="post-metadata">

**Author:** ![martinleim](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/martinleim/32/1766_2.png) [@martinleim](https://forum.keycloak.org/u/martinleim)\
**Post date:** [October 23, 2023, 7:51am UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/5 "2023-10-23T07:51:58Z")

</div>

Hi @dasniko,

thanks for your suggesstion, that worked. I now set my properties as such:

```auto
quarkus.datasource.my-userstore-pg.db-kind=postgresql
quarkus.datasource.my-userstore-pg.jdbc.url=jdbc:postgresql://${MY_PG_HOST}:${MY_PG_PORT:5432}/${MY_PG_DB}
quarkus.datasource.my-userstore-pg.username=${MY_PG_USER}
quarkus.datasource.my-userstore-pg.password=${MY_PG_PASS}

```

This also allows me to use static values for build time properties like the driver class.

---

<div class="post-metadata">

**Author:** ![martinleim](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/martinleim/32/1766_2.png) [@martinleim](https://forum.keycloak.org/u/martinleim)\
**Post date:** [October 23, 2023, 7:54am UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/6 "2023-10-23T07:54:24Z")

</div>

Hi @xgp,

I know how I can access the `spi-*` variables, but I do not understand how I can use these properties to configure a data source. Do you have an example for that?

---

<div class="post-metadata">

**Author:** ![jordibuj](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/jordibuj/32/10406_2.png) [@jordibuj](https://forum.keycloak.org/u/jordibuj)\
**Post date:** [May 14, 2024, 8:11am UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/7 "2024-05-14T08:11:32Z")

</div>

Hi @martinleim I’m facing the same issue, and I can’t see how the environment variables are a working as a solution. They are still to be known at build time, not at runtime, aren’t they?

---

<div class="post-metadata">

**Author:** ![martinleim](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/martinleim/32/1766_2.png) [@martinleim](https://forum.keycloak.org/u/martinleim)\
**Post date:** [May 15, 2024, 1:33pm UTC](https://forum.keycloak.org/t/configure-additional-datasource-while-running-not-building-docker-container/23009/8 "2024-05-15T13:33:24Z")

</div>

Hi @jordibuj ,

> They are still to be known at build time, not at runtime, aren’t they?

No, they don’t - the values are applied on runtime. I can change the values without running Keycloak with `--build` again.
