I want to create a simple spring boot project using the Postgresql database. I am learning docker-compose. So, I make a docker-compose file where I first pull a Postgres image and connect that Postgres to my application jar.
But I am unable to do that. Only postgres-service was running in Docker-compose. Backend_application service was Exited. I did not find out the problem. Because when I ran the application locally, it got the connection with this postgres-serivce.
here is my docker-compose file:
services:
db:
container_name: postgres_container
image: postgres:latest
restart: always
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=1234
ports:
- 5432:5432
application_backend:
container_name: application_backend
build: .
ports:
- 9091:9090
environment:
db-url: jdbc:postgresql://db:5432/postgres
db-username: admin
db-password: 1234
depends_on:
- db
and here is application docker file:
FROM openjdk:21
EXPOSE 9090
ADD target/demo_e-commerce_app-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-jar", "app.jar"]
and here is application-properties file:
spring.application.name=demo_e-commerce_app
server.port=9090
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true
spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=admin
spring.datasource.password=1234
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.use_sql_comments=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.open-in-view=false
spring.jpa.defer-datasource-initialization=true
spring.sql.init.mode=always
here are some error logs:
application_backend | 2024-04-27T12:18:48.933Z ERROR 1 --- [demo_e-commerce_app] [ main] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception during pool initialization.
application_backend |
application_backend | 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.
application_backend | at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:342) ~[postgresql-42.6.2.jar!/:42.6.2]
application_backend | at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:54) ~[postgresql-42.6.2.jar!/:42.6.2]
application_backend | at org.postgresql.jdbc.PgConnection.<init>(PgConnection.java:263) ~[postgresql-42.6.2.jar!/:42.6.2]
application_backend | at org.postgresql.Driver.makeConnection(Driver.java:443) ~[postgresql-42.6.2.jar!/:42.6.2]
application_backend | at org.postgresql.Driver.connect(Driver.java:297) ~[postgresql-42.6.2.jar!/:42.6.2]
application_backend | at com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:138) ~[HikariCP-5.0.1.jar!/:na]
application_backend | at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:359) ~[HikariCP-5.0.1.jar!/:na]
application_backend | at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:201) ~[HikariCP-5.0.1.jar!/:na]
application_backend | at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:470) ~[HikariCP-5.0.1.jar!/:na]
application_backend | at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:561) ~[HikariCP-5.0.1.jar!/:na]
application_backend | at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:100) ~[HikariCP-5.0.1.jar!/:na]
application_backend | at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112) ~[HikariCP-5.0.1.jar!/:na]
How can I fix it?
Thank you so much for your attention and participation.