Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Docker Desktop paths for Linux and Mac #7058

Merged
merged 5 commits into from
May 30, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,10 @@ public boolean allowUserOverrides() {
return true;
}

public String getRemoteDockerUnixSocketPath() {
eddumelendez marked this conversation as resolved.
Show resolved Hide resolved
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