0

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).

4
  • ConnectException arises always when a proxy or firewall is blocking an output connection. Have you reviewed the internetworking configuration of Docker? Commented Sep 5, 2016 at 21:31
  • Both tests and embedded Postgresql run in same container. I mean in test there is postgresql created at 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). Commented Sep 6, 2016 at 5:15
  • I see, but I insist: Even though the communications are kept within the same localhost, connecting to a local port may be blocked by the local firewall. Commented Sep 6, 2016 at 6:20
  • OK, I misunderstood you then and I understand now. Will check Commented Sep 6, 2016 at 7:02

2 Answers 2

2

Problem solved!

According to https://github.com/yandex-qatools/postgresql-embedded we cannot use root to run embedded Postgresql in tests. Therefore what I needed to fix the problem was to run tests inside Docker as non-root user.

You can see fix here: https://github.com/nkoder/postgresql-embedded-example/commit/391977052b1563cdcabf66a2fe3ca0a3e0a7b358

Sign up to request clarification or add additional context in comments.

Comments

0

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. So you'll have to change the config to make this work.

4 Comments

I want to connect to Docker's localhost (inside container). There is nothing on host machine that I want to connect to. There is no "external" Postgresql, only this one created during tests run, inside the same container.
How do you create a Postgres? And if there is no DB running in your Container this is why it fails.
Embedded Postgres configuration is defined in EmbeddedPostgresConfiguration. There is no standalone Postgres (neither in Docker nor on my local environment where tests pass_
Well but this is what I mean. Embedded Postgres is not an in JVM DB like HSQL or H2 but only a wrapper that starts a local instance as I understood the yandex site. Therefore it will just not work if you don't have Postgres installed in your container.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.