Java应用程序构建失败,其中Mysql url指向Docker容器 [英] Java app build fails with Mysql url pointing to a Docker Container

查看:66
本文介绍了Java应用程序构建失败,其中Mysql url指向Docker容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序,需要连接到MySQL。

I have a Java App which needs to connect to MySQL.

提供 url时:jdbc:mysql:// localhost:3306 /< dbname> 建立罐子。但是,我想在网络中的Docker容器上运行它,在该容器中,我还运行了名称为 mysqlindocker 的MySQL容器。为了构建Jar,我将URL放在 jdbc:mysql:// mysqlindocker:3306 /< dbname> 中,希望在容器中运行。但是我做不到,因为构建失败。我的猜测是,在我的本地计算机上,Spring无法指向该数据库URL。

On providing url: jdbc:mysql://localhost:3306/<dbname> I am able to build the Jar. I, however, want to run it on a Docker Container in a network where I also have a MySQL container with name mysqlindocker running. To build the Jar I put in URL as jdbc:mysql://mysqlindocker:3306/<dbname> which I hope would work on running inside the container. But I fail to do it because the build fails. My guess is that on my local machine Spring is not able to point to that db url.

如何用 jdbc:mysql:// mysqlindocker:3306 /< dbname> ?

PS我不希望指向在本地主机上运行的MySQL。

P.S. I would not prefer pointing to MySQL running on localhost.

推荐答案


我使用mvn clean构建jar安装。它在@Test void
contextLoads()上失败。如果我将其注释掉,则可以使用db url指向我的> docker容器

I build the jar using mvn clean install. It fails on @Test void contextLoads(). If I comment that out, I am able to build the jar with db url point to my > docker container

在Maven的 test 阶段执行的测试,无法解析 mysqlindocker 主机名:

In the frame of unit tests executed during the test phase of maven, the mysqlindocker hostname cannot be resolved :

jdbc:mysql://mysqlindocker:3306/<dbname>

只有同一个docker网络中的容器才能解决该问题。

测试是在容器外部执行的(确切地说是在启动之前),它们无法访问该网络。

Only containers inside the same docker network will resolve that.
And as these tests are executed outside the container (before to start it to be exact), they cannot access that network.

如何解决?

1)解决根本原因

实际上,在单元测试期间构建失败的根本原因执行是您没有根据目标范围定义jdbc url。

实际上,在maven构建的 test 阶段,您通常需要测试使用内存数据库或特定的MySQL数据库。出于一致的原因(测试可重复性),您不想使用与主应用程序使用的相同的软件。
这里的一个好习惯是使用另一个数据库实例进行单元测试,并且应该可以从运行构建的主机( localhost )上访问该数据库。

您可以为执行的测试覆盖 spring.datasource.url 属性:通过定义 application-test.properties/yml src / test / resources 中的c $ c>文件,或直接在测试类 @SpringBootTest(properties = ...)中覆盖该属性

In fact, the root cause of your build failure during the unit test execution is that you didn't define a jdbc url according to the target scope.
Indeed in the test phase of a maven build you generally want tests to use an in-memory database or a specific MySQL database. You don't want to use the same one than the which one used for the main application for consistent reasons (test reproducibility). Here a good practice is using another db instance for unit testing and that db should be accessible from the host that runs the build (localhost).
You can overriding the spring.datasource.url property for executed tests : either by defining an application-test.properties/yml file in src/test/resources or overriding the property directly in the test class @SpringBootTest(properties=...).

2)解决方法

请注意,如果MySQL db容器端口发布在构建所在的主机上执行时,(临时)解决方法是将localhost保留在 spring.datasource.url 中为构建定义的url中,并在运行时使用docker容器名称覆盖它您将JAR作为容器端点运行:

Note that if the MySQL db container port is published on the host where the build is executed, a (temporary) workaround would be to keep localhost in the url defined in spring.datasource.url for the build and to override it with the docker container name at runtime when you run the JAR as container endpoint :

java -jar myApp.jar --spring.config.location=jdbc:mysql://mysqlindocker:3306/dbname

这篇关于Java应用程序构建失败,其中Mysql url指向Docker容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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