Supply the host to a jdbcUri in a Scala application hosted in a Docker container

I've created a Docker image to host a Scala application, where the application loads its database-connection information from a properties file located in its directory:

driverClassName=org.postgresql.Driver
jdbcUri=jdbc:postgresql://localhost:5432/pedscreen
user=$APP_USER
password=$APP_PASSWORD

I've also created a Docker image to host a Postgres database.

The docker-compose.yaml file:

version: "3.7"

services:
    database:
        image: postgres:9.6.22
        container_name: "postgres"
        environment:
            POSTGRES_PASSWORD: 'XXXXXXXXXX'
        volumes:
            - ./postgres/initdb:/docker-entrypoint-initdb.d/
        ports:
            - '5432:5432'
        restart: unless-stopped

    app:
        image: scala-app:latest
        container_name: "scala-app"
        depends_on:
            - database

Unfortunately, the application isn't able to connect to the database, because the address localhost isn't correct.

I thought about using an environment variable (DATABASE_HOST) in the properties file:

driverClassName=org.postgresql.Driver
jdbcUri=jdbc:postgresql://$DATABASE_HOST:5432/pedscreen
user=$APP_USER
password=$APP_PASSWORD

and populating it in the yaml file:

...

app:
    image: scala-app:latest
    container_name: "scala-app"
    environment:
        DATABASE_HOST: database
    depends_on:
        - database

But this doesn't work either:

...
 Caused by: org.postgresql.util.PSQLException: The connection attempt failed.
...
Caused by: java.net.UnknownHostException: $DATABASE_HOST

Is there a way to get this to work?

I don't have any ability to modify the Scala application itself.



Read more here: https://stackoverflow.com/questions/67928345/supply-the-host-to-a-jdbcuri-in-a-scala-application-hosted-in-a-docker-container

Content Attribution

This content was originally published by Craig at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: