DB_ADDR unbound variable error

Hello,

I am trying to connec keycloak to a PostgreSqL database. To do this I am following the instructions in https://hub.docker.com/r/jboss/keycloak/.

First I create a user defined network:
docker network create keycloak-network

Then I start a PostgreSQL instance using the PostgreSQL docker image:
docker run -d --name postgres --net keycloak-network -e POSTGRES_DB=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=password postgres

And lastly I try to run Keycloak instance:
docker run --name keycloak --net keycloak-network jboss/keycloak -e DB_USER=keycloak -e DB_PASSWORD=password

However I get the following error:

/opt/jboss/tools/docker-entrypoint.sh: line 156: DB_ADDR: unbound variable

How can I solve this?

You forgot to set the DB_ADDR environment variable when starting keycloak.
-e DB_ADDR=postgres also set -e DB_VENDOR=POSTGRES
Read the section Environment variables on https://hub.docker.com/r/jboss/keycloak/

Maybe also take a look at docker-compose.
Example:

version: '3.7'

services:
  postgres:
      image: postgres:12.2
      container_name: postgres
      environment:
        POSTGRES_DB: keycloak
        POSTGRES_USER: keycloak
        POSTGRES_PASSWORD: password

  keycloak:
      image: jboss/keycloak:9.0.2
      container_name: keycloak
      environment:
        DB_VENDOR: POSTGRES
        DB_ADDR: postgres
        DB_DATABASE: keycloak
        DB_USER: keycloak
        DB_PASSWORD: password
        KEYCLOAK_USER: admin
        KEYCLOAK_PASSWORD: password
        PROXY_ADDRESS_FORWARDING: "true"
      ports:
        - 8080:8080
      depends_on:
        - postgres

  mailhog:
    image: mailhog/mailhog:latest
    container_name: mailhog
    ports:
      - "8025:8025"

Hello,

thanks for your reply. If I run the following I get the same error:

docker run --name keycloak --net keycloak-network jboss/keycloak -e DB_USER=keycloak -e DB_PASSWORD=password, -e DB_VENDOR=POSTGRES, -e DB_ADDR=postgres, -e DB_DATABASE=keycloak, -e KEYCLOAK_USER=keycloak, -e KEYCLOAK_PASSWORD=password, PROXY_ADDRESS_FORWARDING=true

1 Like

You canā€™t put commaā€™s in there

Iā€™m having the same issue.
Here are the steps to reproduce it:

* docker network create keycloak-network
* docker run -d --name postgres --net keycloak-network -e POSTGRES_DB=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=password postgres
* docker run --rm --name keycloak --net keycloak-network -p 8080:8080 jboss/keycloak -e DB_USER=keycloak -e DB_PASSWORD=password -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -e DB_ADDR=postgres -e DB_VENDOR=postgres

The result:

/opt/jboss/tools/docker-entrypoint.sh: line 156: DB_ADDR: unbound variable

Does someone know how to solve it?

I think Iā€™ve found the solution after some tries. Them problem seems to be with Docker and not with Keycloak itself.

You need to pass the image name jboss/keycloak to the end of the whole command. Something like docker run <a bunch of environment variables> jboss/keycloak and it will work fine. Thanks a lot guys.

3 Likes

For full disclosure, the format as @rhuankarlus and @zonaut have described it should look like:

docker run --name keycloak --net keycloak-network \
    -e DB_USER=keycloak -e DB_PASSWORD=password \
    -e DB_VENDOR=POSTGRES \
    -e DB_ADDR=postgres \
    -e DB_DATABASE=keycloak \
    -e KEYCLOAK_USER=keycloak \
    -e KEYCLOAK_PASSWORD=password \
    -e PROXY_ADDRESS_FORWARDING=true \
    jboss/keycloak 
  1. It doesnā€™t use commas in between environment variables.
  2. It has the target docker image at the end of the command.
  3. (Optional) I used \ to denote new lines when running the command in your shell, for readability purposes.

This is great. Someone should update the install instructions, b/c they are broken without these additions:
https://www.keycloak.org/getting-started/getting-started-docker

I get:

 (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([("subsystem" => "microprofile-metrics-smallrye")]): java.lang.NullPointerException

Can you indicate the exact docker image version that works for you?

Iā€™m using the postgres:12 container for postgres.

I am having the exact same issue on install as @sakoht. Please help!

1 Like

@sakoht try adding the JDBC_PARAMS according to the DB that you are using. For example, I am using postgres and this worked for me:

docker run -p 8080:8080 --name keycloak --net keycloak-network \
    -e DB_USER=keycloak -e DB_PASSWORD=password \
    -e DB_VENDOR=POSTGRES \
    -e JDBC_PARAMS='connectTimeout=60' \
    -e DB_ADDR=postgres \
    -e DB_DATABASE=keycloak \
    -e KEYCLOAK_USER=keycloak \
    -e KEYCLOAK_PASSWORD=password \
    -e PROXY_ADDRESS_FORWARDING=true \
    quay.io/keycloak/keycloak:11.0.2

As per the instructions at https://www.keycloak.org/getting-started/getting-started-docker, albeit with one necessary addition that they omitted:

docker run -it -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin -e DB_VENDOR=h2 quay.io/keycloak/keycloak:12.0.2

DB_VENDOR=h2 makes keycloak use an embedded database. No other containers or setup is required. This should be the default setting, but someone likely dropped the ball.

1 Like

I was having the same issue (had to explicitly set DB_VENDOR to H2, which is supposed to be the default). I dug in a bit and found that this line in the docker-entrypoint.sh is to blame:

  if (getent hosts postgres &>/dev/null); then
  export DB_VENDOR="postgres"

In my case, the ā€œgetent hosts postgresā€ is truthy because my host seems to be resolving ā€œpostgresā€ to some weird akamai endpoint (no clue why).

Explicitly setting DB_VENDOR seems like a good idea to avoid this brittle db type detection algorithm.

1 Like

Hello, I am a complete beginner. Following the instructions in the followin page to run keycloak as a docker container locally on a Mac:
https://www.keycloak.org/getting-started/getting-started-docker

Just executed the first step.

docker run -p 8080:8080 -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin quay.io/keycloak/keycloak:12.0.4

And I got this error:
/opt/jboss/tools/docker-entrypoint.sh: line 165: DB_ADDR: unbound variable

Could you please clearly state what prior steps should be done to successfully complete that setup?
Thanks!!

It was mentioned before (env variable DB_VENDOR=h2 is the clue):

docker run --rm -p 8080:8080 \
  -e KEYCLOAK_USER=admin \
  -e KEYCLOAK_PASSWORD=admin \
  -e DB_VENDOR=h2 \
  quay.io/keycloak/keycloak:12.0.4

Thanks! That worked.
Is it possible to update the webpage accordingly?
Normally getting-started tutorials should work.

We are ā€œcommunityā€ here, not a ā€œdevelopersā€, who have access to edit that page. Please open ticket https://issues.jboss.org/browse/KEYCLOAK and help to make it better.

Thanks for guidance. Raised KEYCLOAK-17351

For me, I need added DB_VENDOR=postgres env. variable when I changed name of postgres container (Eg: keycloak-db).
Iā€™m using postgres:13.2-alpine and jboss/keycloak:12.0.4 images.

docker run --rm --name keycloak-db --net keycloak-net -p 5432:5432 -v keycloak-12.0.4-vol:/var/lib/postgresql/data -e POSTGRES_DB=keycloak -e POSTGRES_USER=keycloak -e POSTGRES_PASSWORD=keycloak postgres:13.2-alpine

docker run --rm --name keycloak-app --net keycloak-net -p 8080:8080 -p 8443:8443 -e DB_VENDOR=postgres -e DB_ADDR=keycloak-db -e DB_DATABASE=keycloak -e DB_USER=keycloak -e DB_PASSWORD=keycloak -e JDBC_PARAMS=ā€˜useSSL=falseā€™ -e KEYCLOAK_USER=admin -e KEYCLOAK_PASSWORD=admin jboss/keycloak:12.0.4

`

keycloak use postgres

docker run -d --name postgres
ā€“restart=always
-e DB_ADDR=postgres
-v /opt/postgres:/var/lib/postgresql/data
ā€“net keycloak-network
-e POSTGRES_DB=keycloak
-e POSTGRES_USER=keycloak
-e POSTGRES_PASSWORD=password
postgres

docker run -d -p 8080:8080 -p 8443:8443
ā€“name keycloak
ā€“restart=always
-e DB_VENDOR=postgres
-e DB_ADDR=postgres
-e DB_USER=keycloak
-e DB_DATABASE=keycloak
-e DB_PASSWORD=password
-e KEYCLOAK_USER=admin
-e KEYCLOAK_PASSWORD=password
ā€“net keycloak-network
jboss/keycloak