In order to properly test my Spring Boot application I am using embedded Postgresql in integration tests. Moreover I use Flyway too which is configured to use data source based on Postgresql configuration (take a look into EmbeddedPostgresConfiguration and FlywayConfiguration classes).
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). 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).
Here is minimal code sample to replicate the issue: https://github.com/nkoder/postgresql-embedded-example .
To run tests without problems run ./gradlew clean test. To run tests with failure run docker build ..
Error thrown during tests run inside 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(); 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.
Update 1: Postgresql instance is created during tests on localhost on random open port inside same Docker container. I suspect there is no need for connectivity to host machine (outside container).
localhost:<randomOpenPort>and it's used as data source. There is nothing reaching internet "outside" a container (or at least nothing I am aware of).