Docker - Spring Boot 应用程序 - 无法访问本地主机上的 MySql 服务器 [英] Docker - Spring Boot application - cannot access MySql server on localhost

查看:29
本文介绍了Docker - Spring Boot 应用程序 - 无法访问本地主机上的 MySql 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将在 Docker 中运行的 Spring Boot (JAR) 应用程序连接到我计算机上的 MySql 服务器?[我尝试了不同的帖子,但没有帮助]

How can I connect a Spring Boot (JAR) application, running in Docker, to my MySql server on my computer? [I tried different posts, but that didn't help]

在我的 Spring Boot 'application.properties' 中,我有:

In my Spring Boot 'application.properties' I have:

spring.datasource.url = jdbc:mysql://localhost:3306/geosoldatabase

我尝试了多种选择:

$ docker run -p 8080:8080 --name containername imagename

$ docker run --net="host" -p 8080:8080 --name containername imagename

$ docker run -p 8080:8080 --add-host=localhost:192.168.99.100 --name containername imagename

但是,我无法连接到 MySql 服务器.休眠失败.在我的 CAAS 提供商上,这一切都很好——当然还有一个已知的容器名称.

But alas, I cannot get connection to the MySql server. Hibernate fails. On my CAAS provider this all works nicely - of course with a known container name.

我的 Dockerfile 很简单:

My Dockerfile is very simple:

FROM fabric8/java-jboss-openjdk8-jdk
ENV JAVA_APP_JAR myapplication.jar
ENV AB_OFF true
EXPOSE 8080
ADD target/$JAVA_APP_JAR /deployments/

按照建议,也可以使用环境变量.这是我到目前为止所做的:

As suggested, environment variables can also be used. This is what I've done so far:

  • 在Windows10环境设置界面中定义,我定义了以下环境变量:[1] DATABASE_HOST=127.0.0.1:3306 and [2] DATABASE_NAME=mydbname
  • 我按照建议更改了 application.properties 文件:spring.datasource.url = jdbc:mysql://${DATABASE_HOST}/${DATABASE_NAME}

在我输入docker push..."后的 Docker 快速入门屏幕中,我遇到了同样的错误.这次的原因不同了:

In the Docker Quickstart screen after I type "docker push... " I get the same errors. This time the cause is different:

引起:java.net.UnknownHostException: ${DATABASE_HOST}: Name or服务未知.

Caused by: java.net.UnknownHostException: ${DATABASE_HOST}: Name or service not known.

要检查环境变量是否设置正确,我输入:echo ${DATABASE_HOST}",我得到值127.0.0.1:3306".

To check whether the environment variables are correctly set, I type: "echo ${DATABASE_HOST}" and I get the value "127.0.0.1:3306".

更新:建议将docker-machine ip"地址放入 database_host 变量中.原因现在有点不同:

Update: suggested was to put the 'docker-machine ip' address into the database_host variable. The cause was now a bit different:

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open JDBC connection for schema management target

Caused by: org.hibernate.tool.schema.spi.SchemaManagementException: Unable to open JDBC connection for schema management target

推荐答案

在您的 docker run 命令中使用 --network="host" ,那么您的 docker 容器中的 127.0.0.1 将指向您的 docker 主机.请参阅这篇文章中的答案.

Use --network="host" in your docker run command, then 127.0.0.1 in your docker container will point to your docker host. Refer to the answers in this post.

这篇关于Docker - Spring Boot 应用程序 - 无法访问本地主机上的 MySql 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆