本文へ移動
cccskills
無料GitHub で公開

docker-exec-background-process

Fix docker exec hanging when starting background processes (servers, daemons) inside containers. Use when: (1) docker exec never returns despite using & or nohup, (2) background server started in container causes docker exec to hang indefinitely, (3) env_startup_command in SWE-bench or similar frameworks times out, (4) setsid/disown needed for proper process detachment in Docker. Root cause: docker exec tracks ALL processes in the exec session, not just the top-level PID.

インストール方法を見る

含まれるファイル(1)

  • SKILL.md5.1 KB

SKILL.md(原文)

インストールする前に、エージェントに与えられる指示の中身を確認できます。

Docker Exec Background Process Detachment

Problem

When using docker exec to start a background process (like a database server) inside a container, the docker exec command hangs indefinitely and never returns, even when using &, nohup, or output redirection.

Context / Trigger Conditions

  • docker exec container bash -c "my-server &" never returns
  • docker exec container bash -c "nohup my-server > /dev/null 2>&1 &" still hangs
  • Startup commands in frameworks (SWE-bench, mini-SWE-agent) time out at 300/600s
  • Any scenario where docker exec needs to start a long-running daemon and return

Root Cause

Docker exec tracks ALL processes started in the exec session, not just the top-level bash process. This is different from regular bash behavior:

  1. & — creates background job but bash still tracks it in its job table
  2. nohup — prevents SIGHUP but does NOT create a new session; docker still tracks it
  3. Output redirection (>/dev/null 2>&1) — prevents output but docker exec still waits for all session processes to terminate

Docker exec uses Linux process sessions/groups. All processes started within an exec call share the same session. Docker waits for the entire session to finish.

Solution

Use setsid + disown + stdin/stdout/stderr redirection:

docker exec container bash -c 'setsid /path/to/server arg1 arg2 </dev/null >/dev/null 2>&1 & disown && sleep 1 && echo "Ready"'

All four components are required:

ComponentPurpose
setsidCreates a NEW session (new session ID, detaches from exec session)
</dev/nullPrevents stdin inheritance from docker exec
>/dev/null 2>&1Prevents stdout/stderr from keeping pipe fds alive
& disownRemoves job from bash's job table so bash doesn't wait for it

Why each is necessary:

  • Without setsid: Process stays in docker exec's session → docker waits
  • Without </dev/null: Process may hold stdin fd → docker waits
  • Without >/dev/null 2>&1: Process may hold pipe fds (especially with | tail) → docker waits
  • Without disown: Bash waits for background jobs before exiting → docker waits

Related Problem: Pipe FD Inheritance

A compounding issue occurs when using pipes in the startup command:

# THIS HANGS: tail waits for EOF, but server inherits the pipe fd
docker exec container bash -c 'my-server 2>&1 | tail -3'

If my-server spawns child processes with stdio: 'inherit' (common in Node.js), those children inherit the pipe file descriptor. tail waits for ALL writers to close the pipe, but the inherited fd in the child process keeps it open forever.

Fix: Don't pipe server output. Use >/dev/null 2>&1 or write to a log file:

# Instead of piping, redirect to file and read separately
docker exec container bash -c 'my-server > /tmp/server.log 2>&1 & sleep 2 && tail -3 /tmp/server.log'

Verification

After running the startup command:

  1. docker exec should return promptly (within the sleep duration)
  2. docker exec container ps aux should show the server process still running
  3. The server should be accessible (check socket, port, etc.)

Example: Starting rfdb-server in SWE-bench

# mini-SWE-agent config
run:
  env_startup_command: |
    ln -sf /opt/grafema/node_modules/.bin/grafema /usr/local/bin/grafema && \
    cp /opt/node_modules/@grafema/rfdb/prebuilt/linux-x64/rfdb-server /usr/local/bin/rfdb-server && \
    chmod +x /usr/local/bin/rfdb-server && \
    setsid /usr/local/bin/rfdb-server /testbed/.grafema/graph.rfdb \
      --socket /testbed/.grafema/rfdb.sock </dev/null >/dev/null 2>&1 & disown && \
    sleep 2 && echo "Server ready"

What Does NOT Work

# FAILS: nohup doesn't create new session
docker exec container bash -c 'nohup my-server &'

# FAILS: output redirection alone isn't enough
docker exec container bash -c 'my-server > /dev/null 2>&1 &'

# FAILS: disown alone isn't enough (still in same session)
docker exec container bash -c 'my-server & disown'

# FAILS: even all three without setsid
docker exec container bash -c 'nohup my-server > /dev/null 2>&1 & disown'

Notes

  • setsid is available on most Linux distributions (part of util-linux)
  • The sleep after disown gives the server time to initialize before the script continues
  • If the server needs to be verified running, check its pidfile or socket after the sleep
  • On macOS Docker Desktop, x86_64 emulation adds ~10x latency to all operations
  • For SWE-bench/mini-SWE-agent, env_startup_command runs via docker exec bash -c "...", so this pattern applies directly

レビュー

まだレビューはありません。使ってみた感想をお寄せください。

同じリポジトリのスキル

概要と使いどころ

Fix Elixir/Erlang AST processing bugs in Grafema beam-analyzer. Use when: (1) Elixir parser returns MODULE node but 0 functions/calls — body nesting issue, (2) Erlang parser crashes with "cannot convert list to string" on OTP 26+ — location format changed from integer to keyword list, (3) pipe operator |> creates spurious CALL nodes instead of desugared function calls — clause ordering bug, (4) multi-module .ex files return only the first module — missing __block__ handler, (5) installing Erlang/Elixir on macOS with outdated Xcode/Clang.

日本語の概要は準備中です。原文の説明を表示しています。

Disentinel/grafema362026年8月24日 更新

Fires after fixing any non-trivial bug or regression. Asks: could the graph have caught this as a guarantee? Pairs with reflection-in-and-on-action — picks up after "earliest catchable signal" and asks the next question: was that signal expressible in graph? Triggers: (1) after any non-trivial bug fix is verified working, (2) after a regression report (something used to work, broke), (3) during step 6 (knowledge extraction) of the workflow, (4) when reflection-on-action surfaces a "would have been catchable" signal. Outcome is a triage decision (graph-reachable? rule expressible? rule sound?) and either a draft Linear issue + guarantee proposal, or a recorded "graph capability gap" note. Never auto-creates guarantees.

日本語の概要は準備中です。原文の説明を表示しています。

Disentinel/grafema362026年8月24日 更新

Fix Playwright automation failures against code-server (VS Code in browser). Use when: (1) trust dialog blocks all clicks — "monaco-dialog-modal-block intercepts pointer events", (2) button:has-text() finds wrong buttons behind modal dialog, (3) keyboard shortcuts don't work — Meta vs Control inconsistency, (4) VS Code extension activity bar icon not found by aria-label, (5) panels show placeholder text despite extension being "Connected". Covers trust dialog dismissal, keyboard shortcut hybrid mode, extension panel selectors, and database connection verification for Grafema extension.

日本語の概要は準備中です。原文の説明を表示しています。

Disentinel/grafema362026年8月24日 更新

Systematic methodology for achieving 100% backward dataflow reachability in a new language. Create gauntlet fixture, write trace, diagnose gaps, fix analyzer/algorithm, iterate to 100%. Language-agnostic process. Use when: (1) adding a new language to Grafema, (2) auditing dataflow coverage for existing language, (3) user says "/dataflow-gauntlet".

日本語の概要は準備中です。原文の説明を表示しています。

Disentinel/grafema362026年8月24日 更新

Fix Node.js CLI tools crashing inside Docker containers when host-installed node_modules require a newer Node version than the container provides. Use when: (1) "SyntaxError: Invalid regular expression flags" with /v flag in string-width or similar packages, (2) node_modules installed on host with Node 20+ but container has Node 18, (3) `npm install` with file: protocol creates symlinks that break inside Docker, (4) pnpm workspace packages become broken symlinks in containers. Covers version detection, Node binary mounting, and npm install strategies for cross-version compatibility.

日本語の概要は準備中です。原文の説明を表示しています。

Disentinel/grafema362026年8月24日 更新

Fix intermittent `{:no_translation, :unicode, :latin1}` crashes in Elixir escript daemons that use length-prefixed framed IPC on stdin/stdout. Use when: (1) daemon worker crashes only on some input files, usually ones with non-ASCII bytes (kanji, cyrillic, emoji); (2) error surfaces as `Protocol error` from daemon's error branch or as garbled frame-length bytes seen by the orchestrator/client side; (3) standalone one-shot mode works fine on the same input but multi-request daemon mode fails; (4) `IO.binread(:stdio, N)` returns `{:error, {:no_translation, :unicode, :latin1}}` despite the "bin" prefix suggesting it should be encoding-agnostic. Root cause is the escript default `:standard_io` encoding — it's `:unicode`, and `IO.binread` still routes through the io_server, which translates bytes to codepoints and errors when raw binary frames contain invalid UTF-8 sequences.

日本語の概要は準備中です。原文の説明を表示しています。

Disentinel/grafema362026年8月24日 更新

Disentinel のスキルをすべて見る

このスキルの問題を報告する