Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running test with testcontainers as part of a Dockerfile

My dockerfile looks something like this:

FROM maven:3-jdk-11-slim
COPY pom.xml .
COPY src src
RUN mvn clean install

That means that part of the build is the execution of the unit tests. Some of the unit tests use a testcontainer. Running mvn clean install on my local machine works fine, but running docker build . -t my-app doesn't because the testcontainers won't start.

(...)
15:54:38.793 [ducttape-0] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - Pinging docker daemon...
15:54:38.794 [ducttape-0] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory$1@355cb260
15:54:39.301 [ducttape-0] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - Pinging docker daemon...
15:54:39.301 [ducttape-0] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - Cmd: org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory$1@1c1a1359
15:54:39.469 [main] ERROR org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy - ping failed with configuration Environment variables, system properties and defaults. Resolved dockerHost=unix:///var/run/docker.sock due to org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception
org.rnorth.ducttape.TimeoutException: Timeout waiting for result with exception
(...)

I've seen examples of running docker run with working testcontainers, but how do I make my docker build work?

Help is much appreciated.

like image 697
michel404 Avatar asked Oct 15 '25 11:10

michel404


1 Answers

For future reference: I believe this is simply not possible.

docker run allows you to mount the Docker socket (and thus access the host's Docker daemon) with -v /var/run/docker.sock:/var/run/docker.sock.

docker build doesn't support such an argument.

My workaround will be to modify my Dockerfile to RUN mvn clean install -Dmaven.test.skip=true and run the unit tests separately.

like image 181
michel404 Avatar answered Oct 17 '25 01:10

michel404