Run-Parts Hook Directories
One small runner turns each lifecycle event into a directory of .sh hooks, run in lexical order, with failures reported and skipped so no hook can block bring-up.
- Kind
- Orchestration
- Domain
- Environment & Tooling
- Applies at
.devcontainer/lifecycle/- Status
- Stable
- Source · managed
.devcontainer/lifecycle/run-parts.sh
01 Defines
The contract of run-parts.sh: given one lifecycle event name, it runs every .sh file in the matching <event>.d/ directory with bash, in lexical order, reporting and skipping failures so one broken hook cannot stop the event.
02 Applies when
- A single configured command needs to grow into several independent setup steps.
- Steps are added or removed by adding or deleting files, without editing configuration.
- The same hooks are run by the Dev Containers tooling and by hand while debugging.
03 Boundaries
- The runner only sequences scripts; what each hook does is up to the hook.
- It does not run hooks in parallel.
- Files other than
*.shin a hook directory are ignored.
Expected behaviour
- 01The runner takes exactly one argument, the event name, and exits with usage text otherwise.
- 02A missing
<event>.d/directory is an error that names the expected path. - 03An empty directory is not an error; the runner says no scripts were found.
- 04Hooks run in lexical order, each invoked with
bashand announced by name. - 05A failing hook is reported with its exit status and skipped; the rest of the event still runs.
- 06The runner ends with a line listing every hook that failed, and exits successfully.
Behaviour#
The name comes from the classic Unix run-parts utility, which runs every
script in a directory. Here the directory is chosen by event: post-start
resolves to post-start.d/, on-create to on-create.d/, and so on. Adding
a hook is adding a file; the devcontainer.json command never changes.
- 01 Check arguments Exactly one non-empty event name, or usage text and exit 1
-
02
Resolve directory
<event>.d/beside the runner; a missing directory exits 1 -
03
Announce
Running lifecycle event: <event> -
04
Run hooks
Each
*.shin lexical order, asbash <script>, after==> Running <name> - 05 Report failures A non-zero exit is printed with its status; the rest of the event still runs
- 06 Summarise One final line lists every failing script; the runner exits 0
The canonical event names are initialize, on-create, update-content,
post-create, post-start, and post-attach, one per phase of the
Devcontainer Lifecycle
. The runner enforces them through the
directory check: an event with no matching directory is refused.
Ordering#
Because the shell glob expands in lexical order, numeric prefixes make the
sequence visible in a directory listing: 10-setup-devcontainer-tools.sh runs
before 20-configure-zsh-profile.sh. Scripts that don’t depend on each other
can share a prefix or use none — the convention is to prefix only when order
matters.
Each hook is invoked as bash "$script", so it doesn’t need its executable
bit set and doesn’t depend on its shebang. The runner itself runs under
set -eu, so its own mistakes fail loudly; hook failures are handled
separately, below.
Failure handling#
The runner never lets one hook stop the phase. When a hook exits non-zero, the runner prints the failure with its exit status, records the name, and moves on to the next script. When the loop finishes, a single final line lists every hook that failed during the event — and the runner exits 0.
That is a deliberate trade. Every one of devcontainer.json’s six lifecycle
commands delegates to this runner, so a hook that failed hard here used to
fail devcontainer up itself and leave every later script in the same phase
untried, however unrelated to the one that broke. Bring-up succeeding matters
more than any one script’s exit code: a broken hook needs fixing, but it
never blocks a developer from getting a workspace.
Conventions for hooks#
- A hook is a file: adding one is adding a file, removing one is deleting it.
- Only
*.shruns; aREADME.mdin the directory is documentation, not a hook. - Hooks are invoked with
bash, so the executable bit and shebang are formality — but each hook still runs under its ownset -euo pipefail, so it fails fast and the runner can report it. - Numeric prefixes only when order matters; otherwise the natural file order is enough.
- Hooks announce what they do: explicit checks and clear output beat long explanatory logging.
- Every hook is idempotent — the runner makes reruns possible, and the hooks make them safe.
Execution patterns#
The runner backs every lifecycle command, so the Dev Containers tooling and a developer debugging a hook reach the same code path:
bash .devcontainer/lifecycle/run-parts.sh initialize
bash .devcontainer/lifecycle/run-parts.sh on-create
bash .devcontainer/lifecycle/run-parts.sh update-content
bash .devcontainer/lifecycle/run-parts.sh post-create
bash .devcontainer/lifecycle/run-parts.sh post-start
bash .devcontainer/lifecycle/run-parts.sh post-attach
Running a phase by hand reruns exactly what the container ran — the entry point is the same, so behaviour cannot drift between the two. A hook can be edited and rerun in place without rebuilding the container, and the event summary shows at a glance which hooks failed and with what status.
Examples#
The runner’s loop, with the failure path that keeps the event moving:
found_script=0
failed_scripts=""
for script in "$hooks_dir"/*.sh; do
if [ ! -f "$script" ]; then
continue
fi
found_script=1
script_name=${script##*/}
echo "==> Running $script_name"
if bash "$script"; then
script_status=0
else
script_status=$?
fi
if [ "$script_status" -ne 0 ]; then
echo "==> $script_name failed (exit $script_status); continuing with the rest of $event.d" >&2
failed_scripts="$failed_scripts $script_name"
fi
done
if [ -n "$failed_scripts" ]; then
echo "==> lifecycle event $event completed with failing script(s):$failed_scripts" >&2
fi
exit 0
Connections