如何将Docker内部的SpringBoot应用程序连接到PostgreSQL外部

时间:2018-09-29 19:21:02

标签: postgresql docker spring-boot

我在主机上运行Java SpringBoot Web应用程序。应用程序连接到PostgreSQL数据库。一切正常。操作系统-Ubuntu 18。

现在我需要将应用程序移至Docker容器中,但PostgreSQL仍将保留在主机上。

我安装了Docker,安装了容器,但是我在docker内部的应用无法使用默认设置(localhost)连接到PostgreSQL数据库。

这是我的 application.properties 文件:

spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:5432/webdemodb
spring.datasource.username=postgres
spring.datasource.password=123
spring.jpa.database-platform=org.hibernate.dialect.PostgreSQL95Dialect
spring.jpa.generate-ddl=true

这是我的 Dockerfile

FROM java:8
WORKDIR /
ADD target/webaccount-1.0-SNAPSHOT.jar app.jar
EXPOSE 8080
RUN fc-cache -f -v
ENTRYPOINT ["java","-jar","/app.jar"]

我了解了Docker的网络,但没有找到解决方案。我需要设置什么?

1 个答案:

答案 0 :(得分:0)

Artemiy,谢谢!为了简单使用,我在运行容器时仅应用了下一个选项:

--netstat="host"

完整命令:

docker run -d -p 9000:8080 --network="host" --name webaccount webaccount:1.0