Skip to content

Commit cbff9e5

Browse files
committed
Improve support for alternative container runtimes
In several cases, alternative container runtimes offers a docker compatible API and populates the docker context accordingly. However, in the current implementation of testcontainers, the context is often ignored as the `EnvironmentAndSystemPropertyClientProviderStrategy` only considers the `DOCKER_HOST` override either through testcontainers property or the environment variables. Improve the support of multiple container runtimes by honoring the current docker context. In addition, improve the detection of whether the Docker engine runs in a virtual machine without root access for the current user, so it removes the need to configure `TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE` in standard cases. This change has been tested with: - colima - rancher desktop (in non-privileged mode) - docker desktop - orbstack
1 parent cb7a793 commit cbff9e5

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

core/src/main/java/org/testcontainers/DockerClientFactory.java

+19-1
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@
3030
import org.testcontainers.utility.ResourceReaper;
3131
import org.testcontainers.utility.TestcontainersConfiguration;
3232

33+
import java.nio.file.Files;
34+
import java.io.IOException;
3335
import java.io.InputStream;
3436
import java.net.URI;
37+
import java.nio.file.Paths;
3538
import java.util.ArrayList;
3639
import java.util.Arrays;
3740
import java.util.Collections;
@@ -174,10 +177,25 @@ public String getRemoteDockerUnixSocketPath() {
174177
}
175178

176179
URI dockerHost = getTransportConfig().getDockerHost();
177-
String path = "unix".equals(dockerHost.getScheme()) ? dockerHost.getRawPath() : "/var/run/docker.sock";
180+
181+
String path = this.isRemoteDockerDaemon(dockerHost) ? "/var/run/docker.sock" : dockerHost.getRawPath();
178182
return SystemUtils.IS_OS_WINDOWS ? "/" + path : path;
179183
}
180184

185+
private Boolean isRemoteDockerDaemon(URI dockerHost){
186+
if (! "unix".equals(dockerHost.getScheme())) {
187+
return true;
188+
}
189+
// Several privileged less Docker providers exposes the Docker socket within the user's home directory, owned by the users.
190+
// This is the case for Rancher Desktop for example.
191+
// In this case, it is likely that the Docker daemon running in the virtual machine listens on /var/run/docker.sock
192+
try {
193+
return Files.getOwner(Paths.get(dockerHost.getRawPath())).getName().equals(System.getProperty("user.name"));
194+
} catch (IOException e) {
195+
return false;
196+
}
197+
}
198+
181199
/**
182200
* @return a new initialized Docker client
183201
*/

core/src/main/java/org/testcontainers/dockerclient/EnvironmentAndSystemPropertyClientProviderStrategy.java

+23-4
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,28 @@
99

1010
/**
1111
* Use environment variables and system properties (as supported by the underlying DockerClient DefaultConfigBuilder)
12-
* to try and locate a docker environment.
12+
* to try and locate a docker environment based on the currently active configuration.
1313
* <p>
14-
* Resolution order is:
14+
* Docker Host resolution order is:
1515
* <ol>
1616
* <li>DOCKER_HOST env var</li>
1717
* <li>docker.host in ~/.testcontainers.properties</li>
18+
* <li>docker host pointed by the Docker context</li>
1819
* </ol>
19-
*
20+
* <p>
21+
* Docker context resolution order is
22+
* <ol>
23+
* <li>DOCKER_CONTEXT env var</li>
24+
* <li>docker.context in ~/.testcontainers.properties</li>
25+
* <li>current docker context pointed by the Docker config</li>
26+
* </ol>
27+
* <p>
28+
* Docker config resolution order is
29+
* <ol>
30+
* <li>DOCKER_CONFIG env var</li>
31+
* <li>$HOME/.docker</li>
32+
* </ol>
33+
*
2034
* @deprecated this class is used by the SPI and should not be used directly
2135
*/
2236
@Deprecated
@@ -43,9 +57,14 @@ public EnvironmentAndSystemPropertyClientProviderStrategy() {
4357
case "auto":
4458
Optional<String> dockerHost = getSetting("docker.host");
4559
dockerHost.ifPresent(configBuilder::withDockerHost);
46-
applicable = dockerHost.isPresent();
60+
getSetting("docker.config").ifPresent(configBuilder::withDockerConfig);
61+
getSetting("docker.context").ifPresent(configBuilder::withDockerContext);
4762
getSetting("docker.tls.verify").ifPresent(configBuilder::withDockerTlsVerify);
4863
getSetting("docker.cert.path").ifPresent(configBuilder::withDockerCertPath);
64+
// We don't have access to the docker-java internals to understand whether a docker-context is defined or not.
65+
// Therefore, we assume there is always a docker context defined and try it to start with.
66+
// In case there is an error initializing the docker client with this library, it will fallback to the other resolvers.
67+
applicable = true;
4968
break;
5069
case "autoIgnoringUserProperties":
5170
applicable = configBuilder.isDockerHostSetExplicitly();

docs/supported_docker_environment/index.md

+16-20
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ These Docker environments are automatically detected and used by Testcontainers
1111

1212
It is possible to configure Testcontainers to work with alternative container runtimes.
1313
Making use of the free [Testcontainers Desktop](https://testcontainers.com/desktop/) app will take care of most of the manual configuration.
14-
When using those alternatives without Testcontainers Desktop,
14+
Although Testcontainers has a detection for most of the cases, when using those alternatives without Testcontainers Desktop,
1515
sometimes some manual configuration might be necessary
1616
(see further down for specific runtimes, or [Customizing Docker host detection](/features/configuration/#customizing-docker-host-detection) for general configuration mechanisms).
1717
Alternative container runtimes are not actively tested in the main development workflow,
@@ -21,7 +21,14 @@ please contact the Testcontainers team and other users from the Testcontainers c
2121

2222
## Colima
2323

24-
In order to run testcontainers against [colima](https://github.com/abiosoft/colima) the env vars below should be set
24+
In order to run testcontainers against [colima](https://github.com/abiosoft/colima) the standard configuration should be automatically detected. Starting colima should be enough.
25+
26+
```bash
27+
colima start --network-address
28+
docker context use colima
29+
```
30+
31+
For more complex scenarios you should configure the following environment variables:
2532

2633
```bash
2734
colima start --network-address
@@ -61,25 +68,9 @@ export TESTCONTAINERS_RYUK_DISABLED=true
6168

6269
## Rancher Desktop
6370

64-
In order to run testcontainers against [Rancher Desktop](https://rancherdesktop.io/) the env vars below should be set.
65-
66-
If you're running Rancher Desktop as an administrator in a MacOS (M1) machine:
67-
68-
Using QEMU emulation
69-
70-
```bash
71-
export TESTCONTAINERS_HOST_OVERRIDE=$(rdctl shell ip a show rd0 | awk '/inet / {sub("/.*",""); print $2}')
72-
```
73-
74-
Using VZ emulation
75-
76-
```bash
77-
export TESTCONTAINERS_HOST_OVERRIDE=$(rdctl shell ip a show vznat | awk '/inet / {sub("/.*",""); print $2}')
78-
```
79-
80-
If you're not running Rancher Desktop as an administrator in a MacOS (M1) machine:
71+
In order to run testcontainers against [Rancher Desktop](https://rancherdesktop.io/) the standard configuration should be automatically detected. Starting colima should be enough.
8172

82-
Using VZ emulation
73+
For more complex scenarios you should configure the following environment variables:
8374

8475
```bash
8576
export DOCKER_HOST=unix://$HOME/.rd/docker.sock
@@ -95,10 +86,15 @@ Testcontainers will try to connect to a Docker daemon using the following strate
9586
* `DOCKER_HOST`
9687
* `DOCKER_TLS_VERIFY`
9788
* `DOCKER_CERT_PATH`
89+
* `DOCKER_CONTEXT`
90+
* `DOCKER_CONFIG`
9891
* Defaults:
9992
* `DOCKER_HOST=https://localhost:2376`
10093
* `DOCKER_TLS_VERIFY=1`
10194
* `DOCKER_CERT_PATH=~/.docker`
95+
* `DOCKER_CONTEXT=$(docker context show)`
96+
* `DOCKER_CONFIG=~/.docker`
97+
* If the current docker context provides a working Docker environment, it will be preferred to further installation detection.
10298
* If Docker Machine is installed, the docker machine environment for the *first* machine found. Docker Machine needs to be on the PATH for this to succeed.
10399
* If you're going to run your tests inside a container, please read [Patterns for running tests inside a docker container](continuous_integration/dind_patterns.md) first.
104100

0 commit comments

Comments
 (0)