diff --git a/README.md b/README.md index 95560e2..493bf7c 100644 --- a/README.md +++ b/README.md @@ -474,6 +474,14 @@ For a `unix://` socket the resolved path is bind-mounted into the container; for remote `tcp://` daemon the `DOCKER_HOST` value is passed through to the container instead. Run `floci doctor` to see which endpoint was resolved. +With `podman machine` on macOS, the socket on the Mac (the one `podman machine start` prints) is +a forwarder, and the container runs inside the VM, where that path is not the daemon's socket. +When the daemon is Podman, `floci start` asks `podman info` for the socket the daemon listens on +inside the VM (`/run/podman/podman.sock` rootful, `/run/user//podman/podman.sock` rootless) +and mounts that instead, so this needs the `podman` CLI on `PATH`. Set `DOCKER_SOCK` to choose +the mounted socket yourself: on macOS it is the mount even when `DOCKER_HOST` is set, and podman +is not asked. + With SELinux enforcing (for example the Fedora VM behind `podman machine`), the container's label keeps Floci from connecting to the socket, and the web console fails to start with `java.net.BindException: Permission denied`. Relabeling the mount with `:z` is not enough: it fixes diff --git a/plugin.json b/plugin.json index 1347c73..ec8906d 100644 --- a/plugin.json +++ b/plugin.json @@ -1,7 +1,7 @@ { "$schema": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json", "name": "floci", - "version": "1.0.5", + "version": "1.0.6", "description": "Run, wire up and troubleshoot Floci, the free local emulators for AWS, GCP, Azure and OCI: floci-cli lifecycle, safe wiring of aws, gcloud, az, oci and the SDKs, docker run and Docker Compose files, several instances side by side, and lookups in the published docs.", "author": { "name": "Floci", diff --git a/skills/floci/references/troubleshooting.md b/skills/floci/references/troubleshooting.md index e107cb3..dad52da 100644 --- a/skills/floci/references/troubleshooting.md +++ b/skills/floci/references/troubleshooting.md @@ -41,6 +41,8 @@ pass `-o text`. Then check the cloud-specific leaks: SQS endpoint bypass (AWS), **Docker-backed services fail (Lambda, ECS, RDS, Cloud Run, AKS, Functions, OKE)** The emulator cannot reach a Docker daemon. floci-cli resolves `DOCKER_HOST`, then `DOCKER_SOCK`, then the OS default socket. For Podman: `export DOCKER_HOST=unix:///run/user/$(id -u)/podman/podman.sock`. +With `podman machine` on macOS, `start` mounts the VM-side socket that `podman info` reports (it +needs `podman` on `PATH`); `DOCKER_SOCK` overrides it, even alongside `DOCKER_HOST`. `doctor` shows the resolved endpoint. In Compose, mount the socket explicitly. With SELinux enforcing (for example the `podman machine` VM), `java.net.BindException: Permission denied` means the container's label blocks the socket: run `floci start --security-opt label=disable` diff --git a/src/main/java/io/floci/cli/commands/StartCommand.java b/src/main/java/io/floci/cli/commands/StartCommand.java index 8209eda..c46d6c5 100644 --- a/src/main/java/io/floci/cli/commands/StartCommand.java +++ b/src/main/java/io/floci/cli/commands/StartCommand.java @@ -24,6 +24,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.TreeMap; import java.util.concurrent.Callable; import java.util.function.Consumer; @@ -446,7 +447,7 @@ public class StartCommand implements Callable { return 1; } - List args = dockerRunArgs(DockerClient.dockerSocketRunArgs(), daemon); + List args = dockerRunArgs(socketRunArgs(docker, daemon, printer), daemon); String id; try { @@ -519,6 +520,26 @@ public class StartCommand implements Callable { } } + /** + * The socket mount for this start: the daemon's own socket inside a {@code podman machine} VM + * when there is one ({@link DockerClient#podmanMachineSocket}), else the path the environment + * resolves. When podman cannot say where its socket is, the environment's path is still used, + * with a warning: it may work (podman's mac helper links {@code /var/run/docker.sock} to the + * rootful socket), and the user has a way to name the right one. + */ + public static List socketRunArgs(DockerClient docker, DockerClient.DockerHost daemon, Printer printer) { + try { + Optional vmSocket = docker.podmanMachineSocket(daemon); + if (vmSocket.isPresent()) return DockerClient.socketMountArgs(vmSocket.get()); + } catch (DockerException e) { + printer.warn("The Docker daemon is a Podman machine, but podman could not report its socket inside the VM: " + + e.getMessage().strip() + "\nIf Docker-backed services fail, set DOCKER_SOCK to the daemon's socket" + + " inside the VM (/run/podman/podman.sock for a rootful machine," + + " /run/user//podman/podman.sock for a rootless one) and re-run the command."); + } + return DockerClient.dockerSocketRunArgs(); + } + /** * Whether a busy local {@code port} would stop this start. Not for a remote daemon, which * publishes the port on its own machine, and not for a number that is no port at all: diff --git a/src/main/java/io/floci/cli/docker/DockerClient.java b/src/main/java/io/floci/cli/docker/DockerClient.java index 77e30a6..032fd39 100644 --- a/src/main/java/io/floci/cli/docker/DockerClient.java +++ b/src/main/java/io/floci/cli/docker/DockerClient.java @@ -32,6 +32,7 @@ public class DockerClient { public static final String CHECK_DOCKER = "Check that Docker is running ('docker info') and re-run the command."; private final String binary; + private final String podmanBinary; private final Duration timeout; public DockerClient() { @@ -45,7 +46,13 @@ public class DockerClient { /** Test seam: run {@code binary}, killing any non-streaming call that outlives {@code timeout}. */ public DockerClient(String binary, Duration timeout) { + this(binary, "podman", timeout); + } + + /** Test seam: run {@code binary} in place of {@code docker} and {@code podmanBinary} in place of {@code podman}. */ + public DockerClient(String binary, String podmanBinary, Duration timeout) { this.binary = binary; + this.podmanBinary = podmanBinary; this.timeout = timeout; } @@ -298,6 +305,9 @@ public class DockerClient { throw new DockerException("Interrupted"); } catch (IOException e) { if (e.getMessage() != null && e.getMessage().contains("No such file")) { + if (cmd.length > 0 && "podman".equals(cmd[0])) { + throw new DockerException("podman binary not found in PATH. Install Podman or add it to PATH."); + } throw new DockerException("docker binary not found in PATH. Install Docker Desktop or Docker Engine."); } throw new DockerException(e.getMessage()); @@ -372,6 +382,9 @@ public class DockerClient { if (cmd.length > 0 && "docker".equals(cmd[0])) { cmd = cmd.clone(); cmd[0] = binary; + } else if (cmd.length > 0 && "podman".equals(cmd[0])) { + cmd = cmd.clone(); + cmd[0] = podmanBinary; } return new ProcessBuilder(cmd) .redirectErrorStream(false) @@ -474,6 +487,70 @@ public class DockerClient { return dockerHost().socketPath(); } + /** + * Where the daemon behind {@code daemon} keeps its socket, when that is not the path this + * machine reaches it through, or empty. That is the case for {@code podman machine} on macOS: + * the socket the docker CLI talks to is a forwarder on the Mac, while containers run inside + * the VM, where the Mac path is no socket of the daemon's (and, where podman shares the + * directory into the VM, a socket nothing can connect to). Podman reports the path the + * daemon itself listens on as {@code Host.RemoteSocket.Path}; the Docker-compatible API has + * no such field, so podman is asked. + * + *

On macOS a set {@code DOCKER_SOCK} is that socket, and podman is not asked. It is the + * mount source even when {@code DOCKER_HOST} is what the docker CLI uses to reach the daemon: + * the two name the same daemon from either side of the VM. + * + * @throws DockerException when the daemon is Podman but podman could not say where its socket is + */ + public Optional podmanMachineSocket(DockerHost daemon) throws DockerException { + return podmanMachineSocket(daemon, System.getenv("DOCKER_SOCK"), System.getProperty("os.name", "")); + } + + /** {@link #podmanMachineSocket(DockerHost)} for a given {@code DOCKER_SOCK} and OS: the test seam. */ + public Optional podmanMachineSocket(DockerHost daemon, String dockerSockEnv, String osName) + throws DockerException { + if (!isLocalSocketOnMac(daemon, osName)) { + return Optional.empty(); + } + if (dockerSockEnv != null && !dockerSockEnv.isBlank()) { + return remoteSocketMountSource(dockerSockEnv, daemon.socketPath()); + } + String components; + try { + components = run("docker", "version", "--format", "{{range .Server.Components}}{{.Name}}|{{end}}"); + } catch (DockerException e) { + return Optional.empty(); // Not answered here: start reports an unreachable daemon itself. + } + if (!components.contains("Podman Engine")) return Optional.empty(); + String reported = run("podman", "--url", "unix://" + daemon.socketPath(), + "info", "--format", "{{.Host.RemoteSocket.Path}}").trim(); + return remoteSocketMountSource(reported, daemon.socketPath()); + } + + /** Whether {@link #podmanMachineSocket} applies at all: a local unix socket on macOS. */ + public static boolean isLocalSocketOnMac(DockerHost daemon, String osName) { + boolean mac = osName != null && osName.toLowerCase().startsWith("mac"); + return mac && daemon.kind() == Kind.UNIX && daemon.socketPath() != null; + } + + /** + * The bind source for {@code reported} (podman's {@code Host.RemoteSocket.Path}, or + * {@code DOCKER_SOCK}), or empty when there is nothing to change: no path, or the same path + * this machine already uses. + */ + public static Optional remoteSocketMountSource(String reported, String localPath) { + if (reported == null) return Optional.empty(); + String path = reported.trim(); + if (path.startsWith("unix://")) path = path.substring("unix://".length()); + if (path.isEmpty() || path.equals(localPath)) return Optional.empty(); + return Optional.of(path); + } + + /** The {@code docker run} arguments that mount {@code socketPath}, a path on the daemon's host, as the container's docker socket. */ + public static List socketMountArgs(String socketPath) { + return List.of("-v", socketPath + ":" + DEFAULT_UNIX_SOCKET); + } + /** * 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; @@ -483,7 +560,7 @@ public class DockerClient { 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 UNIX -> socketMountArgs(host.socketPath()); case NPIPE -> List.of("-v", DEFAULT_UNIX_SOCKET + ":" + DEFAULT_UNIX_SOCKET); }; } diff --git a/src/test/java/io/floci/cli/unit/PodmanMachineSocketTest.java b/src/test/java/io/floci/cli/unit/PodmanMachineSocketTest.java new file mode 100644 index 0000000..d19a8cf --- /dev/null +++ b/src/test/java/io/floci/cli/unit/PodmanMachineSocketTest.java @@ -0,0 +1,185 @@ +package io.floci.cli.unit; + +import io.floci.cli.commands.StartCommand; +import io.floci.cli.docker.DockerClient; +import io.floci.cli.docker.DockerClient.DockerHost; +import io.floci.cli.docker.DockerClient.Kind; +import io.floci.cli.docker.DockerException; +import io.floci.cli.output.OutputFormat; +import io.floci.cli.output.Printer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.condition.OS; +import org.junit.jupiter.api.io.TempDir; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * #78: with {@code podman machine} on macOS the socket the docker CLI reaches is a forwarder on + * the Mac, so {@code start} mounts the daemon's own socket inside the VM instead. + */ +class PodmanMachineSocketTest { + + private static final String MAC_SOCKET = "/var/folders/xx/T/podman/podman-machine-default-api.sock"; + private static final DockerHost MAC_PODMAN = new DockerHost(Kind.UNIX, MAC_SOCKET, "unix://" + MAC_SOCKET); + + @TempDir + Path tempDir; + + @ParameterizedTest + @CsvSource(nullValues = "-", value = { + "UNIX, /var/run/docker.sock, Mac OS X, true", + // No VM in between on Linux: the local path is the daemon's. + "UNIX, /run/user/1000/podman/podman.sock, Linux, false", + "NPIPE, //./pipe/podman, Windows 11, false", + "TCP, -, Mac OS X, false"}) + void onlyALocalSocketOnMacApplies(Kind kind, String socketPath, String os, boolean expected) { + assertEquals(expected, DockerClient.isLocalSocketOnMac(new DockerHost(kind, socketPath, null), os)); + } + + /** + * DOCKER_HOST reaches the daemon through the Mac forwarder, DOCKER_SOCK names its socket in + * the VM: the mount is DOCKER_SOCK, and podman is not asked. This is what start's warning + * tells the user to set. + */ + @Test + @DisabledOnOs(OS.WINDOWS) + void dockerSockIsTheMountEvenWithDockerHostSet() throws Exception { + DockerClient docker = client("echo docker must not run >&2\nexit 1", "echo podman must not run >&2\nexit 1"); + + assertEquals(Optional.of("/run/podman/podman.sock"), + docker.podmanMachineSocket(MAC_PODMAN, "/run/podman/podman.sock", "Mac OS X")); + assertEquals(Optional.of("/run/podman/podman.sock"), + docker.podmanMachineSocket(MAC_PODMAN, " unix:///run/podman/podman.sock ", "Mac OS X")); + // DOCKER_SOCK alone: it is already the resolved path, so the usual mount stands. + assertEquals(Optional.empty(), docker.podmanMachineSocket( + new DockerHost(Kind.UNIX, "/run/podman/podman.sock", "/run/podman/podman.sock"), + "/run/podman/podman.sock", "Mac OS X")); + // Not on Linux, where DOCKER_HOST keeps its precedence. + assertEquals(Optional.empty(), docker.podmanMachineSocket( + new DockerHost(Kind.UNIX, "/run/user/1000/podman/podman.sock", "unix:///run/user/1000/podman/podman.sock"), + "/elsewhere.sock", "Linux")); + } + + @ParameterizedTest + @CsvSource(nullValues = "-", value = { + "unix:///run/podman/podman.sock, /run/podman/podman.sock", + "/run/user/501/podman/podman.sock, /run/user/501/podman/podman.sock", + "' unix:///run/podman/podman.sock\n', /run/podman/podman.sock", + "'', -", + "-, -", + // Already the path in use: nothing to change. + "unix://" + MAC_SOCKET + ", -"}) + void theReportedSocketBecomesTheBindSource(String reported, String expected) { + assertEquals(Optional.ofNullable(expected), DockerClient.remoteSocketMountSource(reported, MAC_SOCKET)); + } + + @Test + @DisabledOnOs(OS.WINDOWS) + void aPodmanDaemonIsAskedWhereItsSocketIs() throws Exception { + Path podmanArgs = tempDir.resolve("podman-args"); + DockerClient docker = client( + "echo 'Podman Engine|Conmon|OCI Runtime (crun)|Engine|'", + "echo \"$@\" > '" + podmanArgs + "'\necho unix:///run/user/501/podman/podman.sock"); + + assertEquals(Optional.of("/run/user/501/podman/podman.sock"), + docker.podmanMachineSocket(MAC_PODMAN, null, "Mac OS X")); + assertEquals("--url unix://" + MAC_SOCKET + " info --format {{.Host.RemoteSocket.Path}}", + Files.readString(podmanArgs).trim()); + } + + @Test + @DisabledOnOs(OS.WINDOWS) + void dockerDesktopIsLeftAlone() throws Exception { + DockerClient docker = client("echo 'Engine|containerd|runc|docker-init|'", "echo podman must not run >&2\nexit 1"); + + assertEquals(Optional.empty(), docker.podmanMachineSocket(MAC_PODMAN, null, "Mac OS X")); + } + + @Test + @DisabledOnOs(OS.WINDOWS) + void anUnreachableDaemonIsNotAnsweredHere() throws Exception { + DockerClient docker = client("echo 'Cannot connect' >&2\nexit 1", "exit 1"); + + assertEquals(Optional.empty(), docker.podmanMachineSocket(MAC_PODMAN, null, "Mac OS X")); + } + + @Test + @DisabledOnOs(OS.WINDOWS) + void podmanThatCannotAnswerIsAnError() throws Exception { + DockerClient docker = client("echo 'Podman Engine|'", "echo 'Error: unable to connect' >&2\nexit 125"); + + DockerException e = assertThrows(DockerException.class, + () -> docker.podmanMachineSocket(MAC_PODMAN, null, "Mac OS X")); + assertTrue(e.getMessage().contains("unable to connect"), e.getMessage()); + } + + @Test + void startMountsTheVmSocket() { + List args = StartCommand.socketRunArgs(answering(Optional.of("/run/podman/podman.sock")), MAC_PODMAN, printer(new ByteArrayOutputStream())); + + assertEquals(List.of("-v", "/run/podman/podman.sock:/var/run/docker.sock"), args); + } + + @Test + void startWithoutAVmSocketKeepsTheEnvironmentsMount() { + List args = StartCommand.socketRunArgs(answering(Optional.empty()), MAC_PODMAN, printer(new ByteArrayOutputStream())); + + assertEquals(DockerClient.dockerSocketRunArgs(), args); + } + + @Test + void startWarnsWithANextStepWhenPodmanCannotAnswer() { + DockerClient failing = new DockerClient() { + @Override + public Optional podmanMachineSocket(DockerHost daemon) throws DockerException { + throw new DockerException("podman binary not found in PATH. Install Podman or add it to PATH."); + } + }; + ByteArrayOutputStream err = new ByteArrayOutputStream(); + + List args = StartCommand.socketRunArgs(failing, MAC_PODMAN, printer(err)); + + assertEquals(DockerClient.dockerSocketRunArgs(), args); + String warning = err.toString(); + assertTrue(warning.contains("podman binary not found"), warning); + assertTrue(warning.contains("set DOCKER_SOCK"), warning); + assertTrue(warning.contains("re-run the command"), warning); + } + + private DockerClient client(String dockerScript, String podmanScript) throws Exception { + Path docker = script("docker", dockerScript); + Path podman = script("podman", podmanScript); + return new DockerClient(docker.toString(), podman.toString(), Duration.ofSeconds(10)); + } + + private Path script(String name, String body) throws Exception { + Path file = tempDir.resolve(name); + Files.writeString(file, "#!/bin/sh\n" + body + "\n"); + assertTrue(file.toFile().setExecutable(true)); + return file; + } + + private static DockerClient answering(Optional socket) { + return new DockerClient() { + @Override + public Optional podmanMachineSocket(DockerHost daemon) { + return socket; + } + }; + } + + private static Printer printer(ByteArrayOutputStream err) { + return new Printer(new PrintStream(new ByteArrayOutputStream()), new PrintStream(err), OutputFormat.text, false); + } +} diff --git a/src/test/java/io/floci/cli/unit/StartPreflightTest.java b/src/test/java/io/floci/cli/unit/StartPreflightTest.java index b3038ab..f1eb692 100644 --- a/src/test/java/io/floci/cli/unit/StartPreflightTest.java +++ b/src/test/java/io/floci/cli/unit/StartPreflightTest.java @@ -52,6 +52,12 @@ class StartPreflightTest { return daemon; } + // On a Mac the real one would ask the machine's docker whether it is a Podman machine. + @Override + public Optional podmanMachineSocket(DockerHost daemon) { + return Optional.empty(); + } + @Override public Optional inspectContainer(String name) throws DockerException { return Optional.empty();