Skip to content

Commit ec63953

Browse files
Fix UnixSocketClientProviderStrategy ignoring DOCKER_HOST env var
1 parent f2a6fe9 commit ec63953

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* @deprecated this class is used by the SPI and should not be used directly
1414
*/
1515
@Deprecated
16-
public final class UnixSocketClientProviderStrategy extends DockerClientProviderStrategy {
16+
public class UnixSocketClientProviderStrategy extends DockerClientProviderStrategy {
1717

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

@@ -25,12 +25,19 @@ public final class UnixSocketClientProviderStrategy extends DockerClientProvider
2525

2626
@Override
2727
public TransportConfig getTransportConfig() throws InvalidConfigurationException {
28-
Path dockerSocketFile = Paths.get(DOCKER_SOCK_PATH);
28+
String socketPath = DOCKER_SOCK_PATH;
29+
String envPath = getDockerHostEnv();
30+
31+
if (envPath != null && envPath.startsWith("unix://")) {
32+
socketPath = envPath.replace("unix://", "");
33+
}
34+
35+
Path dockerSocketFile = Paths.get(socketPath);
2936
Integer mode;
3037
try {
3138
mode = (Integer) Files.getAttribute(dockerSocketFile, "unix:mode");
3239
} catch (IOException e) {
33-
throw new InvalidConfigurationException("Could not find unix domain socket", e);
40+
throw new InvalidConfigurationException("Could not find unix domain socket: " + socketPath, e);
3441
}
3542

3643
if ((mode & 0xc000) != SOCKET_FILE_MODE_MASK) {
@@ -39,7 +46,14 @@ public TransportConfig getTransportConfig() throws InvalidConfigurationException
3946
);
4047
}
4148

42-
return TransportConfig.builder().dockerHost(URI.create(SOCKET_LOCATION)).build();
49+
return TransportConfig.builder().dockerHost(URI.create("unix://" + socketPath)).build();
50+
}
51+
52+
/**
53+
* Visible for testing.
54+
*/
55+
protected String getDockerHostEnv() {
56+
return System.getenv("DOCKER_HOST");
4357
}
4458

4559
@Override
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.testcontainers.dockerclient;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static org.assertj.core.api.Assertions.assertThat;
6+
import static org.assertj.core.api.Assertions.catchThrowable;
7+
8+
class UnixSocketClientProviderStrategyTest {
9+
10+
@Test
11+
void shouldRespectDockerHostEnvVar() {
12+
String customSocketPath = "/tmp/test-socket-path";
13+
String envVarValue = "unix://" + customSocketPath;
14+
15+
UnixSocketClientProviderStrategy strategy = new UnixSocketClientProviderStrategy() {
16+
@Override
17+
protected String getDockerHostEnv() {
18+
return envVarValue;
19+
}
20+
};
21+
22+
Throwable thrown = catchThrowable(strategy::getTransportConfig);
23+
24+
assertThat(thrown)
25+
.as("Strategy should use path from DOCKER_HOST env var")
26+
.hasMessageContaining(customSocketPath);
27+
}
28+
}

0 commit comments

Comments
 (0)