Testcontainers; 在Docker内部运行@Testcontainers测试 [在Docker内部运行Docker]

huangapple go评论60阅读模式
英文:

Testcontainers ; Running @Testcontainers Tests inside docker [Running Docker inside Docker]

问题

You can run @Testcontainers based test cases inside a Docker container by ensuring that Docker is installed and accessible within the container. In your Dockerfile, you should make sure to install Docker properly. However, it seems like you're encountering an issue.

你可以通过确保在 Docker 容器内正确安装和访问 Docker 来运行基于 @Testcontainers 的测试用例。在你的 Dockerfile 中,确保正确安装 Docker。不过,看起来你遇到了一个问题。

The error you mentioned indicates that @Testcontainers is having trouble connecting to Docker. You should consider a few things:

你提到的错误表明 @Testcontainers 在连接到 Docker 方面出现了问题。你应该考虑以下几点:

  1. Ensure that Docker is properly installed and running in your Docker image. You've installed it using apt-get install docker.io, but make sure it's started and accessible.

确保 Docker 在你的 Docker 镜像中正确安装并运行。你已经使用 apt-get install docker.io 安装了它,但确保它已经启动并可访问。

  1. Verify that the Docker socket (/var/run/docker.sock) is accessible within the container. You've mounted it as a volume, but ensure there are no permission issues.

验证容器内是否可以访问 Docker 套接字 (/var/run/docker.sock)。你已将其挂载为卷,但确保没有权限问题。

  1. Make sure the Docker daemon is running when you build the image, as it's required for @Testcontainers to interact with Docker.

确保在构建镜像时 Docker 守护程序正在运行,因为 @Testcontainers 需要与 Docker 进行交互。

  1. Check your Docker image and build process to ensure that Docker and the required dependencies are correctly configured.

检查你的 Docker 镜像和构建过程,确保 Docker 和所需的依赖项已正确配置。

You may need to adjust your Docker image and Dockerfile to resolve this issue. Once Docker is correctly set up within the container, @Testcontainers should be able to interact with it, allowing you to run your component-level integration tests during the Maven build phase using the Dockerfile.

你可能需要调整你的 Docker 镜像和 Dockerfile 以解决这个问题。一旦在容器内正确设置了 Docker,@Testcontainers 应该能够与其交互,允许你使用 Dockerfile 在 Maven 构建阶段运行你的组件级集成测试。

英文:

How To Run @Testcontainers based test cases inside the docker container ?

I have Simple Spring Boot App that has Integration Test (Component level) that are interacting with containers using Testcontainers. Test cases are ruining fine from outside container(Local machine).

We are running everything in containers and build is running on docker jenkins image.
Docker file is creating jar and then image. @Testcontainers is not able to find docker installed.
Below is my docker file.

FROM maven:3.6-jdk-11-openj9
VOLUME ["/var/run/docker.sock"]
RUN apt-get update
RUN apt-get -y install docker.io
COPY . /usr/src/app
WORKDIR /usr/src/app
RUN mvn -Dmaven.repo.local=/root/m2 --batch-mode -f pom.xml clean package
EXPOSE 8080
CMD ["/bin/bash"]

While running build i am getting below below 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

Whats the best way to handle this case ? I want to run my component level integration test during mvn build phase using docker file.

below reference did not helped me.
https://www.testcontainers.org/supported_docker_environment/continuous_integration/dind_patterns/

答案1

得分: 3

This is not complete answer but you should enable access to a docker daemon from inside your container. Installing Docker and running it's daemon inside your container is complicated so not recommended. Docker can be controlled via Unix socket or over TCP (I assume the host system is a Linux).

How Test containers look for Docker:
By default it tries to connect to Unix socket /var/run/docker.sock. You can specify other socket path or TCP address by setting environment variables (DOCKER_HOST).

How docker exposes it's control API:
By default via Unix socket /var/run/docker.sock (on your host). You can expose docker API elsewhere by adding following parameters to docker start command (the location of command launching your docker is system dependent): -H fd:// -H tcp://127.0.0.1:2376. Note that you can specify more than one option. -H fd:// - is the default, tcp://127.0.0.1:2376 - tells Docker to listen on localhost port 2376.

How to make Docker available inside your container ("Docker in Docker"): If you enabled network access - no need to do additional config except pointing Testcontaners to it as mentioned above. If you want to use default Unix socket then you can map (mount) it into container via volume option:

docker run --volume /var/run/docker.sock:/var/run/docker.sock your-image-id-here

The remaining problem is that mounted docker.sock inside container will also be owned by root:docker (with same uid:gid as on your host system) so Testcontainers would work only if your container user can connect to that socket. That is user of running process is root or happen to have exact same group id inside your container as group id of docker on your host system.
I do not know yet a good solution to this one, so for starters you can run your tests inside container as root, or hard-code container's user group-id to match your host's docker group id.

英文:

This is not complete answer but you should enable access to a docker daemon from inside your container. Installing Docker and running it's daemon inside your container is complicated so not recommended. Docker can be controlled via Unix socket or over TCP (I assume the host system is a Linux).

How Test containers look for Docker:
By default it tries to connect to Unix socket /var/run/docker.sock. You can specify other socket path or TCP address by setting environment variables (DOCKER_HOST).

How docker exposes it's control API:
By default via Unix socket /var/run/docker.sock (on your host). You can expose docker API elsewhere by adding following parameters to docker start command (the location of command launching your docker is system dependent): -H fd:// -H tcp://127.0.0.1:2376. Note that you can specify more than one option. -H fd:// - is the default, tcp://127.0.0.1:2376 - tells Docker to listen on localhost port 2376.

How to make Docker available inside your container ("Docker in Docker"): If you enabled network access - no need to do additional config except pointing Testcontaners to it as mentioned above. If you want to use default Unix socket then you can map (mount) it into container via volume option:

docker run --volume /var/run/docker.sock:/var/run/docker.sock your-image-id-here

The remaining problem is that mounted docker.sock inside container will also be owned by root:docker (with same uid:gid as on your host system) so Testcontainers would work only if your container user can connect to that socket. That is user of running process is root or happen to have exact same group id inside your container as group id of docker on your host system.
I do not know yet a good solution to this one, so for starters you can run your tests inside container as root, or hard-code container's user group-id to match your host's docker group id.

huangapple
  • 本文由 发表于 2020年1月3日 21:58:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定