Skip to content

Commit b2950ae

Browse files
gesellixraviagarwal7
authored andcommitted
Fix bind mounts for ResourceReaper/ryuk and ContainerisedDockerCompose on macOS (testcontainers#3159)
Relates to testcontainers#545 Relates to testcontainers#2998
1 parent 4853406 commit b2950ae

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
import lombok.SneakyThrows;
1818
import lombok.Synchronized;
1919
import lombok.extern.slf4j.Slf4j;
20+
import org.apache.commons.lang.StringUtils;
21+
import org.apache.commons.lang.SystemUtils;
2022
import org.testcontainers.dockerclient.DockerClientProviderStrategy;
2123
import org.testcontainers.dockerclient.DockerMachineClientProviderStrategy;
24+
import com.github.dockerjava.core.DockerClientConfig;
2225
import org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback;
2326
import org.testcontainers.utility.ComparableVersion;
2427
import org.testcontainers.utility.MountableFile;
@@ -35,6 +38,7 @@
3538
import java.util.UUID;
3639
import java.util.function.BiFunction;
3740
import java.util.function.Consumer;
41+
import java.net.URI;
3842

3943
/**
4044
* Singleton class that provides initialized Docker clients.
@@ -127,6 +131,27 @@ private DockerClientProviderStrategy getOrInitializeStrategy() {
127131
return strategy;
128132
}
129133

134+
@UnstableAPI
135+
public DockerClientConfig getDockerCLientConfig() {
136+
return getOrInitializeStrategy().getDockerClientConfig();
137+
}
138+
139+
@UnstableAPI
140+
public String getRemoteDockerUnixSocketPath() {
141+
String dockerSocketOverride = System.getenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE");
142+
if (!StringUtils.isBlank(dockerSocketOverride)) {
143+
return dockerSocketOverride;
144+
}
145+
146+
URI dockerHost = getDockerCLientConfig().getDockerHost();
147+
String path = "unix".equals(dockerHost.getScheme())
148+
? dockerHost.getRawPath()
149+
: "/var/run/docker.sock";
150+
return SystemUtils.IS_OS_WINDOWS
151+
? "/" + path
152+
: path;
153+
}
154+
130155
/**
131156
*
132157
* @return a new initialized Docker client

core/src/main/java/org/testcontainers/containers/DockerComposeContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ public ContainerisedDockerCompose(List<File> composeFiles, String identifier) {
592592
// as the docker daemon, just mapping the docker control socket is OK.
593593
// As there seems to be a problem with mapping to the /var/run directory in certain environments (e.g. CircleCI)
594594
// we map the socket file outside of /var/run, as just /docker.sock
595-
addFileSystemBind(getDockerSocketHostPath(), "/docker.sock", READ_WRITE);
595+
addFileSystemBind(DockerClientFactory.instance().getRemoteDockerUnixSocketPath(), "/docker.sock", READ_WRITE);
596596
addEnv("DOCKER_HOST", "unix:///docker.sock");
597597
setStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy());
598598
setWorkingDirectory(containerPwd);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ protected int getPriority() {
7272
return 0;
7373
}
7474

75+
public DockerClientConfig getDockerClientConfig() {
76+
return config;
77+
}
78+
7579
/**
7680
* Determine the right DockerClientConfig to use for building clients by trial-and-error.
7781
*

core/src/main/java/org/testcontainers/utility/ResourceReaper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ public static String start(String hostIpAddress, DockerClient client) {
8181
DockerClientFactory.instance().checkAndPullImage(client, ryukImage);
8282

8383
List<Bind> binds = new ArrayList<>();
84-
binds.add(new Bind("//var/run/docker.sock", new Volume("/var/run/docker.sock")));
84+
binds.add(new Bind(DockerClientFactory.instance().getRemoteDockerUnixSocketPath(), new Volume("/var/run/docker.sock")));
8585

8686
String ryukContainerId = client.createContainerCmd(ryukImage)
8787
.withHostConfig(new HostConfig().withAutoRemove(true))

modules/localstack/src/main/java/org/testcontainers/containers/localstack/LocalStackContainer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.List;
2222
import java.util.stream.Collectors;
23+
import org.testcontainers.DockerClientFactory;
2324

2425
/**
2526
* <p>Container for Atlassian Labs Localstack, 'A fully functional local AWS cloud stack'.</p>
@@ -43,7 +44,7 @@ public LocalStackContainer() {
4344
public LocalStackContainer(String version) {
4445
super(TestcontainersConfiguration.getInstance().getLocalStackImage() + ":" + version);
4546

46-
withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock");
47+
withFileSystemBind(DockerClientFactory.instance().getRemoteDockerUnixSocketPath(), "/var/run/docker.sock");
4748
waitingFor(Wait.forLogMessage(".*Ready\\.\n", 1));
4849
}
4950

0 commit comments

Comments
 (0)