1

I'am trying to use docker-compose with a springboot app(uses JPA) and a postgres database, I am kinda new using docker and I don't know what I am doing wrong.

Problem

When I build the springboot app, if I change spring.datasource.url=jdbc:postgresql://localhost:5432/postgres(code that works) to host it in postgres container, spring.datasource.url=jdbc:postgresql://postgres:5432/postgres.

It throws me a PSQLException caused by: java.net.UnknownHostException: postgres.(More details below)

application.properties

server.port = 8080

## default connection pool
spring.datasource.hikari.connectionTimeout=20000
spring.datasource.hikari.maximumPoolSize=5

## PostgreSQL
spring.datasource.url=jdbc:postgresql://postgres:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=postgres

spring.jpa.hibernate.ddl-auto=create

Exception

2020-06-22 19:07:10.597  INFO 16088 --- [           main] c.a.a.AnnotationToolApplicationTests     : Starting AnnotationToolApplicationTests on DESKTOP-P1E07PA with PID 16088 (started by Pedro in C:\Users\Pedro\git\tfg\annotation-tool\annotation-tool)
2020-06-22 19:07:10.598  INFO 16088 --- [           main] c.a.a.AnnotationToolApplicationTests     : No active profile set, falling back to default profiles: default
2020-06-22 19:07:11.584  INFO 16088 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2020-06-22 19:07:11.719  INFO 16088 --- [           main] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 120ms. Found 3 JPA repository interfaces.
2020-06-22 19:07:12.235  INFO 16088 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2020-06-22 19:07:12.572  INFO 16088 --- [           main] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [name: default]
2020-06-22 19:07:12.746  INFO 16088 --- [           main] org.hibernate.Version                    : HHH000412: Hibernate Core {5.4.10.Final}
2020-06-22 19:07:13.002  INFO 16088 --- [           main] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2020-06-22 19:07:13.434  INFO 16088 --- [           main] com.zaxxer.hikari.HikariDataSource       : HikariPool-1 - Starting...
2020-06-22 19:07:16.873 ERROR 16088 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.
org.postgresql.util.PSQLException: The connection attempt failed.
        at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:292) ~[postgresql-42.2.9.jar:42.2.9]
...
 at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:126) ~[surefire-booter-2.22.2.jar:2.22.2]
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:418) ~[surefire-booter-2.22.2.jar:2.22.2]
Caused by: java.net.UnknownHostException: postgres
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:184) ~[na:1.8.0_222]

docker-compose.yml

version: '3'
services: 
    # SpringBoot App
    postgres:
        image: "postgres:9.6-alpine"  
        ports: 
          - 5432:5432
        volumes: 
          - apiDB:/var/lib/postgresql/data

        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres


    api:
      build: ./annotation-tool
      ports: 
        - 8080:8080
      depends_on:
        - postgres


    client:
      container_name: client-container
      build: ./client
      ports: 
        - 8081:8081


  
volumes: 
    apiDB:

Api Dockerfile

FROM maven:3.6.1-jdk-8-slim AS build
RUN mkdir -p /workspace
WORKDIR /workspace
COPY pom.xml /workspace
COPY src /workspace/src
RUN mvn -f pom.xml clean package

FROM openjdk:8
COPY --from=build /workspace/target/*.jar app.jar
EXPOSE 8080
ENTRYPOINT ["java","-jar","app.jar"]

Things that I think that happened/ tried

Maybe the DNS is not working, but I don't know how I can solve it

I tried differents docker-compose and Dockerfiles and it doesn't work

If I run the docker-compose the postgres container is created succesfully

I tried changing the url host to postgres-backend as my container was named that way

I tried by using links between my components.

9
  • 2
    Your container is named postgres-backend, can you try changing the url to include that instead? Commented Jun 22, 2020 at 14:12
  • It doesn't work spring.datasource.url=jdbc:postgresql://postgres-backend:5432/postgres, same error Commented Jun 22, 2020 at 14:15
  • 1
    I'd suggest deleting all of the links: and container_name: options; they probably won't make a difference but might confuse things, and they're not necessary. At what point in your application's execution are you trying to connect to the database? (Do you get this exception in response to an HTTP request? Are you trying to run migrations in a Dockerfile?) Commented Jun 22, 2020 at 14:22
  • 1
    Things that run in the Dockerfile can't connect to the database or other Compose-managed resources. (They aren't on the Compose network, they don't see environment variables or network settings, and there's no guarantee on build ordering.) Commented Jun 22, 2020 at 14:30
  • 1
    The build shouldn't depend on the database. ("Here's a jar file, but it's not really useful because the external database is only on my machine.") I'm not sure what specifically you're trying to build but doing database-related setup at startup time is better than at build time. Commented Jun 22, 2020 at 16:06

3 Answers 3

1

I think springboot application is finding postgres db before its container is getting up...you can add depends_on property in springboot container ..so the postgres container starts first then springboot

version: '3'
services: 
    # SpringBoot App
    postgres:
        image: "postgres:9.6-alpine"  
        ports: 
          - 5432:5432
        volumes: 
          - apiDB:/var/lib/postgresql/data

        environment:
          - POSTGRES_DB=postgres
          - POSTGRES_USER=postgres
          - POSTGRES_PASSWORD=postgres


    api:
      build: ./annotation-tool
      ports: 
        - 8080:8080
      depends_on:
        - postgres


    client:
      container_name: client-container
      build: ./client
      ports: 
        - 8081:8081


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

1 Comment

It doesn't work, I think the problem is while compiling maven to create the .jar when doesnt recognize the host. Thank you anyways
0

I don't know if I am thinking to simple right know but all container of your docker-compose file run on the same host, right? So then your url should point to jdbc:postgresql://localhost:5432/postgres.

1 Comment

It doesn't work, exception telling that the host or the port is wrong
0

I don't know if you already solved the problem but have you tried to use a bridge network on the setup?

version: '3'
services: 
    # SpringBoot App
    postgres:
     image: "postgres:9.6-alpine"  
     ports: 
       - 5432:5432
     volumes: 
       - apiDB:/var/lib/postgresql/data

     environment:
       - POSTGRES_DB=postgres
       - POSTGRES_USER=postgres
       - POSTGRES_PASSWORD=postgres
     networks:
       - database-network


    api:
      build: ./annotation-tool
      ports: 
        - 8080:8080
      depends_on:
        - postgres
      networks:
        - database-network


    client:
      container_name: client-container
      build: ./client
      ports: 
        - 8081:8081

networks:
  database-network:
    driver: bridge
    external: false
  
volumes: 
    apiDB:

1 Comment

it doesn't work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.