Skip to content

Commit

Permalink
Fix bind mounts for ResourceReaper/ryuk and ContainerisedDockerCompos…
Browse files Browse the repository at this point in the history
…e on macOS (testcontainers#3159)

Relates to testcontainers#545
Relates to testcontainers#2998
  • Loading branch information
gesellix authored and raviagarwal7 committed Nov 25, 2020
1 parent 4853406 commit b2950ae
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
25 changes: 25 additions & 0 deletions core/src/main/java/org/testcontainers/DockerClientFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
import lombok.SneakyThrows;
import lombok.Synchronized;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.SystemUtils;
import org.testcontainers.dockerclient.DockerClientProviderStrategy;
import org.testcontainers.dockerclient.DockerMachineClientProviderStrategy;
import com.github.dockerjava.core.DockerClientConfig;
import org.testcontainers.images.TimeLimitedLoggedPullImageResultCallback;
import org.testcontainers.utility.ComparableVersion;
import org.testcontainers.utility.MountableFile;
Expand All @@ -35,6 +38,7 @@
import java.util.UUID;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.net.URI;

/**
* Singleton class that provides initialized Docker clients.
Expand Down Expand Up @@ -127,6 +131,27 @@ private DockerClientProviderStrategy getOrInitializeStrategy() {
return strategy;
}

@UnstableAPI
public DockerClientConfig getDockerCLientConfig() {
return getOrInitializeStrategy().getDockerClientConfig();
}

@UnstableAPI
public String getRemoteDockerUnixSocketPath() {
String dockerSocketOverride = System.getenv("TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE");
if (!StringUtils.isBlank(dockerSocketOverride)) {
return dockerSocketOverride;
}

URI dockerHost = getDockerCLientConfig().getDockerHost();
String path = "unix".equals(dockerHost.getScheme())
? dockerHost.getRawPath()
: "/var/run/docker.sock";
return SystemUtils.IS_OS_WINDOWS
? "/" + path
: path;
}

/**
*
* @return a new initialized Docker client
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ public ContainerisedDockerCompose(List<File> composeFiles, String identifier) {
// as the docker daemon, just mapping the docker control socket is OK.
// As there seems to be a problem with mapping to the /var/run directory in certain environments (e.g. CircleCI)
// we map the socket file outside of /var/run, as just /docker.sock
addFileSystemBind(getDockerSocketHostPath(), "/docker.sock", READ_WRITE);
addFileSystemBind(DockerClientFactory.instance().getRemoteDockerUnixSocketPath(), "/docker.sock", READ_WRITE);
addEnv("DOCKER_HOST", "unix:///docker.sock");
setStartupCheckStrategy(new IndefiniteWaitOneShotStartupCheckStrategy());
setWorkingDirectory(containerPwd);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ protected int getPriority() {
return 0;
}

public DockerClientConfig getDockerClientConfig() {
return config;
}

/**
* Determine the right DockerClientConfig to use for building clients by trial-and-error.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public static String start(String hostIpAddress, DockerClient client) {
DockerClientFactory.instance().checkAndPullImage(client, ryukImage);

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

String ryukContainerId = client.createContainerCmd(ryukImage)
.withHostConfig(new HostConfig().withAutoRemove(true))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.testcontainers.DockerClientFactory;

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

withFileSystemBind("//var/run/docker.sock", "/var/run/docker.sock");
withFileSystemBind(DockerClientFactory.instance().getRemoteDockerUnixSocketPath(), "/var/run/docker.sock");
waitingFor(Wait.forLogMessage(".*Ready\\.\n", 1));
}

Expand Down

0 comments on commit b2950ae

Please sign in to comment.