-1

I have a Spring app that connects to local Postgres db. It works fine.

I then created a Docker image from the Spring app. When running the container it throws a db connection error.

I have followed the solution suggested here, but still getting the same error.

[ERROR ] [task-1] c.z.h.p.HikariPool c.z.h.p.HikariPool.throwPoolInitializationException(HikariPool.java:593) – HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
    at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:285)
    at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:49)
    at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:217)
    at org.postgresql.Driver.makeConnection(Driver.java:458)
    at org.postgresql.Driver.connect(Driver.java:260)

I have run the docker image in several ways with the same result.

docker run app-image

docker run --network=host app-image

Dockerfile:

FROM java:8-jdk-alpine

COPY ./build/libs/runtime-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app

ENTRYPOINT ["java", "-jar", "runtime-0.0.1-SNAPSHOT.jar"]
# Expose standard tomcat port

EXPOSE 9888

postgresql.conf:

#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------

# - Connection Settings -

listen_addresses = '*'

pg_hba.conf

# TYPE  DATABASE        USER            ADDRESS                 METHOD

host        postgres        postgres     0.0.0.0/0              trust
host        postgres        postgres     172.17.0.0/16          trust
EMC
  • 95
  • 3
  • 11
  • 1
    Is there a specific reason you do not use a docker postgres image, e.g. [this one](https://hub.docker.com/_/postgres)? --- You try to connect to `localhost:5432`, but your host machine is a separate network entity. You need to specify the (docker-internal) network address of your machine as db host. – Turing85 Aug 14 '20 at 11:39
  • My idea is not using docker image for the database, but don't know if this is a bad practice. Should I go with a postgres image? – EMC Aug 14 '20 at 11:45

1 Answers1

1

The spring application tries to connect to localhost:5432. Since the host and each container are separate network entities (i.e., the host and each container has its own network address in the docker-internal network), the container tries to connect to the database running on it. Since there is not database running, the connection cannot be established.

To fix this issue, you need to get the IP address of the host machine within the docker-internal network (see, e.g., this article at devilbox) and use this address as database server for the spring application.


I would recommend to run the postgres in a docker container aswell. You can, for example, use the official postgres image from dockerhub. Then you can reference the postgres-container from within the spring-container by the postgres-container's name.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Turing85
  • 18,217
  • 7
  • 33
  • 58
  • Thanks Turing85. I have partially follow your solution. I have updated the Spring app by replacing ```localhost``` with ```host.docker.internal``` in application properties files. Then recreated the image. – EMC Aug 14 '20 at 12:22