I am trying to docerize my spring boot application which uses posgtres. I am running postgres containter and while running app image spring boot not able to connect the data base. Getting below error. I am not using docker-compose as of now.
Caused by: 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:346) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:273) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.Driver.makeConnection(Driver.java:446) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.Driver.connect(Driver.java:298) ~[postgresql-42.7.3.jar!/:42.7.3]
at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:137) ~[HikariCP-5.1.0.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:360) ~[HikariCP-5.1.0.jar!/:na]
at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:202) ~[HikariCP-5.1.0.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:461) ~[HikariCP-5.1.0.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:550) ~[HikariCP-5.1.0.jar!/:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:98) ~[HikariCP-5.1.0.jar!/:na]
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:111) ~[HikariCP-5.1.0.jar!/:na]
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.getConnection(DatasourceConnectionProviderImpl.java:122) ~[hibernate-core-6.5.2.Final.jar!/:6.5.2.Final]
at org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess.obtainConnection(JdbcEnvironmentInitiator.java:437) ~[hibernate-core-6.5.2.Final.jar!/:6.5.2.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.DdlTransactionIsolatorNonJtaImpl.getIsolatedConnection(DdlTransactionIsolatorNonJtaImpl.java:46) ~[hibernate-core-6.5.2.Final.jar!/:6.5.2.Final]
... 130 common frames omitted
Caused by: java.net.ConnectException: Connection refused
at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:542) ~[na:na]
at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592) ~[na:na]
at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:327) ~[na:na]
at java.base/java.net.Socket.connect(Socket.java:751) ~[na:na]
at org.postgresql.core.PGStream.createSocket(PGStream.java:243) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.core.PGStream.<init>(PGStream.java:98) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.core.v3.ConnectionFactoryImpl.tryConnect(ConnectionFactoryImpl.java:136) ~[postgresql-42.7.3.jar!/:42.7.3]
at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:262) ~[postgresql-42.7.3.jar!/:42.7.3]
... 144 common frames omitted
my application.properties file
server.port=9500
spring.application.name=test_service
#database
spring.datasource.url=jdbc:postgresql://${PGHOST:localhost}:${PGPORT:5432}/${POSTGRES_DB:test-dev1}
spring.datasource.username=postgres
spring.datasource.password=${POSTGRES_PASSWORD:abcd}
#JPA Config
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
postgres docker run command
docker run --network=bridge -e POSTGRES_PASSWORD=root -e POSTGRES_DB=test-dev --name postgresdb -p 5051:5432 -d postgres:16.4
spring boot docker run command
docker run -it --name testservice -p 9100:9500 --network=bridge -e PGHOST=postgresdb -e PGPORT=5432 -e POSTGRES_DB=test-dev -e POSTGRES_PASSWORD=root test_service
I tried using –network attribute. Also I Tried to run in different ports also tried –link
3
You’ve got two unrelated problems here.
Application configuration
The fact that your testservice
application is attempting to connect to localhost
means that you’re not correctly configuring the application; it looks as if the PGHOST
variable isn’t useful in the context of your application. Until you fix this problem, nothing else matters. You cannot reach postgres by connecting to localhost
.
I don’t know anything about your application framework; maybe environment variables aren’t expanded in the configuration file? As a first pass, try replacing the environment variables with literal values:
spring.datasource.url=jdbc:postgresql://postgresdb:5432/test-dev
spring.datasource.username=postgres
spring.datasource.password=secret
Once you have things working with literal values, then you can start looking into why variable expansion wasn’t working as you expected.
Name lookups don’t work on the default bridge
network
Name lookups don’t work on the default bridge
network. One reason why docker compose
is convenient is because it takes care of setting a user-defined network for you (and attaching your containers to it), but it doesn’t do anything that we can’t accomplish through other means.
You’ll need to first create a user-defined network:
docker network create mynetwork
And then attached your containers to this network:
docker run --network=mynetwork --name postgresdb -d
-e POSTGRES_USER=postgres
-e POSTGRES_PASSWORD=secret
-e POSTGRES_DB=test-dev docker.io/postgres:16.4
docker run -it --network=mynetwork --name testservice -p 9100:9500 test_service
Note that I’ve made a couple of changes here to your docker
commands:
- Both containers are attached to the
mynetwork
network. - I’ve removed the port publishing on the postgres container; you can put it back if you really need it, but typically your database only needs to be accessed by your service (for debugging you can always
docker exec
into the postgres container). - I’ve removed all the environment variables on the
testservice
container, since we’re not using them at the moment.
The equivalent docker compose configuration to the above would be:
services:
postgresdb:
image: docker.io/postgres:16.4
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: secret
POSTGRES_DB: test-dev
testservice:
image: test_service
ports:
- "9100:9500"
tty: true
stdin: true
This should work; if you’d like to update your question with a [mcve] that I can run to test things out I would be happy to give that a try.
3