Lifecycle scripts allow you to run commands at specific stages of the dev container lifecycle. Commands can be defined as a string (executed via /bin/sh), an array (executed directly without a shell), or an object (to support parallel execution).
Execution Order & Context:
initializeCommand: Runs on the host machine during initialization. Can run multiple times per session.onCreateCommand: Runs inside the container immediately after its first creation. Used for initial setup; cloud services may use this for prebuilding (limited access to user secrets).updateContentCommand: Runs inside the container after onCreateCommand whenever new content is available. Used by cloud services to refresh cached containers.postCreateCommand: Runs inside the container after updateContentCommand and once assigned to a user. Can access user-specific secrets.postStartCommand: Runs inside the container every time it is successfully started.postAttachCommand: Runs inside the container every time a tool successfully attaches.
Important Notes:
- If any lifecycle script fails, subsequent scripts are skipped.
- Use
waitFor (enum) to specify which command tools should wait for before connecting (defaults to updateContentCommand). - Commands execute from the
workspaceFolder.
{
"postAttachCommand": {
"server": "npm start",
"db": ["mysql", "-u", "root", "-p", "my database"]
}
}