Skip to content

Commit ed3f569

Browse files
committed
Support forwarding of '/var/run/docker.sock' on macOS
Podman Desktop doesn't support forwarding of sockets on macOS, but with this hack we can work around it. This also brings podman-compose to parity with the regular command line. Signed-off-by: Aleksei Besogonov <b@alex.net>
1 parent ef9785a commit ed3f569

5 files changed

Lines changed: 74 additions & 12 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added support for `/var/run/docker.sock` forwarding on macOS in Podman Desktop.

podman_compose.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -404,18 +404,28 @@ async def assert_volume(compose: PodmanCompose, mount_dict: dict[str, Any]) -> N
404404
if mount_dict["type"] == "bind":
405405
basedir = os.path.realpath(compose.dirname)
406406
mount_src = mount_dict["source"]
407-
mount_src = os.path.realpath(os.path.join(basedir, os.path.expanduser(mount_src)))
408-
if not os.path.exists(mount_src):
409-
bind_opts = mount_dict.get("bind", {})
410-
if "create_host_path" in bind_opts and not bind_opts["create_host_path"]:
411-
raise ValueError(
412-
"invalid mount config for type 'bind': bind source path does not exist: "
413-
f"{mount_src}"
414-
)
415-
try:
416-
os.makedirs(mount_src, exist_ok=True)
417-
except OSError:
418-
pass
407+
is_darwin = os.uname().sysname == "Darwin"
408+
if is_darwin and os.path.isabs(mount_src) and mount_src.endswith(".sock"):
409+
# This is a hack to allow forwarding of "/var/run/docker.sock" on macOS using
410+
# the Podman Desktop. This file normally resolves to something like
411+
# "/private/var/folders/ph/3afd..../T/podman/podman-machine-default-api.sock".
412+
# Podman Desktop doesn't support socket forwarding, but if we leave the path
413+
# as unresolved, it will use the "/var/run/docker.sock" file from inside the
414+
# Linux virtual machine.
415+
mount_src = os.path.normpath(mount_src)
416+
else:
417+
mount_src = os.path.realpath(os.path.join(basedir, os.path.expanduser(mount_src)))
418+
if not os.path.exists(mount_src):
419+
bind_opts = mount_dict.get("bind", {})
420+
if "create_host_path" in bind_opts and not bind_opts["create_host_path"]:
421+
raise ValueError(
422+
"invalid mount config for type 'bind': bind source path does not exist: "
423+
f"{mount_src}"
424+
)
425+
try:
426+
os.makedirs(mount_src, exist_ok=True)
427+
except OSError:
428+
pass
419429
mount_dict["source"] = mount_src
420430
return
421431
if mount_dict["type"] != "volume" or not vol or not vol.get("name"):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
version: '3'
2+
3+
# The Podman socket is forwarded into the container's context
4+
services:
5+
test-docker-in-docker:
6+
build:
7+
dockerfile_inline: |
8+
FROM nopush/podman-compose-test
9+
RUN apt install -y curl
10+
security_opt:
11+
- label=disable
12+
volumes:
13+
- /var/run/docker.sock:/var/run/docker.sock
14+
command: ["curl", "-s", "--unix-socket", "/var/run/docker.sock", "http://v1.41/info"]
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# SPDX-License-Identifier: GPL-2.0
2+
import os
3+
import unittest
4+
5+
from tests.integration.test_utils import RunSubprocessMixin
6+
from tests.integration.test_utils import podman_compose_path
7+
from tests.integration.test_utils import test_path
8+
9+
10+
def compose_yaml_path() -> str:
11+
return os.path.join(os.path.join(test_path(), "macos_docker_socket"), "docker-compose.yaml")
12+
13+
14+
class TestMacosDockerSocket(unittest.TestCase, RunSubprocessMixin):
15+
def test_macos_docker_socket(self) -> None:
16+
if os.uname().sysname != "Darwin":
17+
self.skipTest("Test only runs on macOS")
18+
if not os.path.exists("/var/run/docker.sock"):
19+
self.skipTest("Docker compatibility is not turned on")
20+
21+
try:
22+
self.run_subprocess_assert_returncode([
23+
podman_compose_path(),
24+
"-f",
25+
compose_yaml_path(),
26+
"run",
27+
"--build",
28+
"test-docker-in-docker",
29+
])
30+
finally:
31+
self.run_subprocess_assert_returncode([
32+
podman_compose_path(),
33+
"-f",
34+
compose_yaml_path(),
35+
"down",
36+
])

0 commit comments

Comments
 (0)