A practical step-by-step guide to setting up, customizing, and launching GitHub Codespaces for any project! Perfect for onboarding, productivity, and cloud-powered dev environments.
This gist gives you a hands-on, beginner-to-advanced walkthrough for setting up your own GitHub Codespace configuration. Includes: core concepts, devcontainer templates, real-world code snippets, troubleshooting, and pro tips to make your repo stand outβand onboard anyone in minutes!
A GitHub Codespace is a fully-configured, cloud-based development environment (VS Code in the browser or desktop) defined by your repositoryβs code. It comes with pre-installed dependencies, a customizable dev container, and instant onboarding for anyone who opens your repo.
- At the heart of Codespaces is a dev container: a Docker-based, reproducible environment.
- Everything is defined by files in a
.devcontainer/folder in your repo.
- Place all Codespace config files here:
devcontainer.json(main config)Dockerfileordocker-compose.yml(for custom setups)- Optional setup scripts, env files, and more.
your-repo/ β βββ .devcontainer/ βββ devcontainer.json βββ Dockerfile # (optional) βββ post-create.sh # (optional)
---
### **Step 2: Add a `devcontainer.json` File**
This file tells Codespaces how to build/configure your environment.
**Minimal Example: Using a Prebuilt Image**
```json
{
"name": "My Codespace",
"image": "mcr.microsoft.com/devcontainers/javascript-node:20",
"features": {
"ghcr.io/devcontainers/features/docker-in-docker:2": {}
},
"customizations": {
"vscode": {
"extensions": [
"dbaeumer.vscode-eslint",
"esbenp.prettier-vscode"
]
}
},
"postCreateCommand": "npm install",
"forwardPorts": [3000, 9229]
}
Breakdown:
-
name: Display name for your codespace
-
image: Base image (see devcontainer images)
-
features: Drop-in extra tools (e.g., Docker-in-Docker)
-
customizations.vscode.extensions: VS Code extensions to pre-install
-
postCreateCommand: Runs after creation (e.g., npm install)
-
forwardPorts: Forward ports automatically for web servers/debuggers
For fully custom setups:
.devcontainer/Dockerfile:
FROM mcr.microsoft.com/devcontainers/base:ubuntu-22.04
# Install Node.js and Git
RUN apt-get update && \
apt-get install -y nodejs npm git
# Set up your app dependencies
WORKDIR /workspace
COPY package*.json ./
RUN npm install
Reference it in devcontainer.json:
{
"name": "Custom Node Dev",
"build": {
"dockerfile": "Dockerfile"
},
"postCreateCommand": "npm run setup"
}
Automate anything you need after the container builds:
.devcontainer/post-create.sh:
#!/bin/bash
# Commands to run after setup
echo "Welcome to your Codespace!"
npm install
And in devcontainer.json:
"postCreateCommand": "./post-create.sh"
-
Push to GitHub
-
On GitHub: Click Code > Create codespace on [branch]
-
Codespaces reads your .devcontainer/ config and sets up your custom dev environment automatically.
-
Start Simple: Use official templates
-
Document Everything: Use comments and README updates for onboarding
-
Forward Required Ports: List them in forwardPorts
-
Automate Onboarding: Use postCreateCommand and scripts
-
Pre-install Extensions: For a consistent coding experience
.devcontainer/devcontainer.json:
{
"name": "Python Starter",
"image": "mcr.microsoft.com/devcontainers/python:3.11",
"customizations": {
"vscode": {
"settings": {
"python.pythonPath": "/usr/local/bin/python"
},
"extensions": [
"ms-python.python",
"ms-python.vscode-pylance"
]
}
},
"postCreateCommand": "pip install -r requirements.txt",
"forwardPorts": [8000]
}
- Use devcontainers/templates for any language
- Use devcontainers/features for one-click tool installs (Docker, GitHub CLI, databases, etc.)
| File | Purpose |
|---|---|
.devcontainer/devcontainer.json |
Main config: image, extensions, scripts, ports |
.devcontainer/Dockerfile |
Custom image build (if default images aren't enough) |
.devcontainer/post-create.sh |
Shell script for automation after creation |
.devcontainer/*.env |
(Optional) Environment variables |
-
Config Not Detected?
Ensure.devcontainer/is at repo root and files are named correctly. -
Build Fails?
Check your Dockerfile anddevcontainer.jsonsyntax. Review Codespace logs. -
Extensions Missing?
Add them undercustomizations.vscode.extensionsindevcontainer.json.
A great Codespace config means anyone can start coding in your repo with zero local setup!
Build on top of the basics with devcontainer features, Docker Compose, multi-service setups, and more as your project grows.
π Follow my GitHub for more gists, tips, and devops resources!