Skip to content

Instantly share code, notes, and snippets.

@Sharique55
Created June 19, 2025 05:27
Show Gist options
  • Select an option

  • Save Sharique55/06c8666033e56aacb75084e8ba88e609 to your computer and use it in GitHub Desktop.

Select an option

Save Sharique55/06c8666033e56aacb75084e8ba88e609 to your computer and use it in GitHub Desktop.
A practical, step-by-step guide to setting up, customizing, and launching GitHub Codespaces with devcontainer templates, Dockerfile setups, onboarding automation, and best practices. Perfect for supercharging repo onboarding and remote dev productivity!

πŸš€ Ultimate Guide: Creating Your Own GitHub Codespace Configuration

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.


πŸ“„ Description

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!


πŸš€ How to Create Your Own GitHub Codespace Configuration

What is a Codespace?

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.


Core Concepts

1. Dev Container

  • 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.

2. .devcontainer Folder

  • Place all Codespace config files here:
    • devcontainer.json (main config)
    • Dockerfile or docker-compose.yml (for custom setups)
    • Optional setup scripts, env files, and more.

Step-by-Step: Creating a Codespace Config

Step 1: Create the .devcontainer Folder

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

Step 3: (Optional) Add a Custom Dockerfile

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"
}

Step 4: (Optional) Add Setup Scripts

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"

Step 5: Push & Launch!

  • Push to GitHub

  • On GitHub: Click Code > Create codespace on [branch]

  • Codespaces reads your .devcontainer/ config and sets up your custom dev environment automatically.

Best Practices & Tips

  • 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

Example: Python Project Codespace

.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]
}

Prebuilt Templates & Features


Quick Reference Table

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

Troubleshooting

  • Config Not Detected?
    Ensure .devcontainer/ is at repo root and files are named correctly.

  • Build Fails?
    Check your Dockerfile and devcontainer.json syntax. Review Codespace logs.

  • Extensions Missing?
    Add them under customizations.vscode.extensions in devcontainer.json.


Final Thoughts

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.


⭐ Love this Guide? Star and share to help others master GitHub Codespaces!

πŸ‘‰ Follow my GitHub for more gists, tips, and devops resources!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment