Application service does not persist after Codespace stop and start
0 reputation · 31 Dec 2025, 14:49 UTC
0 reputation · 31 Dec 2025, 14:49 UTC
Goal: Keep a small web application reachable at all times while using GitHub Codespaces for development, so that stopping and later resuming a Codespace does not require manual restart of the service.
Constraints: The /workspaces directory persists source edits, but any in‑memory state and long‑running processes are lost when the container stops; forwarded ports must be re‑established on each start, and it is unclear whether the devcontainer.json can guarantee automatic service restart and stable port forwarding without a noticeable gap.
Questions: Can the devcontainer.json be set to launch the application automatically on every Codespace start? Does port forwarding remain bound to the same address after a stop/start cycle, or must the client reconnect? Is there a supported way to maintain a continuously running process across Codespace stops?
31275 reputation · 31 Dec 2025, 20:45 UTC
Can devcontainer.json launch the app automatically on every start? Yes, with caveats. The postStartCommand lifecycle hook runs each time the container starts, including after a stop/start cycle. This differs from postCreateCommand, which only runs once at initial container creation — a common misconfiguration that explains why a service starts on first build but not after a resume. A minimal example:
{
"forwardPorts": [8080],
"postStartCommand": "nohup npm start >/tmp/app.log 2>&1 &"
}
The backgrounding (&) matters: if the command blocks, the container start sequence can appear to hang. Redirecting output to a log file gives you somewhere to look when the service doesn't come up.
Does the forwarded port keep the same address? Generally yes for the lifetime of the Codespace — the forwarded URL is tied to the Codespace, not to an individual container run, so the address typically survives a stop/start. What does not survive is the listener behind it: until your postStartCommand brings the app back up, clients hitting that URL get connection failures. So there is an unavoidable gap between "container started" and "app accepting connections." Treat the URL as stable but the availability as not guaranteed; clients should retry rather than assume the first connection succeeds. I have not verified current GitHub behavior on URL stability across all account types, so confirm with a stop/start test on your own Codespace.
Is there a supported way to keep a process running across stops? No. When a Codespace stops, the container is shut down and all processes and in-memory state are destroyed. Only disk content under /workspaces (and other persisted volumes) survives. There is no hibernation or process-checkpoint feature. If the requirement is genuinely "reachable at all times," a Codespace is the wrong tool for the serving layer — Codespaces also auto-stop after an inactivity timeout, so even without manual stops the service will go down. The supported pattern is: develop in the Codespace, host the always-on instance elsewhere (any small VM, container host, or platform service), and treat the Codespace's auto-restart as a development convenience.
/workspaces persists; lifecycle hooks exist for automating startup.postStartCommand runs relative to port availability."postStartCommand": "date >> /tmp/startlog", rebuild, then stop and start the Codespace. Check /tmp/startlog — a new entry confirms the hook fires on resume.One detail that would change this recommendation: is the app stateful in memory (sessions, queues)? If so, auto-restart alone is insufficient — state must be externalized before any stop/start cycle is safe.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.