2

I create an sample java project with springboot data and postgres. I started to make my docker-compose but i can't link my database to my application...

I have this error

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

On my application ressource

server:
  port: 4000

spring:
  datasource:
    platform: postgres
    url: jdbc:postgresql://localhost:5438/acronym
    username: acronym
    password: acronym1234
    driver-class-name: org.postgresql.Driver
  jpa:
    database: POSTGRESQL
    show-sql: true
    generate-ddl: true
    hibernate:
      ddl-auto: create

and my docker compose file

version: '3.3'
services:
  web:
    image: acronym:latest
    ports:
      - 4000:4000
    networks:
      - webnet
    depends_on:
      - db
  db:
    image: postgres:9.6
    restart: always
    environment:
     - POSTGRES_USER=acronym
     - POSTGRES_PASSWORD=acronym1234
     - POSTGRES_DB=acronym
      -PGDATA=/var/lib/postgresql/data/pgdata
    volumes:
         - /var/lib/postgresql/data
    ports:
      - '5438:5432'
volumes:
    db:
networks:
    webnet:

I saw a lot of topic and the solution is to replace localhost to db but that doesn't work

How can i link my database to my app with docker-compose ?

1 Answer 1

2

Use jdbc:postgresql://db:5438/acronym as connection url.

The service is exposed under its name by default (in that case db).

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

3 Comments

Thx you to reply ! Yes I tried to replace localhost to db on my applocation resource, I have the same issue :(
Ok i try again with db and the port 5432 ...and it work !! THX YOU
You mapped the ports, inside the docker network (webnet) it is available under the port 5438 and on the localhost over 5432. ports: - '5438:5432' You could use the same port to avoid confusion => ports: - '5438:5438'

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.