Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker networking - Keycloak can not connect to postgresql running in separate docker instance

I have two docker containers, one running Keycloak 4.7.0.Final, and one running Postgresql.

Keycloak Dockerfile

FROM jboss/keycloak:4.7.0.Final

ADD themes/mytheme /opt/jboss/keycloak/themes/healthjoiner

ADD modules /opt/jboss/keycloak/modules/system/layers/keycloak
ADD standalone-ha.xml /opt/jboss/keycloak/standalone/configuration/
ADD keycloak-config.json /opt/jboss/

RUN mkdir -p $JBOSS_HOME/standalone/data

CMD ["-b", "0.0.0.0", "-Dkeycloak.migration.action=import -Dkeycloak.migration.provider=singleFile -Dkeycloak.migration.file=/opt/jboss/keycloak-config.json -Dkeycloak.migration.strategy=OVERWRITE_EXISTING"]

Postgres Dockerfile

FROM postgres:9.5

# set root user details
ENV POSTGRES_PASSWORD=postgres
ENV POSTGRES_USER=postgres

ADD scripts/init.sql /docker-entrypoint-initdb.d/

# expose the 5432 port to outside the container
EXPOSE 5432

Here is a the problematic section within the standalone-ha.xml file, specifically the connection-url value.

<subsystem xmlns="urn:jboss:domain:datasources:5.0">
        <datasource jndi-name="java:jboss/datasources/KeycloakDS" pool-name="KeycloakDS" enabled="true"
                        use-java-context="true">
            <connection-url>jdbc:postgresql://my-net/keycloak</connection-url>
            <driver>postgresql</driver>
            <pool>
                <max-pool-size>20</max-pool-size>
            </pool>
            <security>
                <user-name>keycloak</user-name>
                <password>sa</password>
            </security>
        </datasource>
        <drivers>
            <driver name="postgresql" module="org.postgresql">
                <xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
            </driver>
        </drivers>
    </datasources>
</subsystem>

Postgres starts fine. When I start Keycloak I get java.net.UnknownHostException: my-net. I have also tried localhost and get the same error.

Lastly I have created a user-defined bridge network as per the guide on docker networking

docker network create --driver bridge my-net

I followed the guide but I can't get keycloak to find the postgresql database. From my understanding, the network my-net should be available internally to the container running keycloak.

Any help would be greatly appreciated. Thanks

UPDATE

Postgres was started with the following:

docker run --name postgresql -p 5432:5432 --network my-net postgresql

And keycloak with:

docker run --name keycloak -p 8080:8180 --network my-net keycloak
like image 620
Chris Ritchie Avatar asked Oct 28 '22 23:10

Chris Ritchie


1 Answers

My recommendation: don't create your own Docker image only for the theme/modules. Use official Keycloak image and mount your custom files as a volume => you will have easier image update. The official image also offers DB config env variables, so you won't need to use a customized config file:

docker run -d \
  --name keycloak \
  --net my-net \
  -p 8080:8080 \
  -p 443:8443 \
  -v $PWD/themes/mytheme:/opt/jboss/keycloak/themes/healthjoiner \
  -v $PWD/modules:/opt/jboss/keycloak/modules/system/layers/keycloak \
  -e DB_VENDOR=postgres \
  -e DB_ADDR=postgresql \
  -e DB_PORT=5432 \
  -e DB_DATABASE=<DB> \
  -e DB_USER=<DBUSER> \
  -e DB_PASSWORD=<DBPASSWORD> \
  jboss/keycloak:4.7.0.Final

See doc: https://hub.docker.com/r/jboss/keycloak

Anyway connection URL in your case is (use only container name):

jdbc:postgresql://keycloak:5432/<DATABASE>
like image 166
Jan Garaj Avatar answered Nov 13 '22 16:11

Jan Garaj