GitHub Codespaces devcontainer.json postCreateCommand execution consistency
0 reputation · 14 Jul 2022, 04:57 UTC
0 reputation · 14 Jul 2022, 04:57 UTC
GitHub Codespaces relies on the devcontainer.json configuration to ensure a repeatable development environment. A key component of this setup is the postCreateCommand, which is intended to automate the final steps of environment preparation, such as installing dependencies or configuring local databases.
There is uncertainty regarding the exact trigger conditions for this command when a codespace is transitioned between different states. Specifically, it is unclear if the command executes only during the initial container build or if it triggers upon the resumption of a stopped codespace that has undergone a configuration update.
postCreateCommand run automatically when a codespace is started from a stopped state if the devcontainer.json was modified?34275 reputation · 14 Jul 2022, 07:49 UTC
The postCreateCommand in devcontainer.json executes only during the creation or full rebuild of a container. It does not trigger automatically when a codespace is resumed from a stopped state, even if the devcontainer.json file was modified while the codespace was inactive.
To maintain environment consistency, GitHub Codespaces distinguishes between creating a filesystem and starting a process. The postCreateCommand is tied to the filesystem creation lifecycle.
devcontainer.json. The postCreateCommand will run.postCreateCommand will not run.devcontainer.json does not trigger an automatic rebuild upon start. The changes remain dormant until a manual "Rebuild Container" is initiated.| Trigger Event | postCreateCommand | postStartCommand |
|---|---|---|
| Initial Build | Executes | Executes |
| Full Rebuild | Executes | Executes |
| Resume from Stop | Skipped | Executes |
| Container Restart | Skipped | Executes |
If you are unsure if your command is executing, you can verify the behavior by adding a timestamped log to your configuration:
"postCreateCommand": "echo \"Created at $(date)\" > /tmp/creation_log.txt"
After stopping and resuming your codespace, check /tmp/creation_log.txt. If the timestamp does not update upon resume, the command is behaving as expected. To apply changes made to devcontainer.json, use the Command Palette (Ctrl+Shift+P) and select Codespaces: Rebuild Container.
Diagnostic Note: Please specify if you are using a custom Dockerfile or a pre-built image, as image layer caching can sometimes mask the perceived duration of the creation phase, though it does not skip the postCreateCommand execution itself.
Use comments to ask for clarification. Post a solution as an answer.
34,275 reputation · 14 Jul 2022, 05:07 UTC
To clarify the execution lifecycle: the postCreateCommand is designed for one-time environment setup and executes only during the initial container creation or after a manual Full Rebuild. It does not trigger automatically when a codespace is resumed from a stopped state, even if devcontainer.json has been modified.
If you need a script to run every time the container starts or resumes, you should use postStartCommand instead. This is the critical distinction for tasks like starting a database or refreshing temporary session tokens.
| Trigger | postCreateCommand | postStartCommand |
|---|---|---|
| Initial Build | Yes | Yes |
| Full Rebuild | Yes | Yes |
| Resume/Start | No | Yes |