Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ fix: ci [skip test] [skip sbom] # skip multiple steps
| Variable | Default | Description |
|----------|---------|-------------|
| `DBUILD_REGISTRY` | `ghcr.io/daemonless` | Default container registry |
| `DBUILD_TEST_NETWORK` | `podman` (`host` inside a jail without vnet) | Network for `dbuild test` containers. `host` shares the caller's network and probes `127.0.0.1` |
| `GITHUB_TOKEN` | | Forwarded as build secret + registry auth |
| `GITHUB_ACTOR` | | Registry login username |
| `DOCKERHUB_USERNAME` | | Enable Docker Hub mirroring |
Expand Down
4 changes: 4 additions & 0 deletions dbuild/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ def _global_config_doc_path() -> str:
),
"ENVIRONMENT": {
"DBUILD_REGISTRY": "Override the target container registry (e.g., ghcr.io/myorg).",
"DBUILD_TEST_NETWORK": (
"Network for the containers dbuild test starts. Default podman; host inside a "
"jail without vnet (a CI runner container), where the checks then probe 127.0.0.1."
),
"GITHUB_TOKEN": "Authentication token for GitHub Packages and build secrets.",
"GITHUB_ACTOR": "The username associated with GITHUB_TOKEN.",
"DOCKERHUB_USERNAME": "Enable mirroring by providing a Docker Hub username.",
Expand Down
41 changes: 38 additions & 3 deletions dbuild/podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,37 @@ def history(image: str, *, quiet: bool = False) -> list[dict[str, Any]]:
return json.loads(result.stdout) if result.stdout.strip() else []


def _jailed_without_vnet() -> bool:
"""True inside a FreeBSD jail that has no network stack of its own."""
def read(name: str) -> str:
try:
out = subprocess.run(
["sysctl", "-n", name], capture_output=True, text=True, check=False,
)
except OSError:
return ""
return out.stdout.strip()
return read("security.jail.jailed") == "1" and read("security.jail.vnet") == "0"


def cit_network() -> str:
"""Network for containers started by CIT.

``$DBUILD_TEST_NETWORK`` wins. Otherwise ``host`` inside a jail without
vnet (a CI runner container): no bridge can be created there, so the
container shares the runner's network. Otherwise ``podman``.
"""
env = os.environ.get("DBUILD_TEST_NETWORK")
if env:
return env
return "host" if _jailed_without_vnet() else "podman"


def run_detached(
image: str,
*,
name: str,
network: str = "podman",
network: str | None = None,
annotations: dict[str, str] | None = None,
env: dict[str, str] | None = None,
volumes: list[str] | None = None,
Expand All @@ -231,8 +257,9 @@ def run_detached(

*env* maps environment variable names to values (``-e KEY=VALUE``).
*volumes* is a list of ``-v`` mount specs (e.g. ``"vol:/config"``).
*network* defaults to :func:`cit_network`.
"""
cmd = ["podman", "run", "-d", "--name", name, f"--network={network}"]
cmd = ["podman", "run", "-d", "--name", name, f"--network={network or cit_network()}"]
for key, val in (annotations or {}).items():
cmd += ["--annotation", f"{key}={val}"]
for key, val in (env or {}).items():
Expand Down Expand Up @@ -288,7 +315,15 @@ def inspect_ip(container_name: str) -> str:
"{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}",
container_name,
])
return result.stdout.strip()
ip = result.stdout.strip()
if ip:
return ip
# A host-network container has no address of its own.
mode = _run(
["podman", "inspect", "--format", "{{.HostConfig.NetworkMode}}", container_name],
check=False,
)
return "127.0.0.1" if (mode.stdout or "").strip() == "host" else ""


def container_running(container_name: str, *, quiet: bool = False) -> bool:
Expand Down
5 changes: 4 additions & 1 deletion docs/dbuild.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH DBUILD 1 "2026-11-12" "dbuild 1.13.1" "User Commands"
.TH DBUILD 1 "2026-11-16" "dbuild 1.13.1" "User Commands"
.SH NAME
dbuild \- FreeBSD OCI container image build tool
.SH SYNOPSIS
Expand Down Expand Up @@ -376,6 +376,9 @@ enable/disable the PUID/PGID re-chown check for this run, overriding the `cit: p
\fIDBUILD_REGISTRY\fR
Override the target container registry (e.g., ghcr.io/myorg).
.TP
\fIDBUILD_TEST_NETWORK\fR
Network for the containers dbuild test starts. Default podman; host inside a jail without vnet (a CI runner container), where the checks then probe 127.0.0.1.
.TP
\fIGITHUB_TOKEN\fR
Authentication token for GitHub Packages and build secrets.
.TP
Expand Down
61 changes: 61 additions & 0 deletions tests/test_podman.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,67 @@ def test_with_env_and_volumes(self, mock_run):
self.assertIn("myvol:/config", cmd)


@patch("dbuild.podman._jailed_without_vnet", return_value=False)
@patch("dbuild.podman._run")
def test_network_defaults_to_podman(self, mock_run, _jailed):
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="abc123\n", stderr=""
)
with patch.dict("os.environ", {}, clear=False) as env:
env.pop("DBUILD_TEST_NETWORK", None)
podman.run_detached("myimage:latest", name="test-ctr")
self.assertIn("--network=podman", mock_run.call_args[0][0])

@patch("dbuild.podman._jailed_without_vnet", return_value=True)
@patch("dbuild.podman._run")
def test_network_is_host_in_a_jail_without_vnet(self, mock_run, _jailed):
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="abc123\n", stderr=""
)
with patch.dict("os.environ", {}, clear=False) as env:
env.pop("DBUILD_TEST_NETWORK", None)
podman.run_detached("myimage:latest", name="test-ctr")
self.assertIn("--network=host", mock_run.call_args[0][0])

@patch("dbuild.podman._jailed_without_vnet", return_value=True)
@patch("dbuild.podman._run")
def test_network_env_wins(self, mock_run, _jailed):
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="abc123\n", stderr=""
)
with patch.dict("os.environ", {"DBUILD_TEST_NETWORK": "mynet"}):
podman.run_detached("myimage:latest", name="test-ctr")
self.assertIn("--network=mynet", mock_run.call_args[0][0])


class TestInspectIp(unittest.TestCase):
"""Tests for inspect_ip()."""

@patch("dbuild.podman._run")
def test_bridge_address(self, mock_run):
mock_run.return_value = subprocess.CompletedProcess(
args=[], returncode=0, stdout="10.88.0.7\n", stderr=""
)
self.assertEqual(podman.inspect_ip("ctr"), "10.88.0.7")
self.assertEqual(mock_run.call_count, 1)

@patch("dbuild.podman._run")
def test_host_network_is_loopback(self, mock_run):
mock_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout="\n", stderr=""),
subprocess.CompletedProcess(args=[], returncode=0, stdout="host\n", stderr=""),
]
self.assertEqual(podman.inspect_ip("ctr"), "127.0.0.1")

@patch("dbuild.podman._run")
def test_no_address_on_other_networks(self, mock_run):
mock_run.side_effect = [
subprocess.CompletedProcess(args=[], returncode=0, stdout="\n", stderr=""),
subprocess.CompletedProcess(args=[], returncode=0, stdout="none\n", stderr=""),
]
self.assertEqual(podman.inspect_ip("ctr"), "")


class TestContainerRunning(unittest.TestCase):
"""Tests for container_running()."""

Expand Down
Loading