英文:
Running test with testcontainers as part of a Dockerfile
问题
FROM maven:3-jdk-11-slim
COPY pom.xml .
COPY src src
RUN mvn clean install
这意味着构建的一部分是执行单元测试。其中一些单元测试使用了一个测试容器。在我的本地机器上运行 mvn clean install
是正常的,但是运行 docker build . -t my-app
则不行,因为测试容器无法启动。
(...)
15:54:38.793 [ducttape-0] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - 正在 ping docker 守护进程...
15:54:38.794 [ducttape-0] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - 命令: org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory$1@355cb260
15:54:39.301 [ducttape-0] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - 正在 ping docker 守护进程...
15:54:39.301 [ducttape-0] DEBUG com.github.dockerjava.core.command.AbstrDockerCmd - 命令: org.testcontainers.dockerclient.transport.okhttp.OkHttpDockerCmdExecFactory$1@1c1a1359
15:54:39.469 [main] ERROR org.testcontainers.dockerclient.EnvironmentAndSystemPropertyClientProviderStrategy - 由于 org.rnorth.ducttape.TimeoutException: 在等待结果时超时,ping 配置的环境变量、系统属性和默认值失败。解析的 dockerHost=unix:///var/run/docker.sock
org.rnorth.ducttape.TimeoutException: 在等待结果时超时
(...)
我看过使用运行 docker run
来使用工作的测试容器的示例,但是如何让我的 docker build
正常工作呢?
非常感谢您的帮助。
英文:
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.
答案1
得分: 2
供将来参考:我相信这是不可能的。
docker run
允许你挂载Docker套接字(从而访问宿主机的Docker守护程序)使用 -v /var/run/docker.sock:/var/run/docker.sock
。
docker build
不支持这样的参数。
我的解决方法将会修改我的Dockerfile为
RUN mvn clean install -Dmaven.test.skip=true
并单独运行单元测试。
英文:
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.
答案2
得分: 0
在 Docker 文件中,你需要安装 Docker 以运行 TestContainer。
我从我所在工作场所的现有项目中学到了这一点。希望这能给你一些解决问题的提示。
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
在 docker-compose 文件中,
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /mnt/cachedata/.m2:/root/.m2
- .:/app
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论