Cloud Development with Codespaces
4 learning objectives · 5 key terms · 4 commands · 1 diagrams · 5 quiz questions
Cloud Development with Codespaces
Automation & SecurityWhat you'll be able to do
- Launch a ready-to-code environment from any branch or pull request
- Define reproducible environments with devcontainer.json
- Prebuild images so new environments start in seconds
- Control costs by stopping idle codespaces and choosing sizes
The idea, in plain language
Codespaces are full VS Code development environments running in the cloud, launched from a branch, a pull request, or a template — dependencies installed, ports forwarded, extensions present. They matter because onboarding collapses from a day of setup to a clicked link, and every contributor works in an identical, disposable machine.
Reproducibility comes from devcontainer.json: base image, features (languages, tools), extensions, forwarded ports, and setup commands, all versioned beside the code. Prebuilds compile that definition ahead of time so new codespaces open in seconds, and per-user dotfiles plus secrets follow individuals without entering the repository. Costs accrue per minute of compute plus storage, so stopping (not just closing the tab) is the habit that keeps bills boring.
Key terms
- codespace
- A cloud-hosted VS Code environment attached to a repository and branch.
- devcontainer.json
- The versioned definition of the environment: image, features, ports, setup.
- prebuild
- A precompiled environment snapshot that makes new codespaces start in seconds.
- port forwarding
- Exposing the codespace localhost to your browser for running apps.
- dotfiles
- Personal shell/editor configuration applied to your codespaces.
How it works, step by step
- 1Launch from context
The Code dropdown, a pull request, or gh codespace create — always attached to a specific branch, so work starts reviewable.
createbashgh codespace create --repo ada/app --branch feature-x
- 2Define the environment
.devcontainer/devcontainer.json names the image, adds features (node, python, docker), forwards ports, and runs setup commands on create.
devcontainerbash{ "image": "mcr.microsoft.com/devcontainers/base:ubuntu", "features": { "ghcr.io/devcontainers/features/node:1": {} }, "forwardPorts": [3000], "postCreateCommand": "npm ci" } - 3Prebuild for speed
Prebuild configurations compile the definition on push, so contributors open warm environments instead of waiting on installs.
lifecyclebashgh codespace ports # inspect forwarded ports gh codespace stop # stop billing compute
- 4Stop, don’t just close
Closing the tab leaves compute billing; stop or delete codespaces when done, and pick the smallest machine type that fits.
costsbashgh codespace list gh codespace delete --all --force # careful: unpushed work dies here
Commands worth memorising
Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.
gh codespace create --repo ada/app --branch feature-x
Created codespace delightful-garbanzo (4 cores, 8GB)
Starts attached to the branch, so commits land where reviewers expect.
gh codespace listNAME STATE REPO delightful-garbanzo Active ada/app
Audit what is running (and billing) before the invoice does.
gh codespace code --web(browser tab opens on the cloud environment)
Choose browser or desktop VS Code per preference; the machine is the same.
gh codespace stop
✓ Codespace stopped
Compute stops; storage persists until deletion.
Workflow diagrams
| Stage | What happens | Who pays attention |
|---|---|---|
| Definition | devcontainer.json versioned with code | Maintainers, once |
| Prebuild | Image compiled on push | Maintainers, configured |
| Create | Warm environment in seconds | Every contributor |
| Stop/Delete | Compute ends; storage until deleted | Everyone, always |
Definition is versioned; prebuilds make creation instant; stopping ends billing.
- Version the environment definition beside the code.
- Prebuild so creation takes seconds.
- Create per branch or pull request; stop when done.
Worked example
Onboarding a contributor onto a Node API with zero local setup.
# maintainer, once: # .devcontainer/devcontainer.json with node feature + postCreateCommand: npm ci # contributor, day one: gh codespace create --repo ada/api --branch main # browser opens: dependencies installed, port 3000 forwarded, tests green
Contributor runs the test suite within minutes of accepting the invite — no local installs, no version drift.
Where you'll use it
Common mistakes
- Closing the tab instead of stopping — compute keeps billing.
- Deleting codespaces with unpushed commits still inside.
- Oversized machine types for text-editing workloads.
- Secrets in devcontainer.json instead of personal secrets stores.
Best practices
- Version devcontainer.json with the code it serves.
- Prebuild on main so creation takes seconds.
- Stop or delete environments when done; audit with gh codespace list.
- Keep personal config in dotfiles, project config in the repository.
- Right-size machines; upgrade only measured workloads.
Hands-on exercise
Define and use a codespace
Take a repository from local-only to cloud-ready.
- Add .devcontainer/devcontainer.json with the project runtime and a postCreateCommand installing dependencies.
- Create a codespace on a branch and confirm the app runs with forwarded ports.
- Push a change from inside the codespace and open it as a pull request.
- Configure (or simulate) a prebuild and note the creation-time difference.
- List, stop, and delete the codespace, confirming no billable compute remains.
💡 Hint: The VS Code Ports panel shows forwarded ports and their visibility (private vs public).
Done when: A branch-tested change developed entirely in a stopped-afterwards cloud environment.
Checkpoint quiz
0 of 5 answered · 0 correct
- Multiple choiceQ1
What does devcontainer.json define?
- Multiple choiceQ2
Closing the browser tab on a codespace does what to billing?
- True or falseQ3
True or false: prebuilds compile the environment ahead of time so new codespaces start in seconds.
- Multiple choiceQ4
Where do personal secrets for codespaces belong?
- TroubleshootingQ5
Ports forwarded from a codespace are unreachable. What should you check first?
Module summary
- Codespaces are disposable cloud VS Code environments attached to branches.
- devcontainer.json versions the environment beside the code.
- Prebuilds make creation take seconds instead of install-minutes.
- Only stopping (not closing tabs) ends compute billing.
- Personal config lives in dotfiles; secrets never in the repository.
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.