3

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.

michel404
  • 473
  • 4
  • 12
  • So you run your build inside a docker container and running inside that container Testcontainers ? Which means Docker in Docker ...? Why running your build inside a container any special requirements? – khmarbaise Apr 03 '20 at 16:40
  • I use a multi stage docker file to build te project. Building the project inside a container, to be able to make a clean build in any environment. That is why. Controlling the host's Docker engine from inside the maven/java-container is fine. Using Docker in Docker (having a separate Docker engine inside the maven/java-container) is also fine. As long as I get it to work... – michel404 Apr 06 '20 at 09:53

2 Answers2

2

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.

michel404
  • 473
  • 4
  • 12
0

In the docker file, you need to have docker installed to run TestContainer. I have learned this from one of the existing projects in my workplace. hopefully, it can give you some hints to solve your problem.

RUN curl -fsSL get.docker.com | sh

RUN curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose && \
chmod +x /usr/local/bin/docker-compose

In the docker-compose file,

volumes:
  - /var/run/docker.sock:/var/run/docker.sock
  - /mnt/cachedata/.m2:/root/.m2
  - .:/app
Andrew
  • 784
  • 9
  • 15