Skip to content

Commit

Permalink
Support Docker Desktop paths for Linux and Mac (#7058)
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
eddumelendez and kiview authored May 30, 2023
1 parent e69eeef commit 86235b4
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ public String getRemoteDockerUnixSocketPath() {
return dockerSocketOverride;
}
}
if (this.strategy != null && this.strategy.getRemoteDockerUnixSocketPath() != null) {
return this.strategy.getRemoteDockerUnixSocketPath();
}

URI dockerHost = getTransportConfig().getDockerHost();
String path = "unix".equals(dockerHost.getScheme()) ? dockerHost.getRawPath() : "/var/run/docker.sock";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,13 @@ public boolean allowUserOverrides() {
return true;
}

/**
/* @return the path under which the Docker unix socket is reachable relative to the Docker daemon
*/
public String getRemoteDockerUnixSocketPath() {
return null;
}

/**
* @return highest to lowest priority value
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package org.testcontainers.dockerclient;

import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.SystemUtils;
import org.jetbrains.annotations.Nullable;

import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;

/**
* Look at the following paths:
* <ul>
* <li>Linux: ~/.docker/desktop/docker.sock</li>
* <li>MacOS: ~/.docker/run/docker.sock</li>
* </ul>
*
* @deprecated this class is used by the SPI and should not be used directly
*/
@Slf4j
@Deprecated
public class DockerDesktopClientProviderStrategy extends DockerClientProviderStrategy {

public static final int PRIORITY = UnixSocketClientProviderStrategy.PRIORITY - 1;

@Getter(lazy = true)
@Nullable
private final Path socketPath = resolveSocketPath();

private Path resolveSocketPath() {
Path linuxPath = Paths.get(System.getProperty("user.home")).resolve(".docker").resolve("desktop");
return tryFolder(linuxPath)
.orElseGet(() -> {
Path macosPath = Paths.get(System.getProperty("user.home")).resolve(".docker").resolve("run");
return tryFolder(macosPath).orElse(null);
});
}

@Override
public String getDescription() {
return "Rootless Docker accessed via Unix socket (" + getSocketPath() + ")";
}

@Override
public TransportConfig getTransportConfig() throws InvalidConfigurationException {
return TransportConfig.builder().dockerHost(URI.create("unix://" + getSocketPath().toString())).build();
}

@Override
protected int getPriority() {
return PRIORITY;
}

@Override
protected boolean isPersistable() {
return false;
}

@Override
public String getRemoteDockerUnixSocketPath() {
return "/var/run/docker.sock";
}

@Override
protected boolean isApplicable() {
return (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) && this.socketPath != null;
}

private Optional<Path> tryFolder(Path path) {
if (!Files.exists(path)) {
log.debug("'{}' does not exist.", path);
return Optional.empty();
}
Path socketPath = path.resolve("docker.sock");
if (!Files.exists(socketPath)) {
log.debug("'{}' does not exist.", socketPath);
return Optional.empty();
}
return Optional.of(socketPath);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ org.testcontainers.dockerclient.UnixSocketClientProviderStrategy
org.testcontainers.dockerclient.DockerMachineClientProviderStrategy
org.testcontainers.dockerclient.NpipeSocketClientProviderStrategy
org.testcontainers.dockerclient.RootlessDockerClientProviderStrategy
org.testcontainers.dockerclient.DockerDesktopClientProviderStrategy

0 comments on commit 86235b4

Please sign in to comment.