babastyle
babastyle

Reputation: 53

Spring Boot Docker Container can't connect to postgresql hosted on Docker host

when I use maven to build my spring boot app the application needs to have a connection to my postgresql database in the application.properties. It only can connect when I use

spring.datasource.url=jdbc:postgresql://localhost:5432/springbootdb

but when I containerize the buileded jar file, my application can't connect to my postgres database hosted outside the Container.

what is the solution for this problem?

I am still a Beginner

Upvotes: 3

Views: 3355

Answers (2)

Rohan J Mohite
Rohan J Mohite

Reputation: 2613

Where you hosted your PostgreSQL database?

Replace localhost with actual machine IP address (PostgreSQL database machine address)

spring.datasource.url=jdbc:postgresql:<ip_address>:5432/springbootdb

Upvotes: 1

GerNik
GerNik

Reputation: 41

From the 18.03 docs:

I want to connect from a container to a service on the host

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host.

The gateway is also reachable as gateway.docker.internal.

example:

spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/springbootdb

Upvotes: 3

Related Questions