Debug shell for containers without a shell (distroless) #14

Closed
opened 2026-11-30 14:39:42 +00:00 by jfro · 1 comment
Owner

OrbStack's debug shell gets you a shell with tools in any container, even distroless images. Krane's "open a shell" needs sh inside the container.

What

  • A "Debug shell" action on containers: start a helper tools image (e.g. a small busybox/alpine/nixery-style image, configurable) with --pid container:<id>, --network container:<id>, and the target's filesystem reachable via /proc/1/root, then open the terminal into it.
  • Remove the helper when the shell exits.
  • Fall back to this automatically, or offer it, when the regular shell fails because the image has no shell.

Notes

  • Pure engine API, works against any Docker-compatible engine.
OrbStack's debug shell gets you a shell with tools in any container, even distroless images. Krane's "open a shell" needs `sh` inside the container. **What** - A "Debug shell" action on containers: start a helper tools image (e.g. a small busybox/alpine/nixery-style image, configurable) with `--pid container:<id>`, `--network container:<id>`, and the target's filesystem reachable via `/proc/1/root`, then open the terminal into it. - Remove the helper when the shell exits. - Fall back to this automatically, or offer it, when the regular shell fails because the image has no shell. **Notes** - Pure engine API, works against any Docker-compatible engine.
jfro added this to the v1.0 milestone 2026-11-30 14:47:14 +00:00
Author
Owner

Approach for the debug shell (research by Nye, our reviewer, tested on Docker 26 with bollard 0.20 against distroless root and nonroot targets). Upper Wolfjaw builds from this.

Krane #14: debug shell for distroless containers (Nye, Nov 30)

Tested on Docker 26.1.5 (Debian 13, arm64) with bollard 0.20. The test targets were distroless/static-debian12 in its root and :nonroot (uid 65532) variants, each running /pause with no sh. docker exec fails on them with rc 126. Probe program: issue14-probe-main.rs (it builds against bollard 0.20.0 as-is).

Approach (works)

  1. Check the target: inspect_container(id). It needs state.running == true. If not, show "Start the container to debug it".
  2. Get the tools image without a terminal: inspect_image(IMAGE). If that errors, run create_image(CreateImageOptionsBuilder::new().from_image(IMAGE).build(), None, None) and drain the stream (try_collect). Show a busy state or a toast while it runs. Default IMAGE: busybox:1.37, pinned (about 2 MB, multi-arch, has sh/ps/wget/nc/vi). Make it a setting so users can choose alpine (gets apk) or their own image.
  3. Create the helper with create_container(Some(CreateContainerOptionsBuilder::new().name(&name).build()), body):
ContainerCreateBody {
  image: Some(IMAGE.into()),
  cmd: Some(vec!["sh".into(), "-c".into(), "cd /proc/1/root 2>/dev/null; exec sh".into()]),
  env: Some(vec!["PS1=debug($(hostname)) \\w # ".into()]),
  tty: Some(true), open_stdin: Some(true), stdin_once: Some(true),
  attach_stdin: Some(true), attach_stdout: Some(true), attach_stderr: Some(true),
  labels: Some({"dev.krane.debug-shell": <target full id>}),
  host_config: Some(HostConfig {
    pid_mode: Some(format!("container:{tid}")),
    network_mode: Some(format!("container:{tid}")),
    cap_add: Some(vec!["SYS_PTRACE".into()]),
    auto_remove: Some(true),
    ..Default::default()
  }),
  ..Default::default()
}
  • name: krane-debug-<short id>-<random 4 hex>. A fixed name gives a 409 on the second debug shell for the same target (tested).
  • Don't set working_dir: /proc/1/root. runc refuses it ("cwd outside container mount namespace root, possible breakout"), so do the cd in the cmd as above.
  • CAP_SYS_PTRACE is required for nonroot targets. Without it, /proc/1/root returns "Permission denied" (root helper, uid-65532 target). Running the helper as the target's uid doesn't help either (tested). For root targets it works without the cap.
  • Leave out ipc_mode: container:<id>. It fails on Docker's default (private) IPC with "non-shareable IPC". It isn't needed for a shell.
  1. Open the terminal using the existing open_terminal plumbing (src/window/mod.rs), but with ["docker", "start", "-ai", <helper id>] in place of docker exec -it. Factor open_terminal to take the argv.

Results in the shell (both targets): ps shows the target's PID 1, the network is the target's (same eth0/IP, 127.0.0.1 reaches its ports), and you start in /proc/1/root where the target's files are readable and writable.

Cleanup

  • auto_remove removes the helper when the shell exits, when start fails, and when the target stops while the shell is open (the helper is killed and removed; all tested, 0 left).
  • Safety net on Krane startup: list_containers(all, filters label=dev.krane.debug-shell) and force-remove any that aren't running, for crashes between create and start.
  • Hide label-dev.krane.debug-shell containers from the Containers list, or show them under their target.

Fallback

docker exec -it id sh fails with rc 126 (exec: "sh": executable file not found) inside the terminal, where Krane can't see it. Do the check before opening the terminal instead: bollard create_exec with cmd ["sh","-c","true"], start_exec detached, then inspect_exec. If the exit code is 126 or 127 (or start errors), open the debug shell, or show "No shell in this image, open a debug shell?". Simpler alternative: always offer a separate "Debug shell" menu item, and only auto-fall back when the check says so.

Caveats

  • Docker CLI: the terminal runs docker (as open_terminal already does). If Krane's engine (or the #16 engine picker) differs from the CLI's current context, the terminal hits the wrong engine. Pass -H <host> / DOCKER_HOST from Krane's connection to both the exec and debug commands. This matters for #16 too.
  • Remote engines: pull/create/attach are pure API calls, so they work. /proc/1/root and the pid namespace are on the engine host, which is fine.
  • Rootless Docker: cap_add SYS_PTRACE is granted inside the user namespace, so it should still work (not tested here, no rootless engine on this box). Podman's compat API supports container: pid/network modes; also not tested.
  • Targets with --pid host or --network host: container: joining still works (you inherit host ns). Containers that use network_mode: container:X themselves can still be joined.
  • Security: the helper gets SYS_PTRACE and the target's pid/net namespaces. That's the same as docker debug / OrbStack, and fine for a local dev tool.

Calls for Jeremy

None that block building. The defaults (busybox:1.37, image as a setting, auto fallback with a prompt) follow the issue text.

Approach for the debug shell (research by Nye, our reviewer, tested on Docker 26 with bollard 0.20 against distroless root and nonroot targets). Upper Wolfjaw builds from this. # Krane #14: debug shell for distroless containers (Nye, Nov 30) Tested on Docker 26.1.5 (Debian 13, arm64) with bollard 0.20. The test targets were distroless/static-debian12 in its root and :nonroot (uid 65532) variants, each running /pause with no sh. `docker exec` fails on them with rc 126. Probe program: issue14-probe-main.rs (it builds against bollard 0.20.0 as-is). ## Approach (works) 1. Check the target: `inspect_container(id)`. It needs `state.running == true`. If not, show "Start the container to debug it". 2. Get the tools image without a terminal: `inspect_image(IMAGE)`. If that errors, run `create_image(CreateImageOptionsBuilder::new().from_image(IMAGE).build(), None, None)` and drain the stream (`try_collect`). Show a busy state or a toast while it runs. Default IMAGE: `busybox:1.37`, pinned (about 2 MB, multi-arch, has sh/ps/wget/nc/vi). Make it a setting so users can choose `alpine` (gets apk) or their own image. 3. Create the helper with `create_container(Some(CreateContainerOptionsBuilder::new().name(&name).build()), body)`: ```rust ContainerCreateBody { image: Some(IMAGE.into()), cmd: Some(vec!["sh".into(), "-c".into(), "cd /proc/1/root 2>/dev/null; exec sh".into()]), env: Some(vec!["PS1=debug($(hostname)) \\w # ".into()]), tty: Some(true), open_stdin: Some(true), stdin_once: Some(true), attach_stdin: Some(true), attach_stdout: Some(true), attach_stderr: Some(true), labels: Some({"dev.krane.debug-shell": <target full id>}), host_config: Some(HostConfig { pid_mode: Some(format!("container:{tid}")), network_mode: Some(format!("container:{tid}")), cap_add: Some(vec!["SYS_PTRACE".into()]), auto_remove: Some(true), ..Default::default() }), ..Default::default() } ``` - name: `krane-debug-<short id>-<random 4 hex>`. A fixed name gives a 409 on the second debug shell for the same target (tested). - Don't set `working_dir: /proc/1/root`. runc refuses it ("cwd outside container mount namespace root, possible breakout"), so do the cd in the cmd as above. - `CAP_SYS_PTRACE` is required for nonroot targets. Without it, /proc/1/root returns "Permission denied" (root helper, uid-65532 target). Running the helper as the target's uid doesn't help either (tested). For root targets it works without the cap. - Leave out `ipc_mode: container:<id>`. It fails on Docker's default (`private`) IPC with "non-shareable IPC". It isn't needed for a shell. 4. Open the terminal using the existing `open_terminal` plumbing (src/window/mod.rs), but with `["docker", "start", "-ai", <helper id>]` in place of `docker exec -it`. Factor `open_terminal` to take the argv. Results in the shell (both targets): `ps` shows the target's PID 1, the network is the target's (same eth0/IP, 127.0.0.1 reaches its ports), and you start in /proc/1/root where the target's files are readable and writable. ## Cleanup - `auto_remove` removes the helper when the shell exits, when start fails, and when the target stops while the shell is open (the helper is killed and removed; all tested, 0 left). - Safety net on Krane startup: `list_containers(all, filters label=dev.krane.debug-shell)` and force-remove any that aren't running, for crashes between create and start. - Hide label-`dev.krane.debug-shell` containers from the Containers list, or show them under their target. ## Fallback `docker exec -it id sh` fails with rc 126 (`exec: "sh": executable file not found`) inside the terminal, where Krane can't see it. Do the check before opening the terminal instead: bollard `create_exec` with cmd `["sh","-c","true"]`, `start_exec` detached, then `inspect_exec`. If the exit code is 126 or 127 (or start errors), open the debug shell, or show "No shell in this image, open a debug shell?". Simpler alternative: always offer a separate "Debug shell" menu item, and only auto-fall back when the check says so. ## Caveats - Docker CLI: the terminal runs `docker` (as open_terminal already does). If Krane's engine (or the #16 engine picker) differs from the CLI's current context, the terminal hits the wrong engine. Pass `-H <host>` / `DOCKER_HOST` from Krane's connection to both the exec and debug commands. This matters for #16 too. - Remote engines: pull/create/attach are pure API calls, so they work. /proc/1/root and the pid namespace are on the engine host, which is fine. - Rootless Docker: cap_add SYS_PTRACE is granted inside the user namespace, so it should still work (not tested here, no rootless engine on this box). Podman's compat API supports `container:` pid/network modes; also not tested. - Targets with `--pid host` or `--network host`: `container:` joining still works (you inherit host ns). Containers that use `network_mode: container:X` themselves can still be joined. - Security: the helper gets SYS_PTRACE and the target's pid/net namespaces. That's the same as `docker debug` / OrbStack, and fine for a local dev tool. ## Calls for Jeremy None that block building. The defaults (busybox:1.37, image as a setting, auto fallback with a prompt) follow the issue text.
jfro closed this issue 2026-11-30 20:34:01 +00:00
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
jfro/krane#14
No description provided.