docs/supported_docker_environment/continuous_integration/dind_patterns.md
Testcontainers itself can be used from inside a container. This is very useful for different CI scenarios like running everything in containers on Jenkins, or Docker-based CI tools such as Drone.
Testcontainers will automatically detect if it's inside a container and instead of "localhost" will use the default gateway's IP.
However, additional configuration is required if you use volume mapping. The following points need to be considered:
If you run the tests with just docker run ... then make sure you add -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock to the command, so it will look like this:
$ tree .
.
├── pom.xml
└── src
└── test
└── java
└── MyTestWithTestcontainers.java
$ docker run -it --rm -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock maven:3 mvn test
Where:
-v $PWD:$PWD will add your current directory as a volume inside the container-w $PWD will set the current directory to this volume-v /var/run/docker.sock:/var/run/docker.sock will map the Docker socket!!! note
If you are using Docker Desktop, you need to configure the TESTCONTAINERS_HOST_OVERRIDE environment variable to use the special DNS name
host.docker.internal for accessing the host from within a container, which is provided by Docker Desktop:
-e TESTCONTAINERS_HOST_OVERRIDE=host.docker.internal
The same can be achieved with Docker Compose:
tests:
image: maven:3
stop_signal: SIGKILL
stdin_open: true
tty: true
working_dir: $PWD
volumes:
- $PWD:$PWD
- /var/run/docker.sock:/var/run/docker.sock
# Maven cache (optional)
- ~/.m2:/root/.m2
command: mvn test
While Docker-in-Docker (DinD) is generally considered an instrument of last resort, it is necessary for some CI environments.
Drone CI is one such example. Testcontainers has a Docker-in-Docker plugin (build image) for use with Drone, which could be used as inspiration for setting up other similar testing using DinD.