0

I'm encountering an issue when trying to run a Spring Boot application along with a PostgreSQL container using Docker Compose. The Spring Boot application is unable to connect to the PostgreSQL container, and I'm consistently getting the following error:

org.postgresql.util.PSQLException: Connection to postgresql:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.

I am using two separate Docker Compose files, one for the Spring Boot application and another for the PostgreSQL container and I declared the containers in the same network.

In the Spring Boot application properties, I'm using the correct PostgreSQL container name in the datasource URL:

  • application.properties file

    spring.jpa.hibernate.ddl-auto=update spring.datasource.url=jdbc:postgresql://postgresql:5432/mydb spring.datasource.username=admin spring.datasource.password=admin

  • database docker-compose file

    version: '2' services:

    networks: mynetwork: driver: bridge

    database container

    database:
      image: postgis/postgis
      container_name: postgresql
      networks:
        - mynetwork
      environment:
        - POSTGRES_USER=admin
        - POSTGRES_PASSWORD=admin
        - POSTGRES_DB=mydb
      ports:
        - "5432:5432"
      volumes:
        - "./datadb:/var/lib/postgresql/data"
    
  • Spring docker-compose file

    version: '2' services: backend: build: . container_name: spring-application ports: - 8085:8085 networks: - mynetwork environment:

         - SPRING_DATASOURCE_URL=jdbc:postgresql://postgresql:5432/mydb
         - SPRING_DATASOURCE_USERNAME=admin
         - SPRING_DATASOURCE_PASSWORD=admin
         - SPRING_JPA_HIBERNATE_DDL_AUTO=update
      depends on: 
         - database
    

    networks: mynetwork: driver: bridge

I also add this line in the pg_hba.conf file and retsart the postgresql container:

host    all             all             0.0.0.0/0               trust

Despite these configurations, the connection is refused, and the Spring Boot application fails to start.

what I am missing? Any ideas PLEASE

possible solutions to address this issue please?

9
  • 1
    Don’t post code as image, please update the question Commented Dec 7, 2023 at 21:30
  • can you update spring.datasource.url to jdbc:postgresql://localhost:5432/mydb and check Commented Dec 8, 2023 at 0:22
  • @AndreiLisa thank you for your feedback. I tried to modify the post; even if I select the entire code and apply the code formatting rule, here is the result it gives. Commented Dec 8, 2023 at 9:57
  • I see that you have 2 docker-compose files. Am i right ? Commented Dec 8, 2023 at 10:02
  • @AndreiLisa yes i am using 2 docker-compose files, for easier control and modifications Commented Dec 8, 2023 at 10:13

1 Answer 1

0

As usually this kind of staff(database + back-end application) are together in the same docker-compose file, because back-end depends on database and it is more flexible and easy to maintain it. Also docker-compose by default set a network, more details you could find Networking-Compose. As I suggested in comments of question you should have just one single docker-compose file and all is going to work well.

As an example of docker-compose file that you can use in your case:

version: '3'

services:
  backend:
    image: "spring-application"
    build:
      context: . // Dockerfile of back-end have to be in the same context path as this compose file
      dockerfile: Dockerfile
    container_name: "spring-application"
    ports:
      - "8085:8085"
    depends_on:
      - "database"
    environment:
      - "SPRING_DATASOURCE_URL=jdbc:postgresql://database:5432/mydb"
      - "SPRING_DATASOURCE_USERNAME=admin"
      - "SPRING_DATASOURCE_PASSWORD=admin"
      - "SPRING_JPA_HIBERNATE_DDL_AUTO=update"
  database:
    image: postgis/postgis
    container_name: postgresql
    environment:
    - POSTGRES_USER=admin
    - POSTGRES_PASSWORD=admin
    - POSTGRES_DB=mydb
    ports:
      - "5432:5432"
    volumes:
      - "./datadb:/var/lib/postgresql/data"
Sign up to request clarification or add additional context in comments.

Comments

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.