1

I am trying to dockerize my Spring boot application and a Postgres db by using a docker-compose like this:

version: "3"
  services:
    springapp:
      build: .
      container_name: springapp
      environment:
        SPRING_DATASOURCE_URL: jdbc:postgresql://local:5432/userdb
      ports:
        - 8080:8080
      restart: always
      depends_on:
        - db
    db:
      image: postgres
      container_name: db
      environment:
        - POSTGRES_USER=postgres
        - POSTGRES_PASSWORD=postgres
        - POSTGRES_DB=userdb
        - PGDATA=/var/lib/postgresql/data/pgdata
      ports:
        - 5432:5432
      volumes:
        - pgdata:/var/lib/postgresql/data
      restart: always
  volumes:
    pgdata:

My application.properties is this:

##connect to database
spring.datasource.driver-class-name=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/userdb
spring.datasource.username=postgres
spring.datasource.password=postgres

##JPA config
spring.jpa.properties..hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=false
spring.jpa.properties.hibernate.format_sql=true

#file config
spring.servlet.multipart.max-file-size=3MB
spring.servlet.multipart.max-request-size=3MB

My Dockerfile is this:

FROM openjdk:17-jdk-alpine
ARG  JAR_FILE=target/*.jar
COPY usermanager-0.0.1-SNAPSHOT.jar application.jar
ENTRYPOINT ["java", "-jar", "application.jar"]

When I try to use my application in local on sts I recieve this output;

2023-05-08 09:36:49.154  INFO 17436 --- [           main] c.u.a.u.UsermanagerApplication           : Starting UsermanagerApplication using Java 14.0.2 on VT-PastoreGu with PID 17436 (C:\Users\PastoreGu\eclipse-workspace\usermanager\target\classes started by PastoreGu in C:\Users\PastoreGu\eclipse-workspace\usermanager)
2023-05-08 09:36:49.158  INFO 17436 --- [           main] c.u.a.u.UsermanagerApplication           : No active profile set, falling back to 1 default profile: "default"
2023-05-08 09:36:50.092  INFO 17436 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2023-05-08 09:36:50.151  INFO 17436 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 49 ms. Found 1 JPA repository interfaces.
2023-05-08 09:36:50.589  INFO 17436 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-05-08 09:36:50.596  INFO 17436 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-05-08 09:36:50.596  INFO 17436 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.74]
2023-05-08 09:36:50.768  INFO 17436 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-05-08 09:36:50.768  INFO 17436 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1569 ms
2023-05-08 09:36:50.973  INFO 17436 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2023-05-08 09:36:51.015  INFO 17436 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate ORM core version 5.6.15.Final
2023-05-08 09:36:51.132  INFO 17436 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.2.Final}
2023-05-08 09:36:51.213  INFO 17436 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2023-05-08 09:36:51.420  INFO 17436 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Start completed.
2023-05-08 09:36:51.432  INFO 17436 --- [           main] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.PostgreSQLDialect
2023-05-08 09:36:51.975  INFO 17436 --- [           main] o.h.e.t.j.p.i.JtaPlatformInitiator       : HHH000490: Using JtaPlatform implementation: [org.hibernate.engine.transaction.jta.platform.internal.NoJtaPlatform]
2023-05-08 09:36:51.987  INFO 17436 --- [           main] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2023-05-08 09:36:52.745  WARN 17436 --- [           main] JpaBaseConfiguration$JpaWebConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2023-05-08 09:36:53.141  INFO 17436 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-05-08 09:36:53.340  INFO 17436 --- [           main] c.u.a.u.UsermanagerApplication           : Started UsermanagerApplication in 4.517 seconds (JVM running for 5.403)

But When I try by using the command: docker-compose up I get error about not connection to the db:

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.

How Can I solve it?

0

1 Answer 1

2

When connecting from one container to another, in the same docker-compose stack, you should use the service name as the hostname. Instead of localhost , try db:

spring.datasource.url=jdbc:postgresql://db:5432/userdb

Read more about networking with Docker Compose here

Sign up to request clarification or add additional context in comments.

3 Comments

If I try to use jdbc:postgresql://db:5432/userdb in docker-compose.yml and in application.properties I get this error: 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.
If I try to use jdbc:postgresql://db:5432/userdb in docker-compose.yml and in application.properties I cannot build the jar file that i set on DockerFile
Make sure to rebuild your project's jar since your dockerfile is pointing to it and might not have the latest changes. The change in the properties file itself should be enough to make it work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.