Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* @deprecated this class is used by the SPI and should not be used directly
*/
@Deprecated
public final class UnixSocketClientProviderStrategy extends DockerClientProviderStrategy {
public class UnixSocketClientProviderStrategy extends DockerClientProviderStrategy {

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

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

@Override
public TransportConfig getTransportConfig() throws InvalidConfigurationException {
Path dockerSocketFile = Paths.get(DOCKER_SOCK_PATH);
String socketPath = DOCKER_SOCK_PATH;
String envPath = getDockerHostEnv();

if (envPath != null && envPath.startsWith("unix://")) {
socketPath = envPath.replace("unix://", "");
}

Path dockerSocketFile = Paths.get(socketPath);
Integer mode;
try {
mode = (Integer) Files.getAttribute(dockerSocketFile, "unix:mode");
} catch (IOException e) {
throw new InvalidConfigurationException("Could not find unix domain socket", e);
throw new InvalidConfigurationException("Could not find unix domain socket: " + socketPath, e);
}

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

return TransportConfig.builder().dockerHost(URI.create(SOCKET_LOCATION)).build();
return TransportConfig.builder().dockerHost(URI.create("unix://" + socketPath)).build();
}

/**
* Visible for testing.
*/
protected String getDockerHostEnv() {
return System.getenv("DOCKER_HOST");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.testcontainers.dockerclient;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;

class UnixSocketClientProviderStrategyTest {

@Test
void shouldRespectDockerHostEnvVar() {
String customSocketPath = "/tmp/test-socket-path";
String envVarValue = "unix://" + customSocketPath;

UnixSocketClientProviderStrategy strategy = new UnixSocketClientProviderStrategy() {
@Override
protected String getDockerHostEnv() {
return envVarValue;
}
};

Throwable thrown = catchThrowable(strategy::getTransportConfig);

assertThat(thrown)
.as("Strategy should use path from DOCKER_HOST env var")
.hasMessageContaining(customSocketPath);
}
}