【问题标题】:Spring Boot application in Docker Container with MySQL in HostDocker 容器中的 Spring Boot 应用程序和主机中的 MySQL
【发布时间】:2019-01-31 16:52:00
【问题描述】:

我有一个Spring Boot 应用程序,我计划在Docker 容器上运行它并测试这个应用程序,我将在我的主机上连接到MySQL。为此,我在application.properties 中有一个属性,如下所示

spring.datasource.url = jdbc:mysql://host.docker.internal:3306/flexapp

现在当我执行./mvnw install dockerfile:build 时,它会在构建Docker image 之前尝试构建应用程序。它在建造时说

Caused by: java.net.UnknownHostException: host.docker.internal: nodename nor servname provided, or not known

显然它无法解析host.docker.internal。

如何将Docker 容器中的Spring Boot 应用程序连接到主机中的MySql 实例?我用谷歌搜索并尝试了很多解决方案,但似乎没有任何效果。

【问题讨论】:

    标签: docker spring-boot dockerfile


    【解决方案1】:

    问题是正在执行的一些测试试图连接到数据库。由于无法解析host.docker.internal,这些测试将失败。

    标准解决方案是使用 Spring Boot 提供的一种方法将配置外部化:Externalized Configuration。

    特别是,一种方法是将spring.datasource.url 设置为指向application.properties 中的localhost,并通过传递在Dockerfile 中覆盖它

    --spring.datasource.url= jdbc:mysql://host.docker.internal:3306/flexapp
    

    到 Dockerfile 中的入口点或 CMD 指令。

    【讨论】:

    • 你是对的。在Docker 进程启动之前,maven build 进程被执行,它不理解host.docker.internal。我会试试你提到的。谢谢!我希望 Docker 提供本地方式来构建 Spring Boot 应用程序
    • 您能否添加一个示例以将数据源 url 传递给 dockerfile 中的入口点?
    猜你喜欢
    • 2018-07-11
    • 2021-04-22
    • 2019-01-16
    • 2016-02-19
    • 2022-01-09
    • 1970-01-01
    • 2021-08-02
    • 2017-07-04
    • 2018-07-03
    相关资源
    最近更新 更多