Skip to content

Commit b6e89f2

Browse files
committed
Support Colima out of the box
This is related to testcontainers#5837, though I'm not sure if it fully fixes it. This makes two changes that make testcontainers work with Colima with no configuration for me: - Since v3.3 docker-java supports looking in the context file to discover the Docker host, but EnvironmentAndSystemPropertyClientProviderStrategy ignored that. This change makes it so that if `DefaultDockerClientConfig.Builder.build` ever returns a non-default docker-host, it is used. - With Colima, the path that you use to refer to the docker socket when setting up container volumes is different from the path you use to directly connect to Docker, because within the VM, you just use the standard `/var/run/docker.sock`. Add some hardcoded logic to always use `/var/run/docker.sock` if the chosen socket appears to be from Colima; you can always override with $TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE.
1 parent 2525748 commit b6e89f2

5 files changed

Lines changed: 61 additions & 40 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ public TransportConfig getTransportConfig() {
156156
return getOrInitializeStrategy().getTransportConfig();
157157
}
158158

159+
/**
160+
* Returns the path to the Docker socket, suitable for mapping into the container itself via a volume.
161+
*
162+
* In some special cases, this might not be the same path that this library uses to talk to Docker;
163+
* for example, if Docker is running inside a VM, this path is the path to the Docker socket within
164+
* the VM.
165+
*/
159166
@UnstableAPI
160167
public String getRemoteDockerUnixSocketPath() {
161168
String dockerSocketOverride = System.getenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE");
@@ -164,6 +171,16 @@ public String getRemoteDockerUnixSocketPath() {
164171
}
165172

166173
URI dockerHost = getTransportConfig().getDockerHost();
174+
175+
// Special case: it looks like we are running Colima on a Mac. Colima runs a VM. Inside
176+
// the VM, you need to access Docker via /var/run/docker.sock; on the Mac itself, you
177+
// need to access it via something like ~/.colima/default/docker.sock, which can be found
178+
// via the current Docker context. To make this just work by default, if it looks like we're
179+
// running Colima, return the path that works inside the VM.
180+
if (dockerHost.getRawPath().contains("/.colima/")) {
181+
return "/var/run/docker.sock";
182+
}
183+
167184
String path = "unix".equals(dockerHost.getScheme()) ? dockerHost.getRawPath() : "/var/run/docker.sock";
168185
return SystemUtils.IS_OS_WINDOWS ? "/" + path : path;
169186
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public EnvironmentAndSystemPropertyClientProviderStrategy() {
3535
}
3636

3737
EnvironmentAndSystemPropertyClientProviderStrategy(DefaultDockerClientConfig.Builder configBuilder) {
38+
boolean applicable = false;
3839
String dockerConfigSource = TestcontainersConfiguration
3940
.getInstance()
4041
.getEnvVarOrProperty("dockerconfig.source", "auto");
@@ -55,6 +56,18 @@ public EnvironmentAndSystemPropertyClientProviderStrategy() {
5556
}
5657

5758
dockerClientConfig = configBuilder.build();
59+
// DefaultDockerClientConfig.Builder is often able to read a docker host from the current context file.
60+
// If it does not find such a thing and there's also no explicit host setting, it will set the host to the default
61+
// Unix or Windows socket location. So if we find something other than those defaults, we'll use it; otherwise,
62+
// we'll let other strategies kick in (including UnixSocketClientProviderStrategy and NpipeSocketClientProviderStrategy
63+
// themselves).
64+
applicable =
65+
applicable ||
66+
(
67+
!dockerClientConfig.getDockerHost().equals(UnixSocketClientProviderStrategy.SOCKET_LOCATION) &&
68+
!dockerClientConfig.getDockerHost().equals(NpipeSocketClientProviderStrategy.SOCKET_LOCATION)
69+
);
70+
this.applicable = applicable;
5871
}
5972

6073
private Optional<String> getSetting(final String name) {

‎core/src/main/java/org/testcontainers/dockerclient/NpipeSocketClientProviderStrategy.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public final class NpipeSocketClientProviderStrategy extends DockerClientProvide
1313

1414
protected static final String DOCKER_SOCK_PATH = "//./pipe/docker_engine";
1515

16-
private static final String SOCKET_LOCATION = "npipe://" + DOCKER_SOCK_PATH;
16+
protected static final String SOCKET_LOCATION = "npipe://" + DOCKER_SOCK_PATH;
1717

1818
public static final int PRIORITY = EnvironmentAndSystemPropertyClientProviderStrategy.PRIORITY - 20;
1919

‎core/src/main/java/org/testcontainers/dockerclient/UnixSocketClientProviderStrategy.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public final class UnixSocketClientProviderStrategy extends DockerClientProvider
1717

1818
protected static final String DOCKER_SOCK_PATH = "/var/run/docker.sock";
1919

20-
private static final String SOCKET_LOCATION = "unix://" + DOCKER_SOCK_PATH;
20+
protected static final String SOCKET_LOCATION = "unix://" + DOCKER_SOCK_PATH;
2121

2222
private static final int SOCKET_FILE_MODE_MASK = 0xc000;
2323

‎docs/supported_docker_environment/index.md‎

Lines changed: 29 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,46 @@
22

33
## Overview
44

5-
Testcontainers requires a Docker-API compatible container runtime.
6-
During development, Testcontainers is actively tested against recent versions of Docker on Linux, as well as against Docker Desktop on Mac and Windows.
5+
Testcontainers requires a Docker-API compatible container runtime.
6+
During development, Testcontainers is actively tested against recent versions of Docker on Linux, as well as against Docker Desktop on Mac and Windows.
77
These Docker environments are automatically detected and used by Testcontainers without any additional configuration being necessary.
88

9-
It is possible to configure Testcontainers to work for other Docker setups, such as a remote Docker host or Docker alternatives.
10-
However, these are not actively tested in the main development workflow, so not all Testcontainers features might be available and additional manual configuration might be necessary.
11-
If you have further questions about configuration details for your setup or whether it supports running Testcontainers-based tests,
9+
It is possible to configure Testcontainers to work for other Docker setups, such as a remote Docker host or Docker alternatives.
10+
However, these are not actively tested in the main development workflow, so not all Testcontainers features might be available and additional manual configuration might be necessary.
11+
If you have further questions about configuration details for your setup or whether it supports running Testcontainers-based tests,
1212
please contact the Testcontainers team and other users from the Testcontainers community on [Slack](https://slack.testcontainers.org/).
1313

14-
| Host Operating System / Environment | Minimum recommended docker versions | Known issues / tips |
15-
|-------------------------------------|-----------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
16-
| Linux - general | Docker v17.09 | After docker installation, follow [post-installation steps](https://docs.docker.com/engine/install/linux-postinstall/). |
17-
| Linux - CircleCI (LXC driver) | Docker v17.09 | The `exec` feature is not compatible with CircleCI. See CircleCI configuration [example](./continuous_integration/circle_ci.md) |
18-
| Linux - within a Docker container | Docker v17.09 | See [Running inside Docker](continuous_integration/dind_patterns.md) for Docker-in-Docker and Docker wormhole patterns |
19-
| Mac OS X - Docker Toolbox | Docker Machine v0.8.0 | |
20-
| Mac OS X - Docker for Mac | v17.09 | Starting 4.13, run `sudo ln -s $HOME/.docker/run/docker.sock /var/run/docker.sock`<br>Support is best-efforts at present<br>`getTestHostIpAddress()` is [not currently supported](https://github.com/testcontainers/testcontainers-java/issues/166) due to limitations in Docker for Mac. |
21-
| Windows - Docker Toolbox | | *Support is limited at present and this is not currently tested on a regular basis*. |
22-
| Windows - Docker for Windows | | *Support is best-efforts at present.* Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md) |
23-
| Windows - Windows Subsystem for Linux (WSL) | Docker v17.09 | *Support is best-efforts at present.* Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md). |
24-
25-
## Using Colima?
26-
27-
In order to run testcontainers against [colima](https://github.com/abiosoft/colima) the env vars bellow should be set
28-
29-
```bash
30-
export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock
31-
export DOCKER_HOST="unix://${HOME}/.colima/docker.sock"
32-
```
14+
| Host Operating System / Environment | Minimum recommended docker versions | Known issues / tips |
15+
| ------------------------------------------- | ----------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
16+
| Linux - general | Docker v17.09 | After docker installation, follow [post-installation steps](https://docs.docker.com/engine/install/linux-postinstall/). |
17+
| Linux - CircleCI (LXC driver) | Docker v17.09 | The `exec` feature is not compatible with CircleCI. See CircleCI configuration [example](./continuous_integration/circle_ci.md) |
18+
| Linux - within a Docker container | Docker v17.09 | See [Running inside Docker](continuous_integration/dind_patterns.md) for Docker-in-Docker and Docker wormhole patterns |
19+
| Mac OS X - Docker Toolbox | Docker Machine v0.8.0 | |
20+
| Mac OS X - Docker for Mac | v17.09 | Starting 4.13, run `sudo ln -s $HOME/.docker/run/docker.sock /var/run/docker.sock`<br>Support is best-efforts at present<br>`getTestHostIpAddress()` is [not currently supported](https://github.com/testcontainers/testcontainers-java/issues/166) due to limitations in Docker for Mac. |
21+
| Windows - Docker Toolbox | | _Support is limited at present and this is not currently tested on a regular basis_. |
22+
| Windows - Docker for Windows | | _Support is best-efforts at present._ Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md) |
23+
| Windows - Windows Subsystem for Linux (WSL) | Docker v17.09 | _Support is best-efforts at present._ Only Linux Containers (LCOW) are supported at the moment. See [Windows Support](windows.md). |
3324

3425
## Docker environment discovery
3526

3627
Testcontainers will try to connect to a Docker daemon using the following strategies in order:
3728

38-
* Environment variables:
39-
* `DOCKER_HOST`
40-
* `DOCKER_TLS_VERIFY`
41-
* `DOCKER_CERT_PATH`
42-
* Defaults:
43-
* `DOCKER_HOST=https://localhost:2376`
44-
* `DOCKER_TLS_VERIFY=1`
45-
* `DOCKER_CERT_PATH=~/.docker`
46-
* 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.
47-
* 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.
29+
- Environment variables:
30+
- `DOCKER_HOST`
31+
- `DOCKER_TLS_VERIFY`
32+
- `DOCKER_CERT_PATH`
33+
- Defaults:
34+
- `DOCKER_HOST=https://localhost:2376`
35+
- `DOCKER_TLS_VERIFY=1`
36+
- `DOCKER_CERT_PATH=~/.docker`
37+
- 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.
38+
- 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.
4839

4940
## Docker registry authentication
5041

5142
Testcontainers will try to authenticate to registries with supplied config using the following strategies in order:
5243

53-
* Environment variables:
54-
* `DOCKER_AUTH_CONFIG`
55-
* Docker config
56-
* At location specified in `DOCKER_CONFIG` or at `{HOME}/.docker/config.json`
44+
- Environment variables:
45+
- `DOCKER_AUTH_CONFIG`
46+
- Docker config
47+
- At location specified in `DOCKER_CONFIG` or at `{HOME}/.docker/config.json`

0 commit comments

Comments
 (0)