Spring Boot, PostgreSQL, and Docker - Connection Refused whil Running in Container

旧街凉风 提交于 2019-12-06 06:32:25

You are pointing your application towards localhost, but this is not shared between containers.

To access another container you have to refer to its hostname.

In your case, I understand that you want the inventory service to access the db service. So you should use the following datasource url:

spring.datasource.url=jdbc:postgresql://db:5432/leisurely_diversion

See this simple tutorial about connecting to a container from another container with docker compose: https://docs.docker.com/compose/gettingstarted/

Elaborating a little upon the answer given by ESala:

I agree, it is a networking issue and the given solution works fine, but you can also use localhost (e.g. if you really really want to), by switching to network host mode when running your containers (cf here). Like done here for nginx.

I'd say you won't want this most of the time, since it messes with the sandbox you gain. But the option exists.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!