question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ebean-test has hardcoded "localhost" instead of the docker container's hostname

See original GitHub issue

Expected behavior

I work on projects where the CI processes (e.g. gradle maven etc.) run inside docker containers. When using ebean-test, my expectation was that ebean would be able to automatically detect the hostname of the database container and connect to it.

Actual behavior

ebean-test always expects the db servers to be accessible via localhost. Examples: https://github.com/ebean-orm/ebean/blob/7cfecb83a91621b49abe0d597ed41d7b08c6eb22/ebean-test/src/main/java/io/ebean/test/config/platform/MySqlSetup.java#L16 https://github.com/ebean-orm/ebean/blob/7cfecb83a91621b49abe0d597ed41d7b08c6eb22/ebean-test/src/main/java/io/ebean/test/config/platform/PostgresSetup.java#L16

Since my tests are already running inside a container, localhost will refer to the container of the gradle/mvn/etc process, not the host machine, thus making it impossible to establish a connection to the database.

Steps to reproduce

  • Create a dummy ebean application
  • Configure the tests:
ebean:
  test:
    platform: mysql
    ddlMode: dropCreate
    dbName: asdf
    username: admin
    password: admin
  • Package the project in a docker image and run it. The image CMD will be something along the lines of ./gradlew test or ./mvnw test depending on the project config. It’s necessary to pass /var/run/docker.sock as a volume to make Docker accessible from within the container.
  • ebean will be able to start a mysql container, try to connect to it for a few seconds and then fail the tests.

Example log showing that ebean tries to connect to localhost

Caused by:
                    java.lang.ExceptionInInitializerError
                        at io.ebean.DB.<clinit>(DB.java:66)
                        at com.picpay.atlas.persistence.configuration.PersistenceTestSetup.database(PersistenceTestSetup.kt:48)
                        at com.picpay.atlas.persistence.configuration.PersistenceTestSetup$$EnhancerBySpringCGLIB$$ea5b36d0.CGLIB$database$0(<generated>)
                        at com.picpay.atlas.persistence.configuration.PersistenceTestSetup$$EnhancerBySpringCGLIB$$ea5b36d0$$FastClassBySpringCGLIB$$f1073658.invoke(<generated>)
                        at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
                        at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
                        at com.picpay.atlas.persistence.configuration.PersistenceTestSetup$$EnhancerBySpringCGLIB$$ea5b36d0.database(<generated>)
                        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
                        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
                        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
                        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
                        ... 176 more

                        Caused by:
                        java.lang.RuntimeException: io.ebean.datasource.DataSourceInitialiseException: Error initialising DataSource with user: admin url:jdbc:mysql://localhost:4306/atlas error:Communications link failure

                        The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
                            at io.ebean.DbContext.<init>(DbContext.java:56)
                            at io.ebean.DbContext.<clinit>(DbContext.java:23)
                            ... 188 more

                            Caused by:
                            io.ebean.datasource.DataSourceInitialiseException: Error initialising DataSource with user: admin url:jdbc:mysql://localhost:4306/atlas error:Communications link failure

Is this by design? Can ebean support this? Would it be difficult to implement? If not, I think I can contribute with a PR.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:13 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
rbygravecommented, Jan 14, 2022

Ok, I have somewhat tested this PR / Fix and I think it should work. I have not tested strictly in Docker but the docker detection all looks correct and manually simulated that for in docker linux.

So this should be fixed in 12.14.1 … so great for you to confirm that in your CI environment if you can (and I’ll look to release 12.14.1 today or tomorrow).

For in docker CI environments where 172.17.0.1 does not work (for linux), we can explicitly specify a dockerHost property which will be used when ebean-test detects it is running inside docker.

Cheers, Rob.

1reaction
jairovskycommented, Jan 13, 2022

yes, yes. The thing is host.docker.internal apparently only comes by default on Mac and Windows. At least in my tests on linux I needed to docker run ... --add-host="host.docker.internal:host-gateway" for it to work, hence why I’ll need to check if can do this in my CI environment.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Connection between docker containers as localhost
I dont want to change all localhosts in my code to the mysql / redis container names. Is there a way to keep...
Read more >
How to reach localhost on host from docker container?
I have done the trick with another container. ... To connect to the mongo -container from app -container use the hostname mongo.
Read more >
nginx proxy cannot resolve container hostnames (Version 2 ...
I've attempted to migrate my stack to use version 2 docker-compose.yml and have run into a problem with network hostnames not being resolved ......
Read more >
Learn how to access a docker container by its name from host ...
Having the hostname of the docker service is useful, since you can see the name followed by its port. For example, instead of ......
Read more >
Why Can't I Connect to Kafka? | Troubleshoot Connectivity
The broker metadata is showing now with a hostname that correctly resolves from the client. Scenario 2: Kafka and client running in Docker....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found