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

@@ -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);
}
}