-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Docker Desktop paths for Linux and Mac (#7058)
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
1 parent
e69eeef
commit 86235b4
Showing
4 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
core/src/main/java/org/testcontainers/dockerclient/DockerDesktopClientProviderStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters