|
| 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 | +} |
0 commit comments