Skip to content

Commit 86235b4

Browse files
eddumelendezkiview
andauthored
Support Docker Desktop paths for Linux and Mac (#7058)
Docker provides additional paths to support Docker Desktop. * Linux: ~/.docker/desktop/docker.sock * MacOS: ~/.docker/run/docker.sock Also, `ryuk` should use `/var/run/docker.sock`. --------- Co-authored-by: Kevin Wittek <[email protected]>
1 parent e69eeef commit 86235b4

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ public String getRemoteDockerUnixSocketPath() {
164164
return dockerSocketOverride;
165165
}
166166
}
167+
if (this.strategy != null && this.strategy.getRemoteDockerUnixSocketPath() != null) {
168+
return this.strategy.getRemoteDockerUnixSocketPath();
169+
}
167170

168171
URI dockerHost = getTransportConfig().getDockerHost();
169172
String path = "unix".equals(dockerHost.getScheme()) ? dockerHost.getRawPath() : "/var/run/docker.sock";

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ public boolean allowUserOverrides() {
102102
return true;
103103
}
104104

105+
/**
106+
/* @return the path under which the Docker unix socket is reachable relative to the Docker daemon
107+
*/
108+
public String getRemoteDockerUnixSocketPath() {
109+
return null;
110+
}
111+
105112
/**
106113
* @return highest to lowest priority value
107114
*/
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package org.testcontainers.dockerclient;
2+
3+
import lombok.Getter;
4+
import lombok.extern.slf4j.Slf4j;
5+
import org.apache.commons.lang3.SystemUtils;
6+
import org.jetbrains.annotations.Nullable;
7+
8+
import java.net.URI;
9+
import java.nio.file.Files;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
12+
import java.util.Optional;
13+
14+
/**
15+
* Look at the following paths:
16+
* <ul>
17+
* <li>Linux: ~/.docker/desktop/docker.sock</li>
18+
* <li>MacOS: ~/.docker/run/docker.sock</li>
19+
* </ul>
20+
*
21+
* @deprecated this class is used by the SPI and should not be used directly
22+
*/
23+
@Slf4j
24+
@Deprecated
25+
public class DockerDesktopClientProviderStrategy extends DockerClientProviderStrategy {
26+
27+
public static final int PRIORITY = UnixSocketClientProviderStrategy.PRIORITY - 1;
28+
29+
@Getter(lazy = true)
30+
@Nullable
31+
private final Path socketPath = resolveSocketPath();
32+
33+
private Path resolveSocketPath() {
34+
Path linuxPath = Paths.get(System.getProperty("user.home")).resolve(".docker").resolve("desktop");
35+
return tryFolder(linuxPath)
36+
.orElseGet(() -> {
37+
Path macosPath = Paths.get(System.getProperty("user.home")).resolve(".docker").resolve("run");
38+
return tryFolder(macosPath).orElse(null);
39+
});
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Rootless Docker accessed via Unix socket (" + getSocketPath() + ")";
45+
}
46+
47+
@Override
48+
public TransportConfig getTransportConfig() throws InvalidConfigurationException {
49+
return TransportConfig.builder().dockerHost(URI.create("unix://" + getSocketPath().toString())).build();
50+
}
51+
52+
@Override
53+
protected int getPriority() {
54+
return PRIORITY;
55+
}
56+
57+
@Override
58+
protected boolean isPersistable() {
59+
return false;
60+
}
61+
62+
@Override
63+
public String getRemoteDockerUnixSocketPath() {
64+
return "/var/run/docker.sock";
65+
}
66+
67+
@Override
68+
protected boolean isApplicable() {
69+
return (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) && this.socketPath != null;
70+
}
71+
72+
private Optional<Path> tryFolder(Path path) {
73+
if (!Files.exists(path)) {
74+
log.debug("'{}' does not exist.", path);
75+
return Optional.empty();
76+
}
77+
Path socketPath = path.resolve("docker.sock");
78+
if (!Files.exists(socketPath)) {
79+
log.debug("'{}' does not exist.", socketPath);
80+
return Optional.empty();
81+
}
82+
return Optional.of(socketPath);
83+
}
84+
}

core/src/main/resources/META-INF/services/org.testcontainers.dockerclient.DockerClientProviderStrategy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
44
org.testcontainers.dockerclient.DockerMachineClientProviderStrategy
55
org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy
66
org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy
7+
org.testcontainers.dockerclient.DockerDesktopClientProviderStrategy

0 commit comments

Comments
 (0)