Skip to content

Commit 79779e0

Browse files
committed
reorg doc into stages; build to example
1 parent 7abc64c commit 79779e0

File tree

1 file changed

+113
-40
lines changed

1 file changed

+113
-40
lines changed

docs/admin/templates/extending-templates/devcontainers.md

Lines changed: 113 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Dev containers provide consistent, reproducible development environments using t
44
[Development Containers specification](https://containers.dev/).
55
Coder's dev container support allows developers to work in fully configured environments with their preferred tools and extensions.
66

7-
To enable dev containers in workspaces, [configure your template](../creating-templates.md) with the dev containers
7+
To add dev container support to a workspace, [configure your template](../creating-templates.md) with the dev containers
88
modules and configurations outlined in this doc.
99

1010
## Why Use Dev Containers
@@ -15,7 +15,7 @@ the code repository.
1515
When integrated with Coder templates, dev containers provide:
1616

1717
- **Project-specific environments**: Each repository can define its own tools, extensions, and configuration.
18-
- **Zero setup time**: Developers start workspace with fully configured environments without additional installation.
18+
- **Faster start times**: Developers start workspace with fully configured environments without additional installation.
1919
- **Consistency across teams**: Everyone works in identical environments regardless of their local machine.
2020
- **Version-controlled environments**: Development environment changes are tracked alongside code changes.
2121

@@ -42,34 +42,116 @@ to install `devcontainers/cli` in your workspace:
4242

4343
```terraform
4444
module "devcontainers-cli" {
45-
count = data.coder_workspace.me.start_count
46-
source = "registry.coder.com/modules/devcontainers-cli/coder"
45+
count = data.coder_workspace.me.start_count
46+
source = "registry.coder.com/modules/devcontainers-cli/coder"
4747
agent_id = coder_agent.main.id
4848
}
4949
```
5050

51-
Alternatively, install `@devcontainer/cli` manually in your base image:
51+
Alternatively, you can install `@devcontainer/cli` manually in your base image:
5252

5353
```shell
5454
RUN npm install -g @devcontainers/cli
5555
```
5656

57+
## Configure the Agent for Docker Support
58+
59+
Your Coder agent needs proper configuration to support Docker and dev containers:
60+
61+
```terraform
62+
resource "coder_agent" "main" {
63+
os = "linux"
64+
arch = "amd64"
65+
66+
# Ensure Docker starts before the agent considers the workspace ready
67+
startup_script_behavior = "blocking"
68+
startup_script = "sudo service docker start"
69+
70+
# Properly shut down Docker when the workspace stops
71+
shutdown_script = "sudo service docker stop"
72+
}
73+
```
74+
75+
The `blocking` behavior ensures Docker is fully started before the workspace is considered ready, which is critical for dev containers to function correctly.
76+
5777
## Define the Dev Container Resource
5878

59-
If you don't use the [`git-clone`](#clone-the-repository) module, point the resource at the folder that contains `devcontainer.json`:
79+
Use the git-clone module to provide repository access and define the dev container:
80+
81+
```terraform
82+
module "git-clone" {
83+
count = data.coder_workspace.me.start_count
84+
source = "registry.coder.com/modules/git-clone/coder"
85+
agent_id = coder_agent.main.id
86+
url = "https://github.com/example/repository.git"
87+
base_dir = "" # defaults to $HOME
88+
}
89+
90+
resource "coder_devcontainer" "repository" {
91+
count = data.coder_workspace.me.start_count
92+
agent_id = coder_agent.main.id
93+
workspace_folder = module.git-clone[0].repo_dir # Points to the cloned repository
94+
}
95+
```
96+
97+
The `repo_dir` output from the git-clone module provides the path to the cloned repository.
98+
99+
### If you're not using the git-clone module
100+
101+
If you need to point to a pre-existing directory or have another way of provisioning repositories, specify the directory that contains the dev container configuration:
60102

61103
```terraform
62104
resource "coder_devcontainer" "my-repository" {
63-
count = data.coder_workspace.me.start_count
64-
agent_id = coder_agent.main.id
65-
workspace_folder = "/home/coder/project" # or /home/coder/project/.devcontainer
105+
count = data.coder_workspace.me.start_count
106+
agent_id = coder_agent.main.id
107+
workspace_folder = "/home/coder/project" # Path to a folder with devcontainer.json
66108
}
67109
```
68110

69111
> [!NOTE]
70-
> The `workspace_folder` attribute must specify the location of the dev
71-
> container's workspace and should point to a valid project folder containing a
72-
> `devcontainer.json` file.
112+
> The `workspace_folder` attribute must specify the location of the dev container's workspace and should point to a
113+
> valid project directory that contains a `devcontainer.json` file or `.devcontainer` directory.
114+
115+
## Create the Docker Container Resource
116+
117+
Configure the Docker container to run the Coder agent and enable Docker-in-Docker capabilities required for dev containers.
118+
119+
This example uses the `sysbox-runc` runtime for secure container isolation.
120+
If sysbox isn't available in your environment, consult [Docker in workspaces](./docker-in-workspaces.md) for alternatives.
121+
122+
```terraform
123+
resource "docker_container" "workspace" {
124+
count = data.coder_workspace.me.start_count
125+
126+
# Base image - this example uses one with npm pre-installed
127+
image = "codercom/enterprise-node:ubuntu"
128+
129+
# Create a unique container name to avoid conflicts
130+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
131+
132+
# Use sysbox-runc for secure Docker-in-Docker
133+
runtime = "sysbox-runc"
134+
135+
# Start the Coder agent when the container starts
136+
entrypoint = ["sh", "-c", coder_agent.main.init_script]
137+
138+
# Configure agent authentication
139+
env = [
140+
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
141+
"CODER_AGENT_URL=${data.coder_workspace.me.access_url}"
142+
]
143+
}
144+
```
145+
146+
### Agent naming
147+
148+
Coder names dev container agents in this order:
149+
150+
1. `customizations.coder.name` in `devcontainer.json`
151+
1. Project directory name (name of folder containing `devcontainer.json` or `.devcontainer` folder)
152+
1. If the project directory name is already taken, the name is expanded to include the parent folder.
153+
154+
For example, if the path is `/home/coder/some/project` and `project` is taken, then the agent is `some-project`.
73155

74156
## Add Dev Container Features
75157

@@ -78,7 +160,7 @@ For more advanced use cases, consult the [advanced dev containers doc](./advance
78160

79161
### Custom applications
80162

81-
Add apps to the dev container workspace resource for one-click access.
163+
Add apps to the dev container workspace resource for one-click access:
82164

83165
```terraform
84166
"coder": {
@@ -158,27 +240,17 @@ resource "coder_devcontainer" "my-repository" {
158240

159241
</details>
160242

161-
### Agent naming
162-
163-
Coder names dev container agents in this order:
164-
165-
1. `customizations.coder.name` in `devcontainer.json`
166-
1. Project directory name (name of folder containing `devcontainer.json` or `.devcontainer` folder)
167-
1. If the project directory name is already taken, the name is expanded to include the parent folder.
168-
169-
For example, if the path is `/home/coder/some/project` and `project` is taken, then the agent is `some-project`.
170-
171243
### Multiple dev containers
172244

173245
```terraform
174246
resource "coder_devcontainer" "frontend" {
175-
count = data.coder_workspace.me.start_count
176-
agent_id = coder_agent.main.id
247+
count = data.coder_workspace.me.start_count
248+
agent_id = coder_agent.main.id
177249
workspace_folder = "/home/coder/frontend"
178250
}
179251
resource "coder_devcontainer" "backend" {
180-
count = data.coder_workspace.me.start_count
181-
agent_id = coder_agent.main.id
252+
count = data.coder_workspace.me.start_count
253+
agent_id = coder_agent.main.id
182254
workspace_folder = "/home/coder/backend"
183255
}
184256
```
@@ -190,7 +262,7 @@ resource "coder_devcontainer" "backend" {
190262
```terraform
191263
terraform {
192264
required_providers {
193-
coder = { source = "coder/coder" }
265+
coder = { source = "coder/coder" }
194266
docker = { source = "kreuzwerker/docker" }
195267
}
196268
}
@@ -199,43 +271,44 @@ data "coder_workspace" "me" {}
199271
data "coder_workspace_owner" "me" {}
200272
201273
resource "coder_agent" "main" {
202-
os = "linux"
274+
os = "linux"
203275
arch = "amd64"
204276
startup_script_behavior = "blocking"
205-
startup_script = "sudo service docker start"
277+
startup_script = "sudo service docker start"
206278
shutdown_script = "sudo service docker stop"
207279
}
208280
209281
module "devcontainers-cli" {
210-
count = data.coder_workspace.me.start_count
211-
source = "registry.coder.com/modules/devcontainers-cli/coder"
282+
count = data.coder_workspace.me.start_count
283+
source = "registry.coder.com/modules/devcontainers-cli/coder"
212284
agent_id = coder_agent.main.id
213285
}
214286
215287
module "git-clone" {
216-
count = data.coder_workspace.me.start_count
217-
source = "registry.coder.com/modules/git-clone/coder"
288+
count = data.coder_workspace.me.start_count
289+
source = "registry.coder.com/modules/git-clone/coder"
218290
agent_id = coder_agent.main.id
219-
url = "https://github.com/coder/coder.git"
220-
base_dir = "/home/coder"
291+
url = "https://github.com/coder/coder.git"
292+
base_dir = "" # defaults to $HOME
293+
depth = 1 # Use a shallow clone for faster startup
221294
}
222295
223296
resource "coder_devcontainer" "my-repository" {
224-
count = data.coder_workspace.me.start_count
225-
agent_id = coder_agent.main.id
297+
count = data.coder_workspace.me.start_count
298+
agent_id = coder_agent.main.id
226299
workspace_folder = module.git-clone[0].repo_dir
227300
}
228301
229302
resource "docker_container" "workspace" {
230303
count = data.coder_workspace.me.start_count
231304
image = "codercom/enterprise-node:ubuntu"
232-
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
305+
name = "coder-${data.coder_workspace_owner.me.name}-${lower(data.coder_workspace.me.name)}"
233306
runtime = "sysbox-runc"
234307
entrypoint = ["sh", "-c", coder_agent.main.init_script]
235308
env = [
236309
"CODER_AGENT_TOKEN=${coder_agent.main.token}",
237310
"CODER_AGENT_URL=${data.coder_workspace.me.access_url}"
238-
]
311+
]
239312
}
240313
```
241314

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy