Lifecycle specification

Initialize Phase

Host-side hooks that run before the container is built or created, preparing only the files and directories the container definition expects.

Applies at
.devcontainer/lifecycle/initialize.d/
Status
Stable

01 Defines

The initializeCommand phase: scripts in initialize.d/ that run on the host, before the image is built, to create host-side state that a mount or build depends on.

02 Applies when

  • A mount needs a host-side directory or file to exist before the container is created.
  • A required environment file is created from a repository-provided example before container creation reads it.

03 Boundaries

  • Nothing here may assume the container shell, the mounted workspace, or tooling provided by the environment.
  • Project dependency operations and in-container tool installs belong in later phases or features.
  • Interactive sign-in flows are out of scope.

Expected behaviour

  1. 01
    Scripts run on the host, not in the container.
  2. 02
    The phase may run more than once, so every script is safe to repeat.
  3. 03
    The phase holds only what the container definition needs before creation.
  4. 04
    A failing script is reported and skipped by the runner; bring-up proceeds with the failure in the event summary.

Behaviour#

initializeCommand is the only phase that runs outside the container. It runs before the image is built and the container is created, and the Dev Containers tooling may run it again on later rebuilds. That makes it the narrowest phase: it exists so that host-side paths are in place when the container definition refers to them, and for nothing else.

If a task needs the container shell, the mounted workspace, or a tool the environment provides, it belongs in On-Create Phase or later.

What is in place when it runs#

Almost nothing. There is no container, no image, and no guarantee that the repository’s tools exist on the host. A hook here can rely on the repository being checked out and on the host’s own shell — that is the whole environment.

The corollary is that the phase ages badly: the more a hook does, the more it depends on the developer’s host, and the less the container definition is the single source of the environment. A good initialize hook is one or two lines, produces a path, and changes nothing else.

Failure behaviour#

A failing initialize hook is reported and skipped by the Run-Parts Hook Directories ; devcontainer up continues. The failure is not silent — the event summary names the hook and its exit status — but the consequence lands later, when the mount or build that expected the prepared path complains about it. Writing the hook idempotently matters for the same reason: the fix is to rerun the phase by hand and rebuild.

Good uses

  • Creating a persistent host-side cache directory, such as .cache/bazel, before it is mounted
  • Creating a required environment file from a checked-in example
  • Pre-creating a file a mount expects, such as .devcontainer/aws/config

Avoid

  • npm install, pnpm install, dotnet restore, or terraform init
  • Installing Hugo, Node, Terraform, or other in-container tools
  • aws sso login, gh auth login, and other interactive sign-ins

Examples#

.devcontainer/lifecycle/initialize.d/10-create-cache.sh
#!/usr/bin/env bash
set -euo pipefail

mkdir -p .cache/bazel

mkdir -p is already idempotent, which is exactly the shape a host-side hook should take: it can run on every rebuild without changing anything the second time.

The same shape covers preparing a file a mount will read, when the mount needs the file to exist rather than be empty:

.devcontainer/lifecycle/initialize.d/20-prepare-aws-config.sh
#!/usr/bin/env bash
set -euo pipefail

# The AWS config is mounted at ~/.aws/config; the mount creates the file if
# it is missing, so the hook only supplies the container defaults once.
[ -f .devcontainer/aws/config ] || cp .devcontainer/aws/config.example .devcontainer/aws/config

The guard makes the copy a one-time seed: later edits to the config survive every subsequent initialize run.