Ry2254
Ry2254

Reputation: 1009

Scala JDBC project won't run outside of Docker Container?

com.zaxxer.hikari.pool.HikariPool$PoolInitializationException: Failed to initialize pool: The connection attempt failed.

I get the above error when entering sbt run However, inside my docker containers everything works fine.

Inside the first container I have a postgres database. The second container I have an image built from my project folders. When I run docker-compose up --build everything works fine.

I suspect the project (actual codebase) can't see the postgres database in docker-compose container.

Do I need another postgres database outside the docker-compose containers to go with my project code outside the containers?

docker-compose.yml file.

version: '3.6'
services:

  # App Backend PostgreSQL
  postgres:
    container_name: sportsAppApiDb
    image: postgres:11.7-alpine
    environment:
      POSTGRES_USER: admin
      POSTGRES_PASSWORD: password
      POSTGRES_URL: postgres://admin:password@localhost:5432/sportsappapi
      POSTGRES_DB: sportsappapi
      POSTGRES_HOST: postgres
    ports:
      - "5432:5432"

  # App Backend
  sports-app-api:
    container_name: sportsAppApi
    build: ./
    volumes:
      - ./:/usr/src/sports-app-api
    command: sbt run
    working_dir: /usr/src/sports-app-api
    ports:
      - "8000:8000"
    environment:
      POSTGRES_URI: postgres://admin:password@postgres:5432/sportsappapi

Entrypoint for scala project

object SportsAppApiStartup extends App {
  SportsAppApiDb(SportsAppApiConfig.appDb).init
  WebServer(Endpoints.handler, 8000).start()

  println(s"Running sports-app-api on port: 8000")

}

Upvotes: 0

Views: 112

Answers (1)

Mateusz Kubuszok
Mateusz Kubuszok

Reputation: 27595

Your database is not accessible outside of docker-compose under postgres:5432. Try to connect to it through psql or pgcli or other client and you'll see.

When you'll call docker-compose ps or docker ps you'll be able to see how to connect to Postgres docker image (under ports) - most likely it will be something like 0.0.0.0:5432.

E.g. if I have:

> docker ps
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                               NAMES
d90871418bcb        postgres                "docker-entrypoint.s…"   2 weeks ago         Up 4 days           0.0.0.0:7766->5432/tcp              postgres_container

it means that Postgres was available under 0.0.0.0:7766 from outside Docker.

This has nothing to do with Scala, sbt and slick as far as I can tell.

Upvotes: 1

Related Questions