ebean-test has hardcoded "localhost" instead of the docker container's hostname
See original GitHub issueExpected 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:
- Created 2 years ago
- Comments:13 (9 by maintainers)
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 adockerHost
property which will be used when ebean-test detects it is running inside docker.Cheers, Rob.
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 todocker 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.