vasilis 123
vasilis 123

Reputation: 675

unable to connect to running postgresql container from local spring boot project

I have created a docker-compose.yml to run a postgresql database . While the image is pulled and can run succesfully I am unable to connect to my database through a spring boot project I have . When I try to run my spring boot project after succesfully running my postgresql container with docker-compose up I get the error org.postgresql.util.PSQLException: The connection attempt failed. and a huge text of error logs underneath where I spotted the cause.

Caused by: java.net.UnknownHostException: postgresqldb

which is the name of my db image .

My docker-compose.yml at the root of my spring project

version: '3.1'
services:
  postgresqldb:
    image: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=booksdb

My application.properties in my spring boot project .

server.port=8081
server.servlet.context-path=/rest

#Postgres
spring.datasource.url=jdbc:postgresql://postgresqldb:5432/booksdb
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.jpa.show-sql=true
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=create

#JWT
jwt.secret-key=someKey

I would appreciate your help .

Upvotes: 0

Views: 1385

Answers (1)

Roberto Paz
Roberto Paz

Reputation: 356

Maybe you should add a hostname to the container:

version: '3.1'
services:
  postgresqldb:
    image: postgres
    hostname: postgres
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_DB=booksdb

docker uses its own network and resolves hostname internally. You should assign a hostname to the running container that may be found for the rest of the containers.

Upvotes: 2

Related Questions