[英]java.net.ConnectException when running test with Flyway and embedded Postgresql inside Docker container
In order to properly test my Spring Boot application I am using embedded Postgresql in integration tests. 为了正确测试我的Spring Boot应用程序,我在集成测试中使用了嵌入式Postgresql 。 Moreover I use Flyway too which is configured to use data source based on Postgresql configuration (take a look into EmbeddedPostgresConfiguration
and FlywayConfiguration
classes). 此外,我也使用Flyway,它被配置为使用基于Postgresql配置的数据源(请查看EmbeddedPostgresConfiguration
和FlywayConfiguration
类)。
All my tests pass without problems when running on my environment (OS X), but they fail when being run inside Docker container (for both java:8
and openjdk:8
images). 在我的环境(OS X)上运行时,我所有的测试均顺利通过,但在Docker容器中运行时均失败(对于java:8
和openjdk:8
映像)。 And I have no clue what is the reason for that difference and how to fix it to have tests passing inside Docker (it is needed for further Continuous Integration pipeline). 而且我不知道造成这种差异的原因是什么,以及如何解决它以使测试在Docker内部通过(进一步的持续集成管道需要它)。
Here is minimal code sample to replicate the issue: https://github.com/nkoder/postgresql-embedded-example . 这是复制该问题的最小代码示例: https : //github.com/nkoder/postgresql-embedded-example 。
To run tests without problems run ./gradlew clean test
. 要运行测试没有问题,请运行./gradlew clean test
。 To run tests with failure run docker build .
要运行失败的测试,请运行docker build .
. 。
Error thrown during tests run inside Docker: 在Docker内部运行测试期间引发的错误:
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.springframework.beans.BeanInstantiationException
Caused by: org.flywaydb.core.api.FlywayException
Caused by: org.postgresql.util.PSQLException
Caused by: java.net.ConnectException
I was trying to solve this issues for my own but I still have only some guesses and nothing solid. 我试图自己解决这个问题,但我仍然只有一些猜测,没有任何根据。 The only "hint": for hardcoded (non-random) port used in embedded Postgresql configuration sometimes similar exception occured in line flyway.clean();
唯一的“提示”:对于嵌入式Postgresql配置中使用的硬编码(非随机)端口,有时在line flyway.clean();
也会发生类似的异常flyway.clean();
in FlywayConfiguration
class (when running tests from IntelliJ IDEA in real project with more tests) which seems to be first line of code which access database through provided data source. 在FlywayConfiguration
类中(当在实际项目中从IntelliJ IDEA运行测试并进行更多测试时),这似乎是通过提供的数据源访问数据库的第一行代码。
Update 1: Postgresql instance is created during tests on localhost on random open port inside same Docker container. 更新1: Postgresql实例是在同一Docker容器内的随机开放端口上的localhost上的测试过程中创建的。 I suspect there is no need for connectivity to host machine (outside container). 我怀疑没有必要连接到主机(外部容器)。
Problem solved! 问题解决了!
According to https://github.com/yandex-qatools/postgresql-embedded we cannot use root to run embedded Postgresql in tests. 根据https://github.com/yandex-qatools/postgresql-embedded的说明,我们无法在测试中使用root运行嵌入式Postgresql。 Therefore what I needed to fix the problem was to run tests inside Docker as non-root user. 因此,我需要解决的问题是以非root用户身份在Docker内部运行测试。
You can see fix here: https://github.com/nkoder/postgresql-embedded-example/commit/391977052b1563cdcabf66a2fe3ca0a3e0a7b358 您可以在此处查看修复程序: https : //github.com/nkoder/postgresql-embedded-example/commit/391977052b1563cdcabf66a2fe3ca0a3e0a7b358
Your problem is most likely that you application is trying to reach the DB at localhost. 您的问题很可能是您的应用程序试图访问本地主机上的数据库。 But when it is running in a docker container localhost is the container and not the hist machine. 但是,当它在docker容器中运行时,localhost是容器,而不是历史机器。 So you'll have to change the config to make this work. 因此,您必须更改配置以使其正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.