3

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

2
  • Are you using Windows? Commented Jul 11, 2018 at 13:42
  • yes i am using Windows 10 Commented Jul 11, 2018 at 14:11

2 Answers 2

3

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

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

1 Comment

The information above can be found in the Networking documentation here: docs.docker.com/docker-for-mac/networking
1

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

5 Comments

didnt work for me: java.net.UnknownHostException: postgresql when I want to build with maven
Where is your PostgreSQL database? on same machine where docker installed? or outside? (windows?) and where is docker application (windows/ubuntu?)
it's on my windows localhost, but i think the Container has another Environment. same machine as docker right
is your firewall blocking incoming/outgoing port 5432? can you please add/allow both in and out 5432 on your windows firewall?
unfortunately it didn't work for me, thank you for your advices

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.