Using initializeCommand for Host-Side Setup
Some setup must happen on the host before the container exists — generating a .env, verifying a prerequisite, preparing a directory to mount. This page uses initializeCommand, the one lifecycle hook that runs on the host, to do exactly that, and explains what belongs there versus in the container hooks.
Prerequisites
You need a devcontainer config and a host-side setup step.
- A
.devcontainer/devcontainer.jsonyou can edit. - A host command that must run before container creation.
- Awareness that it runs on the host, not in the container.
Step-by-Step Implementation
- Declare a host-side command. It runs before the container is built.
{
"initializeCommand": "bash .devcontainer/init.sh",
"remoteUser": "vscode"
}
- Keep it host-safe — it runs in your host shell, not the container.
# .devcontainer/init.sh — generate a .env the container will mount
[ -f .env ] || cp .env.example .env
- Leave container work to postCreate.
{ "postCreateCommand": "npm ci" }
- Verify it runs on each
devcontainer up.
Common Pitfalls
Misuse comes from putting container work in initializeCommand (or vice versa).
| Symptom | Root Cause | Remediation |
|---|---|---|
| Command can't find container tools | Ran on the host via initializeCommand | Move container work to postCreate |
| .env missing at build | Generated too late in a container hook | Generate it in initializeCommand |
| Runs on every attach unexpectedly | Confused with postAttach | Use initializeCommand for pre-create only |
| Fails on a teammate's OS | Host-shell assumptions | Keep the host command portable |
Conclusion
initializeCommand is the only hook that runs on your host, before the container exists — use it for host-side preparation like generating a .env or a mount directory, and leave everything container-related to onCreate/postCreate. Match the step to where it must run.
FAQ
When should I use initializeCommand instead of postCreateCommand?
Use initializeCommand for work that must happen on the host before the container is built — generating a .env the container will mount, creating a directory, or checking a host prerequisite. Use postCreateCommand for anything that runs inside the container, like installing dependencies.
Does initializeCommand run in the container? No — it runs in your host shell before the container exists. That is its whole purpose. It cannot see container tools or paths, so keep it to host-side preparation and portable across your team's operating systems.
How often does initializeCommand run?
On each devcontainer up / container creation, before the build. It is not a per-attach hook, so use it for setup that should precede building the environment, not for messaging or per-session actions (which belong in postAttachCommand).
Related
- Up to Understanding the DevContainer Specification — the lifecycle model.
- Feature & Lifecycle Hook Sequencing — the full hook order.
- How to Configure devcontainer.json from Scratch — where hooks fit in a config.