Skip to content

Add container_run: run Linux binaries on macOS via Apple's container CLI - #7

Open
arianvp wants to merge 4 commits into
mainfrom
container-run
Open

arianvp wants to merge 4 commits into
mainfrom
container-run

Conversation

@arianvp

@arianvp arianvp commented Oct 1, 2026

Copy link
Copy Markdown
Owner

container_run wraps a runnable target so that buck2 run (and
buck2 test) of a Linux-configured target works on a Mac: a small POSIX
launcher runs the program with Apple's container CLI there, mounting the
project root at the same path so the absolute paths buck2 renders stay
valid, and execs it directly on every other host. The wrap decision is made
from the target's configuration (only config//os:linux targets are
wrapped) and the dispatch decision at run time (uname -s), so action
digests stay identical across hosts and wrapped targets keep working as
tools on Linux executors.

  • prelude/container: rule, toolchain provider and launcher
  • prelude/toolchains/container.bzl: system_container_run_toolchain, added
    to system_demo_toolchains()
  • docs/users/how_tos/container_run.md: setup and usage guide
  • examples/with_prelude/container_run: CI-exercised example and launcher
    unit tests (fake container/uname cover the macOS path on any host)

Co-Authored-By: Claude Opus 5.5 noreply@anthropic.com
Claude-Session: https://claude.ai/code/session_01GdVaTFQQMHjXr7W3gpRL2d

`container_run` wraps a runnable target so that `buck2 run` (and
`buck2 test`) of a Linux-configured target works on a Mac: a small POSIX
launcher runs the program with Apple's `container` CLI there, mounting the
project root at the same path so the absolute paths buck2 renders stay
valid, and execs it directly on every other host. The wrap decision is made
from the target's configuration (only `config//os:linux` targets are
wrapped) and the dispatch decision at run time (`uname -s`), so action
digests stay identical across hosts and wrapped targets keep working as
tools on Linux executors.

- prelude/container: rule, toolchain provider and launcher
- prelude/toolchains/container.bzl: system_container_run_toolchain, added
  to system_demo_toolchains()
- docs/users/how_tos/container_run.md: setup and usage guide
- examples/with_prelude/container_run: CI-exercised example and launcher
  unit tests (fake container/uname cover the macOS path on any host)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GdVaTFQQMHjXr7W3gpRL2d
Launcher:
- Put `--` before the image so the container CLI cannot parse program
  arguments such as `---` or `--generate-completion-script` as its own.
- Prefix every launcher variable with `_cr_` and apply `env` with env(1) in
  direct mode, so program env names like `mode` or `UID` are not clobbered.
- Name the container and start a watchdog in its own process group that
  stops it when the CLI is killed (buck2 SIGKILLs cancelled tests; closing
  a terminal sends SIGHUP), instead of leaking a running VM.
- Find uname, launchctl and pwd at their macOS paths too; keep verbose
  output linear under bash 3.2; fix the not-found hint.

Rule and toolchain:
- `buck2 run` of a wrapped test with `env` passes the env to the launcher
  instead of running the prelude's Python env injector in the image.
- Leave `--platform` to the CLI when the target has no CPU constraint.
- Deduplicate `ports`; reserve the `_cr_` prefix; validate `cpus`/`memory`.
- Demo toolchain reads `[container_run] cli/cli_flavor` so developers can
  switch to a Docker-compatible CLI in .buckconfig.local.

Tests, CI and docs:
- Launcher tests cover args after the image, env collisions, exec, argv
  integrity, a pty, and the watchdog; they run in the PR workflow on Linux
  and macOS (bash 3.2).
- The example adds a wrapped test and an end-to-end test of the container
  path with a fake CLI.
- Docs: platform setup, per-developer CLI override, test environment,
  debugging, registry login, and corrected run-time details.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GdVaTFQQMHjXr7W3gpRL2d
Launcher watchdog:
- Only use `set -m` under bash (macOS's /bin/sh), where it leaves the
  process group without touching the terminal; dash takes over the
  controlling terminal with it. Other shells use setsid(1) if present.
- Give the watchdog the system directories on PATH, and give up instead
  of spinning if `sleep` is unavailable (a test's `env` may replace PATH).
- Leave containers started with `-d`/`--detach` alone.

Rule and toolchain:
- Only replace a wrapped test's RunInfo when it is the prelude's env
  injector and all env names are identifiers, so other test rules' RunInfo
  (e.g. erlang_test's shell) and unusual env names keep working.
- Accept decimal memory sizes (and decimal CPUs for Docker), and require a
  unit on memory, which both CLIs otherwise read as bytes.

Tests: read UID from the environment (bash always sets it, which broke
the macOS CI step), and check the watchdog against a process-group kill
with a PATH that lacks `sleep`, as buck2 does to a cancelled test.

CI: a macOS workflow that runs the launcher tests, builds and tests the
example with this prelude, installs Apple's container CLI, and runs a
Linux target through it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GdVaTFQQMHjXr7W3gpRL2d
Records why `container run` cannot start a VM on hosted macOS runners.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GdVaTFQQMHjXr7W3gpRL2d
@arianvp

arianvp commented Oct 9, 2026

Copy link
Copy Markdown
Owner Author

We might benefit from the container machine integration so that we don't pay for VM startup for every command. But not sure

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants