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