skip to Main Content

In my project, using Quarkus, Angular and PostgreSQL DB, when I run the backend & and the frontend in dev Mode, I can connect to the DB (which is postgreSQL image running in a docker container) and create new lines in the tables and just works fine.
Of course the Quarkus docker file is auto-generated.
Here is the "application.properties" file I typed (inside the Quarkus project) :

quarkus.datasource.db-kind=postgresql
quarkus.datasource.username= username
quarkus.datasource.password= pwd
quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/db-mcs-thirdparty
quarkus.flyway.migrate-at-start=true
quarkus.flyway.baseline-on-migrate=true
quarkus.flyway.out-of-order=false
quarkus.flyway.baseline-version=1

and this is the "docker-compose.yml" file which I placed inside the backend folder (Quarkus):

version: '3.8'
services:
  db:
    container_name: pg_container
    image: postgres:latest
    restart: always
    environment:
      POSTGRES_USER: username
      POSTGRES_PASSWORD: pwd
      POSTGRES_DB: db-mcs-thirdparty
    ports:
      - "5432:5432"

  pgadmin:
      container_name: pgadmin4_container
      image: dpage/pgadmin4
      restart: always
      environment:
        PGADMIN_DEFAULT_EMAIL: usernamepgadmin
        PGADMIN_DEFAULT_PASSWORD: pwdpgadmin
      ports:
        - "5050:80"

But when I build a Quarkus docker image and try to run it in docker container, it fails !! knowing that the Angular docker container runs well, also the DB.
Here the error logs which I get after running the container:

Starting the Java application using /opt/jboss/container/java/run/run-java.sh …

__  ____  __  _____   ___  __ ____  ______
 --/ __ / / / / _ | / _ / //_/ / / / __/
 -/ /_/ / /_/ / __ |/ , _/ ,< / /_/ / 
--________/_/ |_/_/|_/_/|_|____/___/

2022-05-06 12:58:31,967 WARN  [io.agr.pool] (agroal-11) Datasource '<default>': The connection attempt failed.
2022-05-06 12:58:32,015 ERROR [io.qua.run.Application] (main) Failed to start application (with profile prod): java.net.UnknownHostException: db

at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:229)
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.base/java.net.Socket.connect(Socket.java:609)
at org.postgresql.core.PGStream.createSocket(PGStream.java:241)
at org.postgresql.core.PGStream.<init>(PGStream.java:98)
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:109)
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:235)
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:223)
at org.postgresql.Driver.makeConnection(Driver.java:400)
at org.postgresql.Driver.connect(Driver.java:259)
at io.agroal.pool.ConnectionFactory.createConnection(ConnectionFactory.java:210)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:513)
at io.agroal.pool.ConnectionPool$CreateConnectionTask.call(ConnectionPool.java:494)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at io.agroal.pool.util.PriorityScheduledExecutor.beforeExecute(PriorityScheduledExecutor.java:75)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1126)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:829)

So I replaced "localhost" in the:

quarkus.datasource.jdbc.url=jdbc:postgresql://localhost:5432/db-mcs-thirdparty

with the IP address, with the DB’s name, even I tried to enter the user name and the psw in that same line, etc …., but didn’t work.

I even stopped all the running containers (DB, frontend) and tried to run only the Quarkus container, the same case happens.
For the ports that I used, you can check the attached image.
the used ports

How should resolve this issue? thank you in advance.

2

Answers


  1. The localhost url stated in application.properties file refers to the containers own system localhost. That means your quarkus application container looking for this port on its own local ports.

    As far as i know, every docker-container and every started docker-compose.yaml create their own network, and only within this network the started services can connect through their service-names.

    Therefore your quarkus docker container has to connect to the services started by docker-compose. One solution could be to define all services (database, angular and backend) in one docker-compose.yaml and then refers to the service names in your url.

    Another solution could be using host.docker.internal instead of localhost.

    May further information regarding docker networks and host.docker.internal stated here: https://docs.docker.com/desktop/windows/networking/

    Login or Signup to reply.
  2. You can run your Quarkus container with network mode host (--network host), like this example:

    $ docker run --rm -d --network host --name my_nginx nginx
    

    https://docs.docker.com/network/network-tutorial-host/

    You could also add your Quarkus to your docker-compose like this example:

    https://github.com/quarkusio/quarkus-quickstarts/blob/main/kafka-quickstart/docker-compose.yaml

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search