fix(start): mount the podman machine's own socket on macOS (#81)

* fix(start): mount the podman machine's own socket on macOS

With podman machine on macOS, the socket the docker CLI reaches is a
forwarder on the Mac, and start bind-mounted that path into a container
that runs inside the VM. Depending on the podman version the start
failed ("operation not supported"), or it reported ready while the
emulator could not reach the daemon, so every Docker-backed service
failed later.

When the daemon is Podman, start now asks podman for the socket the
daemon listens on inside the VM (Host.RemoteSocket.Path) and mounts
that. DOCKER_SOCK still names the mounted socket and turns the lookup
off. When podman cannot answer, start warns and keeps the old mount.

Closes #78

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

* fix(start): mount DOCKER_SOCK on macOS even when DOCKER_HOST is set

start's warning tells the user to set DOCKER_SOCK when podman cannot
report its VM socket, but with DOCKER_HOST on the Mac forwarder the
mount still used the DOCKER_HOST path, so following the warning did not
help. On macOS DOCKER_SOCK is now the mount source whenever it is set.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>

---------

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-18 22:47:27 -06:00
1 parent 5c0db8f69f
commit e23bc9a481
7 files changed
+302 -3

No files matched your search

+8
View File
@@ -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/<uid>/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
+1 -1
View File
@@ -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",
@@ -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 <tree> start --security-opt label=disable`
@@ -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<Integer> {
return 1;
}
List<String> args = dockerRunArgs(DockerClient.dockerSocketRunArgs(), daemon);
List<String> args = dockerRunArgs(socketRunArgs(docker, daemon, printer), daemon);
String id;
try {
@@ -519,6 +520,26 @@ public class StartCommand implements Callable<Integer> {
}
}
/**
* 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<String> socketRunArgs(DockerClient docker, DockerClient.DockerHost daemon, Printer printer) {
try {
Optional<String> 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/<uid>/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:
@@ -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.
*
* <p>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<String> 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<String> 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<String> 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<String> 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);
};
}
@@ -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<String> 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<String> 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<String> 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<String> 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<String> socket) {
return new DockerClient() {
@Override
public Optional<String> podmanMachineSocket(DockerHost daemon) {
return socket;
}
};
}
private static Printer printer(ByteArrayOutputStream err) {
return new Printer(new PrintStream(new ByteArrayOutputStream()), new PrintStream(err), OutputFormat.text, false);
}
}
@@ -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<String> podmanMachineSocket(DockerHost daemon) {
return Optional.empty();
}
@Override
public Optional<ContainerInfo> inspectContainer(String name) throws DockerException {
return Optional.empty();