Home Coding & Scripting Understanding Dev Containers in GitHub Codespaces

Understanding Dev Containers in GitHub Codespaces

237
0

⭐ Introduction

GitHub Codespaces lets us develop entirely in the cloud—but Dev Containers are what make that experience powerful, consistent, and customizable.

A Dev Container tells Codespaces:

“Create my development environment exactly like this every time.”

This ensures that all developers (including future you!) get the same tools, versions, extensions, and settings, without installing anything locally.

If you’re new to Codespaces, make sure to read the prerequisite post first:

📌 Prerequisite: Getting Started with GitHub Codespaces

Once you know how to open a Codespace, this post teaches you how to customize it properly using Dev Containers.


🧠 What Is a Dev Container?

A Dev Container is a development environment defined by configuration files inside a folder called:

.devcontainer/
    devcontainer.json

This folder tells GitHub Codespaces:

  • Which operating system image to use
  • Which tools to install (Node, Python, Java, Docker, Terraform, Azure CLI, etc.)
  • Which VS Code extensions to auto-install
  • What commands to run after creation
  • What ports to forward
  • Whether to use a Dockerfile
  • Memory, CPU, and environment variables

It turns our project into a fully reproducible cloud development environment.


🟩 Do You Need a Dev Container?

Short answer: No.

Any GitHub repo can open in a Codespace with zero configuration.

Long answer: It depends.

Codespaces provides a good default environment, but we should use a Dev Container if our project needs:

  • A specific version of Node, Python, Java, .NET
  • Tools like Docker, kubectl, Helm, Terraform
  • Cloud CLI tools (AWS CLI, Azure CLI, GCP CLI)
  • Auto-installed VS Code extensions
  • Clean environment setup for students or teams

For anything beyond a simple project, a Dev Container removes pain and ensures consistency.


🚀 How to Create a Dev Container

1. Open our repository in a Codespace

(From the Getting Started post.)

2. Open Command Palette

Ctrl+Shift+P

3. Search for:

Dev Containers: Add Dev Container Configuration

4. Choose a template

VS Code offers many presets like:

  • Node.js
  • Python
  • Java
  • .NET
  • Docker
  • Azure CLI
  • Terraform
  • Rust
  • Go
  • PHP

5. VS Code creates a .devcontainer folder automatically

That’s it. Your environment is now fully configurable.


📁 Minimal Dev Container Example (For Beginners)

This is the simplest possible devcontainer.json:

{
  "name": "Basic Dev Container",
  "image": "mcr.microsoft.com/devcontainers/universal:2",
  "customizations": {
    "vscode": {
      "extensions": [
        "GitHub.copilot",
        "eamodio.gitlens"
      ]
    }
  }
}

This gives:

  • A powerful universal Ubuntu image
  • GitHub Copilot
  • GitLens
  • Zero complexity

Perfect for students.


🧩 Useful Dev Container Examples (Common Stacks)


1️⃣ Node.js

{
  "name": "Node Dev",
  "image": "mcr.microsoft.com/devcontainers/javascript-node:0-20",
  "postCreateCommand": "npm install",
  "customizations": {
    "vscode": {
      "extensions": [
        "dbaeumer.vscode-eslint",
        "esbenp.prettier-vscode"
      ]
    }
  }
}

2️⃣ Python

{
  "name": "Python",
  "image": "mcr.microsoft.com/devcontainers/python:3.12",
  "customizations": {
    "vscode": {
      "extensions": [
        "ms-python.python",
        "ms-python.black-formatter"
      ]
    }
  }
}

3️⃣ Docker-based (Docker-in-Docker)

{
  "name": "Docker Workspace",
  "image": "mcr.microsoft.com/devcontainers/base:ubuntu",
  "features": {
    "docker-in-docker": "latest"
  }
}

4️⃣ Azure Cloud Development

{
  "name": "Azure Dev",
  "image": "mcr.microsoft.com/devcontainers/universal:2",
  "features": {
    "azure-cli": "latest",
    "terraform": "latest"
  }
}

5️⃣ AWS Cloud Development

{
  "name": "AWS Dev",
  "image": "mcr.microsoft.com/devcontainers/universal:2",
  "features": {
    "aws-cli": "latest"
  }
}

🔁 Rebuilding Your Dev Container (When & How)

After you edit your .devcontainer/devcontainer.json file, Codespaces does not automatically apply the new settings.
You must rebuild the container so GitHub can recreate the environment with your updated configuration.

⭐ When do you need to rebuild?

You must rebuild if you change:

  • The base image
  • Features
  • Extensions
  • Environment variables
  • Ports
  • Post-create commands
  • Dockerfile content

Basically, any change to the development environment requires a rebuild.


🔧 How to rebuild the container

You trigger the rebuild from VS Code, not from the terminal.

Steps:

  1. Open your Codespace (browser or desktop VS Code)
  2. Press Ctrl + Shift + P
  3. Search for: Dev Containers: Rebuild Container
  4. Select it
  5. Wait for Codespaces to rebuild your environment

Your Codespace will restart with the updated configuration.


💡 Pro Tip

VS Code automatically shows a notification like:

“Configuration changes detected. Rebuild required.”

You can click Rebuild directly from the notification.


🎯 When to Use Dev Containers

Use dev containers if:

✔ Your project uses specific toolchains
✔ You want consistent environments for all contributors
✔ You teach coding or run workshops
✔ You often switch machines
✔ You want a clean, fresh environment for every task
✔ You use Codespaces frequently

Avoid dev containers only if:

❌ You develop offline
❌ Your project is extremely small and needs nothing special


🎉 Conclusion

A Dev Container is one of the most powerful features of GitHub Codespaces.
It ensures every developer gets the same setup, tools, extensions, and environment—automatically.

With just a single file (devcontainer.json), you can transform your project into a fully consistent, cloud-ready development environment.

If you’ve already gone through the Getting Started with GitHub Codespaces post, this guide is the next best step toward mastering cloud-based development.

Previous articleGetting Started with GitHub Codespaces
Next articleUndoing Changes in Git: restore, reset, revert
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here