Skip to content

docs: add comprehensive dev containers documentation with examples #18582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 30 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
718e6f9
remove ea feature status
EdwardAngert Jun 23, 2025
ba7a36c
add advanced dev container doc
EdwardAngert Jun 24, 2025
ef8a4ea
update devcontainers
EdwardAngert Jun 25, 2025
f4db3da
new workspace screenshot
EdwardAngert Jun 25, 2025
df07196
consistent and working examples
EdwardAngert Jun 26, 2025
18998b7
edits; prep advanced doc
EdwardAngert Jun 26, 2025
ddc5893
Merge branch 'main' into dev-container-ga
EdwardAngert Jun 26, 2025
048cf5b
advanced features
EdwardAngert Jun 26, 2025
5dc41a5
better examples
EdwardAngert Jun 26, 2025
be23e85
init user-facing update
EdwardAngert Jun 26, 2025
ec77a25
remove coder_agent_devcontainers_enable requirement
EdwardAngert Jun 27, 2025
06d714c
update features; add personal devcontainer file
EdwardAngert Jun 27, 2025
8f392c0
update troubleshooting
EdwardAngert Jun 27, 2025
2fe5291
add comparison doc; crosslink
EdwardAngert Jun 27, 2025
33d3eed
shorten title
EdwardAngert Jun 27, 2025
747822e
clarify envbuilder doc titles
EdwardAngert Jun 27, 2025
d9f818c
update envbuilder doc with dev container integration consideration
EdwardAngert Jun 27, 2025
c10eb7e
update envbuilder add-devcontainer with dev container integration links
EdwardAngert Jun 27, 2025
3f8970e
envbuilder tweaks
EdwardAngert Jun 27, 2025
0d0a9ba
link fix
EdwardAngert Jun 27, 2025
88e7c0f
tweak to devcontainer.local note
EdwardAngert Jun 27, 2025
8f5e613
ap title case
EdwardAngert Jun 27, 2025
e83d564
leftover ea language
EdwardAngert Jun 27, 2025
03c60bf
update ssh note and example
EdwardAngert Jun 27, 2025
cfcef3c
update example template
EdwardAngert Jun 27, 2025
acdbe4d
Apply suggestions from code review
EdwardAngert Jul 1, 2025
1deae60
Merge branch 'main' into dev-container-ga
EdwardAngert Jul 1, 2025
d3fd3b8
edits from code review
EdwardAngert Jul 1, 2025
6ee9fd4
remove full examples; adjustments from review
EdwardAngert Jul 1, 2025
7b2ba10
Merge branch 'main' into dev-container-ga
EdwardAngert Jul 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
200 changes: 200 additions & 0 deletions docs/admin/templates/extending-templates/advanced-dev-containers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
# Advanced Dev Container Configuration

This page extends [devcontainers.md](./devcontainers.md) with patterns for multiple dev containers,
user-controlled startup, repository selection, and infrastructure tuning.

## Run Multiple Dev Containers

Run independent dev containers in the same workspace so each component appears as its own agent.

In this example, there are three: `frontend`, `backend`, and a `database`:

```terraform
# Clone each repo
module "git_clone_frontend" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/modules/git-clone/coder"
version = "~> 1.0"

agent_id = coder_agent.main.id
url = "https://github.com/your-org/frontend.git"
base_dir = "/home/coder/frontend"
}

module "git_clone_backend" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/modules/git-clone/coder"
version = "~> 1.0"

agent_id = coder_agent.main.id
url = "https://github.com/your-org/backend.git"
base_dir = "/home/coder/backend"
}

module "git_clone_database" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/modules/git-clone/coder"
version = "~> 1.0"

agent_id = coder_agent.main.id
url = "https://github.com/your-org/database.git"
base_dir = "/home/coder/database"
}

# Dev container resources
resource "coder_devcontainer" "frontend" {
count = data.coder_workspace.me.start_count
agent_id = coder_agent.main.id
workspace_folder = "/home/coder/frontend/${module.git_clone_frontend[0].folder_name}"
}

resource "coder_devcontainer" "backend" {
count = data.coder_workspace.me.start_count
agent_id = coder_agent.main.id
workspace_folder = "/home/coder/backend/${module.git_clone_backend[0].folder_name}"
}

resource "coder_devcontainer" "database" {
count = data.coder_workspace.me.start_count
agent_id = coder_agent.main.id
workspace_folder = "/home/coder/database/${module.git_clone_database[0].folder_name}"
}
```

Each dev container appears as a separate agent, so developers can connect to any
component in the workspace.

## Conditional Startup

Use `coder_parameter` booleans to let workspace creators choose which dev containers start automatically,
reducing resource usage for unneeded components:

```terraform
data "coder_parameter" "enable_frontend" {
type = "bool"
name = "Enable frontend container"
default = true
mutable = true
order = 3
}

resource "coder_devcontainer" "frontend" {
count = data.coder_parameter.enable_frontend.value ? data.coder_workspace.me.start_count : 0
agent_id = coder_agent.main.id
workspace_folder = "/home/coder/frontend/${module.git_clone_frontend[0].folder_name}"
}
```

## Repository-selection Patterns

Prompt users to pick a repository or team at workspace creation time and clone the selected repo(s) automatically into the workspace:

### Dropdown selector

```terraform
data "coder_parameter" "project" {
name = "Project"
description = "Choose a project"
type = "string"
mutable = true
order = 1

option { name = "E-commerce FE" value = "https://github.com/org/ecom-fe.git" icon = "/icon/react.svg" }
option { name = "Payment API" value = "https://github.com/org/pay.git" icon = "/icon/nodejs.svg" }
}

module "git_clone_selected" {
count = data.coder_workspace.me.start_count
source = "registry.coder.com/modules/git-clone/coder"
version = "~> 1.0"

agent_id = coder_agent.main.id
url = data.coder_parameter.project.value
base_dir = "/home/coder/project"
}
```

### Team-based selection

```terraform
data "coder_parameter" "team" {
name = "Team"
type = "string"
mutable = true
order = 1

option { name = "Frontend" value = "frontend" icon = "/icon/react.svg" }
option { name = "Backend" value = "backend" icon = "/icon/nodejs.svg" }
}

locals {
repos = {
frontend = ["https://github.com/your-org/web.git"]
backend = ["https://github.com/your-org/api.git"]
}
}

module "git_clone_team" {
count = length(local.repos[data.coder_parameter.team.value]) * data.coder_workspace.me.start_count
source = "registry.coder.com/modules/git-clone/coder"
version = "~> 1.0"

agent_id = coder_agent.main.id
url = local.repos[data.coder_parameter.team.value][count.index]
base_dir = "/home/coder/${replace(basename(url), \".git\", \"\")}"
}
```

## Infrastructure Tuning

Adjust workspace infrastructure to set memory/CPU limits, attach a custom Docker network,
or add persistent volumes—to improve performance and isolation for dev containers:

### Resource limits

```terraform
resource "docker_container" "workspace" {
count = data.coder_workspace.me.start_count
image = "codercom/enterprise-node:ubuntu"

resources {
memory = 4096 # MiB
cpus = 2
memory_swap = 8192
}
}
```

### Custom network

```terraform
resource "docker_network" "dev" {
name = "coder-${data.coder_workspace.me.id}-dev"
}

resource "docker_container" "workspace" {
networks_advanced { name = docker_network.dev.name }
}
```

### Volume caching

```terraform
resource "docker_volume" "node_modules" {
name = "coder-${data.coder_workspace.me.id}-node-modules"
lifecycle { ignore_changes = all }
}

resource "docker_container" "workspace" {
volumes {
container_path = "/home/coder/project/node_modules"
volume_name = docker_volume.node_modules.name
}
}
```

## Troubleshooting

1. Run `docker ps` inside the workspace to ensure Docker is available.
1. Check `/tmp/startup.log` for agent logs.
1. Verify the workspace image includes Node/npm or add the `nodejs` module before the `devcontainers_cli` module.
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Choose an Approach To Dev Containers

Coder supports two independent ways to run Dev Containers inside a workspace.

Both implement the [Dev Container specification](https://containers.dev/), but they differ in how the container is built,
who controls it, and which runtime requirements exist.

Use this page to decide which path fits your project or platform needs.

## Options at a Glance

| Capability / Trait | Dev Containers integration (CLI-based) | Envbuilder Dev Containers |
|------------------------------------------|------------------------------------------|-------------------------------------------|
| Build engine | `@devcontainers/cli` + Docker | Envbuilder transforms the workspace image |
| Runs separate Docker container | Yes (parent workspace + child container) | No (modifies the parent container) |
| Multiple Dev Containers per workspace | Yes | No |
| Rebuild when `devcontainer.json` changes | Yes (auto-prompt) | Limited (requires full workspace rebuild) |
| Docker required in workspace | Yes | No (works in restricted envs) |
| Templates | Standard `devcontainer.json` | Terraform + Envbuilder blocks |
| Suitable for CI / AI agents | Yes. Deterministic, composable | Less ideal. No isolated container |

## When To Choose the Dev Containers Integration

Choose the new integration if:

- Your workspace image can run Docker (DinD, Sysbox, or a mounted Docker socket).
- You need multiple Dev Containers (like `frontend`, `backend`, `db`) in a single workspace.
- Developers should own their environment and rebuild on demand.
- You rely on features such as automatic port forwarding, full SSH into containers, or change-detection prompts.

[Dev Container integration](./devcontainers.md) documentation.

## When To Choose Envbuilder

Envbuilder remains a solid choice when:

- Docker isn’t available or allowed inside the workspace image.
- The platform team wants tight control over container contents via Terraform.
- A single layered environment is sufficient (no need for separate sub-containers).
- You already have Envbuilder templates in production and they meet current needs.

[Envbuilder Dev Container](../managing-templates/devcontainers/add-devcontainer.md#envbuilder-terraform-provider) documentation.

## How To Migrate From Envbuilder to the Dev Containers Integration

1. Ensure the workspace image can run Docker and has sufficient resources:

```shell
docker ps
```

1. Remove any Envbuilder blocks that reference `coder_dev_envbuilder` from the template.
1. Add (or keep) a standard `.devcontainer/` folder with `devcontainer.json` in the repository.
1. Add the `devcontainers-cli` module:

```terraform
module "devcontainers_cli" {
source = "dev.registry.coder.com/modules/devcontainers-cli/coder"
agent_id = coder_agent.main.id
}
```

1. Start a new workspace.
Coder detects and launches the dev container automatically.
1. Verify ports, SSH, and rebuild prompts function as expected.

## Related Reading

- [Dev Containers Integration](./index.md)
- [Troubleshooting Dev Containers](../../../user-guides/devcontainers/troubleshooting-dev-containers.md)
- [Envbuilder on GitHub](https://github.com/coder/envbuilder)
- [Dev Container specification](https://containers.dev/)
Loading
Loading