mirror of
https://github.com/floci-io/floci-cli.git
synced 2026-11-22 21:50:47 -05:00
feat: honor DOCKER_HOST for Docker daemon resolution (#4)
Resolve the Docker endpoint from DOCKER_HOST (with the legacy DOCKER_SOCK override and an OS default fallback) so Podman, rootless setups, and remote Docker contexts work without assuming /var/run/docker.sock. start/az start/gcp start bind-mount the resolved unix socket or pass a remote tcp:// daemon through via DOCKER_HOST; doctor's docker.socket check reports the resolved endpoint. Adds DockerHostResolveTest and README documentation. Closes #3
This commit is contained in:
1 parent
67468966ce
commit
6862c0dd81
8 files changed
+204
-11
No files matched your search
@@ -81,7 +81,7 @@ public class StartCommand implements Callable<Integer> {
|
||||
List<String> args = new ArrayList<>();
|
||||
args.addAll(List.of("-d", "--name", global.container));
|
||||
args.addAll(List.of("-p", port + ":4566"));
|
||||
args.addAll(List.of("-v", "/var/run/docker.sock:/var/run/docker.sock"));
|
||||
args.addAll(DockerClient.dockerSocketRunArgs());
|
||||
if (persistDir != null) {
|
||||
args.addAll(List.of("-v", persistDir + ":/var/lib/floci"));
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ public class AzStartCommand implements Callable<Integer> {
|
||||
List<String> args = new ArrayList<>();
|
||||
args.addAll(List.of("-d", "--name", global.container));
|
||||
args.addAll(List.of("-p", port + ":4577"));
|
||||
args.addAll(List.of("-v", "/var/run/docker.sock:/var/run/docker.sock"));
|
||||
args.addAll(DockerClient.dockerSocketRunArgs());
|
||||
if (persistDir != null) {
|
||||
args.addAll(List.of("-v", persistDir + ":/app/data"));
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ public class GcpStartCommand implements Callable<Integer> {
|
||||
List<String> args = new ArrayList<>();
|
||||
args.addAll(List.of("-d", "--name", global.container));
|
||||
args.addAll(List.of("-p", port + ":4588"));
|
||||
args.addAll(List.of("-v", "/var/run/docker.sock:/var/run/docker.sock"));
|
||||
args.addAll(DockerClient.dockerSocketRunArgs());
|
||||
if (persistDir != null) {
|
||||
args.addAll(List.of("-v", persistDir + ":/app/data"));
|
||||
}
|
||||
|
||||
@@ -173,9 +173,74 @@ public class DockerClient {
|
||||
}
|
||||
}
|
||||
|
||||
/** How the Docker daemon is reached, resolved from the environment. */
|
||||
public enum Kind { UNIX, TCP, NPIPE }
|
||||
|
||||
/**
|
||||
* Parsed Docker daemon endpoint. {@code socketPath} is the local socket/pipe path
|
||||
* for {@link Kind#UNIX}/{@link Kind#NPIPE}, and null for {@link Kind#TCP}.
|
||||
*/
|
||||
public record DockerHost(Kind kind, String socketPath, String raw) {}
|
||||
|
||||
private static final String DEFAULT_UNIX_SOCKET = "/var/run/docker.sock";
|
||||
private static final String DEFAULT_WINDOWS_PIPE = "\\\\.\\pipe\\docker_engine";
|
||||
|
||||
/** Resolve the Docker endpoint from the current process environment and OS. */
|
||||
public static DockerHost dockerHost() {
|
||||
return parseDockerHost(
|
||||
System.getenv("DOCKER_HOST"),
|
||||
System.getenv("DOCKER_SOCK"),
|
||||
System.getProperty("os.name", ""));
|
||||
}
|
||||
|
||||
/**
|
||||
* Pure resolver for the Docker endpoint. Precedence: {@code DOCKER_HOST}
|
||||
* (standard) → {@code DOCKER_SOCK} (legacy override) → OS default.
|
||||
*/
|
||||
public static DockerHost parseDockerHost(String dockerHostEnv, String dockerSockEnv, String osName) {
|
||||
boolean windows = osName != null && osName.toLowerCase().contains("win");
|
||||
|
||||
if (dockerHostEnv != null && !dockerHostEnv.isBlank()) {
|
||||
String value = dockerHostEnv.trim();
|
||||
if (value.startsWith("unix://")) {
|
||||
return new DockerHost(Kind.UNIX, value.substring("unix://".length()), value);
|
||||
}
|
||||
if (value.startsWith("tcp://") || value.startsWith("http://") || value.startsWith("https://")) {
|
||||
return new DockerHost(Kind.TCP, null, value);
|
||||
}
|
||||
if (value.startsWith("npipe://")) {
|
||||
return new DockerHost(Kind.NPIPE, value.substring("npipe://".length()), value);
|
||||
}
|
||||
// No scheme — treat as a bare socket/pipe path.
|
||||
return new DockerHost(windows ? Kind.NPIPE : Kind.UNIX, value, value);
|
||||
}
|
||||
|
||||
if (dockerSockEnv != null && !dockerSockEnv.isBlank()) {
|
||||
return new DockerHost(Kind.UNIX, dockerSockEnv.trim(), dockerSockEnv.trim());
|
||||
}
|
||||
|
||||
if (windows) {
|
||||
return new DockerHost(Kind.NPIPE, DEFAULT_WINDOWS_PIPE, null);
|
||||
}
|
||||
return new DockerHost(Kind.UNIX, DEFAULT_UNIX_SOCKET, null);
|
||||
}
|
||||
|
||||
/** Back-compat accessor for the resolved local socket/pipe path (null for TCP). */
|
||||
public static String socketPath() {
|
||||
String os = System.getProperty("os.name", "").toLowerCase();
|
||||
if (os.contains("win")) return "\\\\.\\pipe\\docker_engine";
|
||||
return System.getenv().getOrDefault("DOCKER_SOCK", "/var/run/docker.sock");
|
||||
return dockerHost().socketPath();
|
||||
}
|
||||
|
||||
/**
|
||||
* The {@code docker run} arguments needed to give a container access to the host
|
||||
* Docker daemon. Unix sockets are bind-mounted at the canonical in-container path;
|
||||
* remote TCP daemons are passed through via {@code DOCKER_HOST}.
|
||||
*/
|
||||
public static List<String> dockerSocketRunArgs() {
|
||||
DockerHost host = dockerHost();
|
||||
return switch (host.kind()) {
|
||||
case TCP -> List.of("-e", "DOCKER_HOST=" + host.raw());
|
||||
case UNIX -> List.of("-v", host.socketPath() + ":" + DEFAULT_UNIX_SOCKET);
|
||||
case NPIPE -> List.of("-v", DEFAULT_UNIX_SOCKET + ":" + DEFAULT_UNIX_SOCKET);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -11,17 +11,23 @@ public class DockerSocketCheck implements Check {
|
||||
|
||||
@Override
|
||||
public CheckResult run(String endpoint, String container) {
|
||||
String socketPath = DockerClient.socketPath();
|
||||
String os = System.getProperty("os.name", "").toLowerCase();
|
||||
if (os.contains("win")) {
|
||||
return CheckResult.ok("docker.socket", "Windows named pipe (" + socketPath + ")");
|
||||
DockerClient.DockerHost host = DockerClient.dockerHost();
|
||||
switch (host.kind()) {
|
||||
case NPIPE:
|
||||
return CheckResult.ok("docker.socket", "Windows named pipe (" + host.socketPath() + ")");
|
||||
case TCP:
|
||||
return CheckResult.ok("docker.socket", "remote daemon (" + host.raw() + ")");
|
||||
default:
|
||||
break;
|
||||
}
|
||||
String socketPath = host.socketPath();
|
||||
if (Files.exists(Path.of(socketPath))) {
|
||||
return CheckResult.ok("docker.socket", socketPath + " accessible");
|
||||
}
|
||||
String os = System.getProperty("os.name", "").toLowerCase();
|
||||
String fix = os.contains("mac")
|
||||
? "Open Docker Desktop — the socket is created when Docker Desktop is running"
|
||||
: "sudo chmod 666 /var/run/docker.sock OR add your user to the docker group";
|
||||
: "sudo chmod 666 " + socketPath + " OR add your user to the docker group";
|
||||
return CheckResult.fail("docker.socket", socketPath + " not found or not accessible", fix);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user