0
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")

}
Ry2254
  • 859
  • 1
  • 10
  • 19

1 Answers1

1

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.

Mateusz Kubuszok
  • 24,995
  • 4
  • 42
  • 64
  • I checked my ports and its 5432:5432 so everything should work fine. How can the codebase talk to postgres port 5432 at the same time as my codebase image is trying to talk to the same postgres container? – Ry2254 May 13 '20 at 07:50
  • But `localhost` (`127.0.0.1`) is not the same as `0.0.0.0`. If you are connecting on `localhost` and you have ports exposed on `0.0.0.0` it won't click. And Docker's internal network names ( here `postgres`, `sports-app-api`) are not seen outside. – Mateusz Kubuszok May 13 '20 at 07:52
  • ```CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 76d0436eac25 sports-app-api_sports-app-api "sbt run" 3 minutes ago Up 3 minutes 0.0.0.0:8000->8000/tcp sportsAppApi 748db3e51e40 postgres:11.7-alpine "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:5432->5432/tcp sportsAppApiDb ``` So what do I need to change? How do I make it available to the outside. Do I change the Docker-compose file? – Ry2254 May 13 '20 at 08:11
  • `0.0.0.0:5432->5432` - external port `5432` (`...:5432->...`) is mapped to internal port `5432` (`...->5432`) on container `postgres`, so within the network (other Docker containers in `docker-compose.yml`) it requries `postgres:5432` and on the outside it (outside of Docker) requires `0.0.0.0:5432` (`0.0.0.0:5432->...`). – Mateusz Kubuszok May 13 '20 at 08:29