@@ -14,18 +14,13 @@ To follow this guide, you'll need:
14
14
[ Docker] ( https://docs.docker.com/get-docker/ ) and[ Coder] ( ../install/index.md )
15
15
installed on it.
16
16
17
- > When setting up your computer or computing instance, make sure to install
18
- > Docker first, then Coder. Otherwise, you'll need to add the` coder ` user to
19
- > the` docker ` group.
17
+ - When setting up your computer or computing instance, make sure to install Docker first, then Coder. Otherwise, you'll need to add the` coder ` user to the` docker ` group.
20
18
21
19
- The URL for your Coder instance. If you're running Coder locally, the default
22
20
URL is[ http://127.0.0.1:3000 ] ( http://127.0.0.1:3000 ) .
23
21
24
22
- A text editor. For this tour, we use[ GNU nano] ( https://nano-editor.org/ ) .
25
23
26
- > Haven't written Terraform before? Check out Hashicorp's
27
- > [ Getting Started Guides] ( https://developer.hashicorp.com/terraform/tutorials ) .
28
-
29
24
##What's in a template
30
25
31
26
The main part of a Coder template is a[ Terraform] ( https://terraform.io ) ` tf `
@@ -36,6 +31,9 @@ Coder can provision all Terraform modules, resources, and properties. The Coder
36
31
server essentially runs a` terraform apply ` every time a workspace is created,
37
32
started, or stopped.
38
33
34
+ > Haven't written Terraform before? Check out Hashicorp's
35
+ > [ Getting Started Guides] ( https://developer.hashicorp.com/terraform/tutorials ) .
36
+
39
37
Here's a simplified diagram that shows the main parts of the template we'll
40
38
create.
41
39
@@ -47,10 +45,8 @@ On your local computer, create a directory for your template and create the
47
45
` Dockerfile ` .
48
46
49
47
``` sh
50
- mkdir template-tour
51
- cd template-tour
52
- mkdir build
53
- nano build/Dockerfile
48
+ mkdir -p template-tour/build&& cd $_
49
+ nano Dockerfile
54
50
```
55
51
56
52
You'll enter a simple` Dockerfile ` that starts with the
@@ -72,7 +68,6 @@ RUN useradd --groups sudo --no-create-home --shell /bin/bash ${USER} \
72
68
&& chmod 0440 /etc/sudoers.d/${USER}
73
69
USER ${USER}
74
70
WORKDIR /home/${USER}
75
-
76
71
```
77
72
78
73
Notice how` Dockerfile ` adds a few things to the parent` ubuntu ` image, which
@@ -83,7 +78,7 @@ your template needs later:
83
78
84
79
##2. Set up template providers
85
80
86
- Now you can edit the Terraform file, which provisions the workspace's resources.
81
+ Edit the Terraform file to provision the workspace's resources:
87
82
88
83
``` shell
89
84
nano main.tf
@@ -97,31 +92,28 @@ terraform {
97
92
required_providers {
98
93
coder = {
99
94
source = "coder/coder"
100
- version = "~> 0.8.3"
101
95
}
102
96
docker = {
103
97
source = "kreuzwerker/docker"
104
- version = "~> 3.0.1"
105
98
}
106
99
}
107
100
}
108
101
109
- provider "coder" {
102
+ locals {
103
+ username = data.coder_workspace_owner.me.name
110
104
}
111
105
112
- provider "docker " {
106
+ data "coder_provisioner" "me " {
113
107
}
114
108
115
- locals {
116
- username = data.coder_workspace.me.owner
109
+ provider "docker" {
117
110
}
118
111
119
- data "coder_provisioner" "me " {
112
+ provider "coder " {
120
113
}
121
114
122
115
data "coder_workspace" "me" {
123
116
}
124
-
125
117
```
126
118
127
119
Notice that the` provider ` blocks for` coder ` and` docker ` are empty. In a more
132
124
[ ` coder_workspace ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace )
133
125
data source provides details about the state of a workspace, such as its name,
134
126
owner, and so on. The data source also lets us know when a workspace is being
135
- started or stopped. We'll take advantage of this information in later steps to
136
- do these things:
127
+ started or stopped. We'll take advantage of this information in later steps to:
137
128
138
129
- Set some environment variables based on the workspace owner.
139
130
- Manage ephemeral and persistent storage.
@@ -150,26 +141,25 @@ You do not need to have any open ports on the compute aspect, but the agent
150
141
needs` curl ` access to the Coder server. Remember that we installed` curl ` in
151
142
` Dockerfile ` , earlier.
152
143
153
- This snippetcreates the agent:
144
+ Add this snippetbelow the last closing ` } ` in ` main.tf ` to create the agent:
154
145
155
146
``` tf
156
147
resource "coder_agent" "main" {
157
148
arch = data.coder_provisioner.me.arch
158
149
os = "linux"
159
- startup_script_timeout = 180
160
150
startup_script = <<-EOT
161
151
set -e
162
152
163
153
# install and start code-server
164
- curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server --version 4.11.0
154
+ curl -fsSL https://code-server.dev/install.sh | sh -s -- --method=standalone --prefix=/tmp/code-server
165
155
/tmp/code-server/bin/code-server --auth none --port 13337 >/tmp/code-server.log 2>&1 &
166
156
EOT
167
157
168
158
env = {
169
- GIT_AUTHOR_NAME= "${ data.coder_workspace .me.owner}"
170
- GIT_COMMITTER_NAME = "${data.coder_workspace .me.owner }"
171
- GIT_AUTHOR_EMAIL = "${ data.coder_workspace .me.owner_email}"
172
- GIT_COMMITTER_EMAIL = "${data.coder_workspace .me.owner_email }"
159
+ GIT_AUTHOR_NAME = coalesce( data.coder_workspace_owner .me.full_name, data.coder_workspace_owner.me.name)
160
+ GIT_AUTHOR_EMAIL = "${data.coder_workspace_owner .me.email }"
161
+ GIT_COMMITTER_NAME = coalesce( data.coder_workspace_owner .me.full_name, data.coder_workspace_owner.me.name)
162
+ GIT_COMMITTER_EMAIL = "${data.coder_workspace_owner .me.email }"
173
163
}
174
164
175
165
metadata {
@@ -188,11 +178,10 @@ resource "coder_agent" "main" {
188
178
timeout = 1
189
179
}
190
180
}
191
-
192
181
```
193
182
194
183
Because Docker is running locally in the Coder server, there is no need to
195
- authenticate` coder_agent ` . But if your` coder_agent ` were running on a remote
184
+ authenticate` coder_agent ` . But if your` coder_agent ` is running on a remote
196
185
host, your template would need
197
186
[ authentication credentials] ( ../admin/external-auth.md ) .
198
187
@@ -229,7 +218,7 @@ This is commonly used for
229
218
[ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) such as
230
219
[ code-server] ( https://coder.com/docs/code-server ) , RStudio, and JupyterLab.
231
220
232
- To installand code-server in the workspace, remember that we installed it in
221
+ To install code-server in the workspace, remember that we installed it in
233
222
the` startup_script ` argument in` coder_agent ` . We make it available from a
234
223
workspace with a` coder_app ` resource. See
235
224
[ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) for more examples.
@@ -250,11 +239,10 @@ resource "coder_app" "code-server" {
250
239
threshold = 6
251
240
}
252
241
}
253
-
254
242
```
255
243
256
244
You can also use a` coder_app ` resource to link to external apps, such as links
257
- to wikis or cloud consoles.
245
+ to wikis or cloud consoles:
258
246
259
247
``` tf
260
248
resource "coder_app" "coder-server-doc" {
@@ -264,7 +252,6 @@ resource "coder_app" "coder-server-doc" {
264
252
url = "https://coder.com/docs/code-server"
265
253
external = true
266
254
}
267
-
268
255
```
269
256
270
257
##5. Persistent and ephemeral resources
@@ -296,7 +283,6 @@ resource "docker_volume" "home_volume" {
296
283
ignore_changes = all
297
284
}
298
285
}
299
-
300
286
```
301
287
302
288
For details, see
@@ -305,7 +291,7 @@ For details, see
305
291
##6. Set up the Docker container
306
292
307
293
To set up our Docker container, our template has a` docker_image ` resource that
308
- uses` build/Dockerfile ` , which we created earlier.
294
+ uses` build/Dockerfile ` , which we created earlier:
309
295
310
296
``` tf
311
297
resource "docker_image" "main" {
@@ -320,7 +306,6 @@ resource "docker_image" "main" {
320
306
dir_sha1 = sha1(join("", [for f in fileset(path.module, "build/*") : filesha1(f)]))
321
307
}
322
308
}
323
-
324
309
```
325
310
326
311
Our` docker_container ` resource uses` coder_workspace ` ` start_count ` to start
@@ -331,7 +316,7 @@ resource "docker_container" "workspace" {
331
316
count = data.coder_workspace.me.start_count
332
317
image = docker_image.main.name
333
318
# Uses lower() to avoid Docker restriction on container names.
334
- name = "coder-${data.coder_workspace .me.owner }-${lower(data.coder_workspace.me.name)}"
319
+ name = "coder-${data.coder_workspace_owner .me.name }-${lower(data.coder_workspace.me.name)}"
335
320
# Hostname makes the shell more user friendly: coder@my-workspace:~$
336
321
hostname = data.coder_workspace.me.name
337
322
# Use the docker gateway if the access URL is 127.0.0.1
@@ -349,7 +334,6 @@ resource "docker_container" "workspace" {
349
334
read_only = false
350
335
}
351
336
}
352
-
353
337
```
354
338
355
339
##7. Create the template in Coder