Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitef8a4ea

Browse files
committed
update devcontainers
1 parentba7a36c commitef8a4ea

File tree

2 files changed

+251
-47
lines changed

2 files changed

+251
-47
lines changed

‎docs/admin/templates/extending-templates/advanced-dev-containers.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ module "git-clone-backend" {
5656

5757
Each dev container will appear as a separate agent in the Coder UI, allowing developers to connect to different environments within the same workspace.
5858

59+
##Add a Personal devcontainer.json alongside a repository-specific one
60+
61+
Keep a canonical`devcontainer.json` in the repo, then let each developer add an
62+
untracked`devcontainer.local.json` (or another file referenced via`"extends"`).
63+
64+
```jsonc
65+
// devcontainer.local.json (ignored by Git)
66+
{
67+
"extends":"./devcontainer.json",
68+
"features": {
69+
"ghcr.io/devcontainers/features/node:1": {"version":"20" }
70+
},
71+
"postStartCommand":"npm i -g tldr"
72+
}
73+
```
74+
5975
##Conditional startup with user control
6076

6177
Allow users to selectively enable dev containers:
Lines changed: 235 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
11
#Configure a template for dev containers
22

3-
To enable dev containers in workspaces, configure your template with the dev containers
3+
Dev containers provide a consistent, reproducible development environment using the
4+
[Development Containers specification](https://containers.dev/).
5+
Coder's dev container support allows developers to work in fully configured environments with their preferred tools and extensions.
6+
7+
To enable dev containers in workspaces,[configure your template](../creating-templates.md) with the dev containers
48
modules and configurations outlined in this doc.
59

10+
##Why use dev containers
11+
12+
Dev containers improve consistency across environments by letting developers define their development setup.
13+
When integrated with Coder templates, they provide:
14+
15+
-**Project-specific environments**: Each repository can define its own tools, extensions, and configuration.
16+
-**Zero setup time**: Developers get fully configured environments without manual installation.
17+
-**Consistency across teams**: Everyone works in identical environments regardless of their local machine.
18+
-**Version control**: Development environment changes are tracked alongside code changes.
19+
20+
##Prerequisites
21+
22+
Dev containers require Docker to build and run containers.
23+
Ensure your workspace infrastructure has Docker configured with container creation permissions and sufficient resources.
24+
25+
To confirm that Docker is configured correctly, create a test workspace and confirm that`docker ps` runs.
26+
If it doesn't, follow the steps in[Docker in workspaces](./docker-in-workspaces.md).
27+
28+
##Enable Dev Containers Integration
29+
30+
To enable the dev containers integration in your workspace, add the`CODER_AGENT_DEVCONTAINERS_ENABLE` environment variable to your existing`coder_agent` block:
31+
32+
```terraform
33+
env = {
34+
CODER_AGENT_DEVCONTAINERS_ENABLE = "true"
35+
# existing variables ...
36+
}
37+
```
38+
39+
This environment variable is required for the Coder agent to detect and manage dev containers.
40+
Without it, the agent will not attempt to start or connect to dev containers even if the
41+
`coder_devcontainer` resource is defined.
42+
643
##Install the Dev Containers CLI
744

845
Use the
946
[devcontainers-cli](https://registry.coder.com/modules/devcontainers-cli) module
10-
toensure the`@devcontainers/cli` is installed in your workspace:
47+
toinstall`@devcontainers/cli` in your workspace:
1148

1249
```terraform
1350
module "devcontainers-cli" {
@@ -17,7 +54,44 @@ module "devcontainers-cli" {
1754
}
1855
```
1956

20-
Alternatively, install the devcontainer CLI manually in your base image.
57+
Alternatively, install the devcontainer CLI manually in your base image:
58+
59+
```shell
60+
RUN npm install -g @devcontainers/cli
61+
```
62+
63+
##Define the dev container resource
64+
65+
Point the resource at the folder that contains`devcontainer.json`:
66+
67+
```terraform
68+
resource "coder_devcontainer" "project" {
69+
count = data.coder_workspace.me.start_count
70+
agent_id = coder_agent.dev.id
71+
workspace_folder = "/home/coder/project"
72+
}
73+
```
74+
75+
##Clone the repository
76+
77+
This step is optional, but it ensures that the project is present before the dev container starts.
78+
79+
```terraform
80+
module "git_clone" {
81+
count = data.coder_workspace.me.start_count
82+
source = "dev.registry.coder.com/modules/git-clone/coder"
83+
agent_id = coder_agent.dev.id
84+
url = "https://github.com/example/project.git"
85+
path = "/home/coder/project"
86+
}
87+
88+
resource "coder_devcontainer" "project" {
89+
count = data.coder_workspace.me.start_count
90+
agent_id = coder_agent.dev.id
91+
workspace_folder = module.git_clone[0].path
92+
depends_on = [module.git_clone]
93+
}
94+
```
2195

2296
##Configure Automatic Dev Container Startup
2397

@@ -34,46 +108,67 @@ resource "coder_devcontainer" "my-repository" {
34108
}
35109
```
36110

37-
>[!NOTE]
38-
>
39-
>The`workspace_folder` attribute must specify the location of the dev
40-
>container's workspace and should point to a valid project folder containing a
41-
>`devcontainer.json` file.
111+
The`workspace_folder` attribute must specify the location of the dev container's workspace and should point to a
112+
valid project folder that contains a`devcontainer.json` file.
42113

43-
<!-- nolint:MD028/no-blanks-blockquote-->
114+
##Dev container features
44115

45-
>[!TIP]
46-
>
47-
>Consider using the[`git-clone`](https://registry.coder.com/modules/git-clone)
48-
>module to ensure your repository is cloned into the workspace folder and ready
49-
>for automatic startup.
116+
Enhance your dev container experience with additional features.
117+
For more advanced use cases, consult the[advanced dev containers doc](./advanced-dev-containers.md).
50118

51-
##Enable Dev Containers Integration
119+
###Custom applications
120+
121+
```jsonc
122+
{
123+
"customizations": {
124+
"coder": {
125+
"apps": {
126+
"flask-app": {
127+
"command":"python app.py",
128+
"icon":"/icon/flask.svg",
129+
"subdomain":true,
130+
"healthcheck": {
131+
"url":"http://localhost:5000/health",
132+
"interval":10,
133+
"threshold":10
134+
}
135+
}
136+
}
137+
}
138+
}
139+
}
140+
```
141+
142+
###Agent naming
143+
144+
Coder names dev container agents in this order:
52145

53-
To enable the dev containers integration in your workspace, you must set the
54-
`CODER_AGENT_DEVCONTAINERS_ENABLE` environment variable to`true` in your
55-
workspace container:
146+
1.`customizations.coder.agent.name` in`devcontainer.json`
147+
2.`name` in`devcontainer.json`
148+
3. Directory name that contains the config
149+
4.`devcontainer` (default)
150+
151+
###Multiple dev containers
56152

57153
```terraform
58-
resource "docker_container" "workspace" {
59-
count = data.coder_workspace.me.start_count
60-
image = "codercom/oss-dogfood:latest"
61-
env = [
62-
"CODER_AGENT_DEVCONTAINERS_ENABLE=true",
63-
# ... Other environment variables.
64-
]
65-
# ... Other container configuration.
154+
resource "coder_devcontainer" "frontend" {
155+
count = data.coder_workspace.me.start_count
156+
agent_id = coder_agent.dev.id
157+
workspace_folder = "/home/coder/frontend"
158+
}
159+
160+
resource "coder_devcontainer" "backend" {
161+
count = data.coder_workspace.me.start_count
162+
agent_id = coder_agent.dev.id
163+
workspace_folder = "/home/coder/backend"
66164
}
67165
```
68166

69-
This environment variable is required for the Coder agent to detect and manage
70-
dev containers. Without it, the agent will not attempt to start or connect to
71-
dev containers even if the`coder_devcontainer` resource is defined.
167+
##Complete Template Examples
72168

73-
##Complete Template Example
169+
You can test the Coder dev container integration and features with these starter templates.
74170

75-
Here's a simplified template example that enables the dev containers
76-
integration:
171+
<details><summary>Docker-based template (privileged)</summary>
77172

78173
```terraform
79174
terraform {
@@ -83,44 +178,137 @@ terraform {
83178
}
84179
}
85180
86-
provider "coder" {}
87181
data "coder_workspace" "me" {}
88182
data "coder_workspace_owner" "me" {}
89183
90184
resource "coder_agent" "dev" {
91-
arch = "amd64"
92-
os = "linux"
185+
os = "linux"
186+
arch = "amd64"
187+
env = { CODER_AGENT_DEVCONTAINERS_ENABLE = "true" }
188+
93189
startup_script_behavior = "blocking"
94-
startup_script = "sudo service docker start"
95-
shutdown_script = "sudo service docker stop"
96-
# ...
190+
startup_script = "sudo service docker start"
191+
shutdown_script = "sudo service docker stop"
97192
}
98193
99-
module "devcontainers-cli" {
194+
module "devcontainers_cli" {
100195
count = data.coder_workspace.me.start_count
101196
source = "dev.registry.coder.com/modules/devcontainers-cli/coder"
102197
agent_id = coder_agent.dev.id
103198
}
104199
105-
resource "coder_devcontainer" "my-repository" {
200+
module "git_clone" {
201+
count = data.coder_workspace.me.start_count
202+
source = "dev.registry.coder.com/modules/git-clone/coder"
203+
agent_id = coder_agent.dev.id
204+
url = "https://github.com/example/project.git"
205+
path = "/home/coder/project"
206+
}
207+
208+
resource "coder_devcontainer" "project" {
106209
count = data.coder_workspace.me.start_count
107210
agent_id = coder_agent.dev.id
108-
workspace_folder = "/home/coder/my-repository"
211+
workspace_folder = module.git_clone[0].path
212+
depends_on = [module.git_clone]
109213
}
110214
111215
resource "docker_container" "workspace" {
216+
count = data.coder_workspace.me.start_count
217+
image = "codercom/enterprise-base:ubuntu"
218+
name = "coder-$ta.coder_workspace_owner.me.name}-$ta.coder_workspace.me.name}"
219+
privileged = true # or mount /var/run/docker.sock
220+
}
221+
```
222+
223+
</details>
224+
225+
<details><summary>Kubernetes-based template (Sysbox runtime)</summary>
226+
227+
```terraform
228+
terraform {
229+
required_providers {
230+
coder = { source = "coder/coder" }
231+
kubernetes = { source = "hashicorp/kubernetes" }
232+
}
233+
}
234+
235+
data "coder_workspace" "me" {}
236+
data "coder_workspace_owner" "me" {}
237+
238+
resource "coder_agent" "dev" {
239+
os = "linux"
240+
arch = "amd64"
241+
env = { CODER_AGENT_DEVCONTAINERS_ENABLE = "true" }
242+
243+
startup_script_behavior = "blocking"
244+
startup_script = "sudo service docker start"
245+
}
246+
247+
module "devcontainers_cli" {
248+
count = data.coder_workspace.me.start_count
249+
source = "dev.registry.coder.com/modules/devcontainers-cli/coder"
250+
agent_id = coder_agent.dev.id
251+
}
252+
253+
module "git_clone" {
254+
count = data.coder_workspace.me.start_count
255+
source = "dev.registry.coder.com/modules/git-clone/coder"
256+
agent_id = coder_agent.dev.id
257+
url = "https://github.com/example/project.git"
258+
path = "/home/coder/project"
259+
}
260+
261+
resource "coder_devcontainer" "project" {
262+
count = data.coder_workspace.me.start_count
263+
agent_id = coder_agent.dev.id
264+
workspace_folder = module.git_clone[0].path
265+
depends_on = [module.git_clone]
266+
}
267+
268+
resource "kubernetes_pod" "workspace" {
112269
count = data.coder_workspace.me.start_count
113-
image = "codercom/oss-dogfood:latest"
114-
env = [
115-
"CODER_AGENT_DEVCONTAINERS_ENABLE=true",
116-
# ... Other environment variables.
117-
]
118-
# ... Other container configuration.
270+
271+
metadata {
272+
name = "coder-$ta.coder_workspace_owner.me.name}-$ta.coder_workspace.me.name}"
273+
namespace = "coder-workspaces"
274+
}
275+
276+
spec {
277+
container {
278+
name = "dev"
279+
image = "codercom/enterprise-base:ubuntu"
280+
281+
security_context { privileged = true } # or use Sysbox / rootless
282+
env { name = "CODER_AGENT_TOKEN" value = coder_agent.dev.token }
283+
}
284+
}
119285
}
120286
```
121287

288+
</detail>
289+
290+
##Troubleshoot common issues
291+
292+
###Dev container does not start
293+
294+
1.`CODER_AGENT_DEVCONTAINERS_ENABLE=true` missing.
295+
1. Docker daemon not running inside the workspace.
296+
1.`devcontainer.json` missing or mislocated.
297+
1. Build errors: check agent logs.
298+
299+
###Permission errors
300+
301+
- Docker socket not mounted or user lacks access.
302+
- Workspace not`privileged` and no rootless runtime.
303+
304+
###Slow builds
305+
306+
- Allocate more CPU/RAM.
307+
- Use image caching or pre-build common images.
308+
122309
##Next Steps
123310

311+
-[Advanced dev containers](./advanced-dev-containers.md)
124312
-[Dev Containers Integration](../../../user-guides/devcontainers/index.md)
125313
-[Working with Dev Containers](../../../user-guides/devcontainers/working-with-dev-containers.md)
126314
-[Troubleshooting Dev Containers](../../../user-guides/devcontainers/troubleshooting-dev-containers.md)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp