1
- # A guided tour of a template
1
+ # Write a template from scratch
2
2
3
- This guided tour introduces you to the different parts of a Coder template by
4
- showing you how to create a template from scratch.
3
+ A template is a common configuration that you use to deploy workspaces.
5
4
6
- You'll write a simple template that provisions a workspace as a Docker container
7
- with Ubuntu.
5
+ This tutorial teaches you how to create a template that provisions a workspace
6
+ as a Docker container with Ubuntu.
8
7
9
8
## Before you start
10
9
11
- To follow this guide, you'll need:
12
-
13
- - A computer or cloud computing instance with both
14
- [ Docker] ( https://docs.docker.com/get-docker/ ) and [ Coder] ( ../install/index.md )
15
- installed on it.
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.
20
-
21
- - The URL for your Coder instance. If you're running Coder locally, the default
22
- URL is [ http://127.0.0.1:3000 ] ( http://127.0.0.1:3000 ) .
23
-
24
- - A text editor. For this tour, we use [ GNU nano] ( https://nano-editor.org/ ) .
10
+ You'll need a computer or cloud computing instance with both
11
+ [ Docker] ( https://docs.docker.com/get-docker/ ) and [ Coder] ( ../install/index.md )
12
+ installed on it.
25
13
26
14
## What's in a template
27
15
@@ -37,22 +25,21 @@ started, or stopped.
37
25
> [ Getting Started Guides] ( https://developer.hashicorp.com/terraform/tutorials ) .
38
26
39
27
Here's a simplified diagram that shows the main parts of the template we'll
40
- create.
28
+ create:
41
29
42
30
![ Template architecture] ( ../images/templates/template-architecture.png )
43
31
44
32
## 1. Create template files
45
33
46
34
On your local computer, create a directory for your template and create the
47
- ` Dockerfile ` .
35
+ ` Dockerfile ` . You will upload the files to your Coder instance later.
48
36
49
37
``` sh
50
38
mkdir -p template-tour/build && cd $_
51
- nano Dockerfile
52
39
```
53
40
54
- You'll enter a simple ` Dockerfile ` that starts with the
55
- [ official Ubuntu image] ( https://hub.docker.com/_/ubuntu/ ) . In the editor, enter
41
+ Enter content into a ` Dockerfile ` that starts with the
42
+ [ official Ubuntu image] ( https://hub.docker.com/_/ubuntu/ ) . In your editor, enter
56
43
and save the following text in ` Dockerfile ` then exit the editor:
57
44
58
45
``` dockerfile
@@ -72,22 +59,18 @@ USER ${USER}
72
59
WORKDIR /home/${USER}
73
60
```
74
61
75
- Notice how ` Dockerfile ` adds a few things to the parent ` ubuntu ` image, which
76
- your template needs later:
62
+ ` Dockerfile ` adds a few things to the parent ` ubuntu ` image, which your template
63
+ needs later:
77
64
78
65
- It installs the ` sudo ` and ` curl ` packages.
79
66
- It adds a ` coder ` user, including a home directory.
80
67
81
68
## 2. Set up template providers
82
69
83
- Edit the Terraform file to provision the workspace's resources:
84
-
85
- ``` shell
86
- nano main.tf
87
- ```
70
+ Edit the Terraform ` main.tf ` file to provision the workspace's resources.
88
71
89
- We'll start by setting up our providers. At a minimum, we need the ` coder `
90
- provider. For this template, we also need the ` docker ` provider:
72
+ Start by setting up the providers. At a minimum, we need the ` coder ` provider.
73
+ For this template, we also need the ` docker ` provider:
91
74
92
75
``` tf
93
76
terraform {
126
109
[ ` coder_workspace ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace )
127
110
data source provides details about the state of a workspace, such as its name,
128
111
owner, and so on. The data source also lets us know when a workspace is being
129
- started or stopped. We'll take advantage of this information in later steps to:
112
+ started or stopped. We'll use this information in later steps to:
130
113
131
114
- Set some environment variables based on the workspace owner.
132
115
- Manage ephemeral and persistent storage.
@@ -140,10 +123,9 @@ runs inside the compute aspect of your workspace, typically a VM or container.
140
123
In our case, it will run in Docker.
141
124
142
125
You do not need to have any open ports on the compute aspect, but the agent
143
- needs ` curl ` access to the Coder server. Remember that we installed ` curl ` in
144
- ` Dockerfile ` , earlier.
126
+ needs ` curl ` access to the Coder server.
145
127
146
- Add this snippet below the last closing ` } ` in ` main.tf ` to create the agent:
128
+ Add this snippet after the last closing ` } ` in ` main.tf ` to create the agent:
147
129
148
130
``` tf
149
131
resource "coder_agent" "main" {
@@ -184,29 +166,33 @@ resource "coder_agent" "main" {
184
166
185
167
Because Docker is running locally in the Coder server, there is no need to
186
168
authenticate ` coder_agent ` . But if your ` coder_agent ` is running on a remote
187
- host, your template would need
169
+ host, your template will need
188
170
[ authentication credentials] ( ../admin/external-auth.md ) .
189
171
190
172
This template's agent also runs a startup script, sets environment variables,
191
173
and provides metadata.
192
174
193
- The
194
- [ ` startup script ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script )
195
- installs [ code-server] ( https://coder.com/docs/code-server ) , a browser-based
196
- [ VS Code] ( https://code.visualstudio.com/ ) app that runs in the workspace. We'll
197
- give users access to code-server through ` coder_app ` , later.
175
+ - [ ` startup script ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script )
198
176
199
- The
200
- [ ` env ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#env )
201
- block sets environments variables for the workspace. We use the data source from
202
- ` coder_workspace ` to set the environment variables based on the workspace's
203
- owner. This way, the owner can make git commits immediately without any manual
204
- configuration.
177
+ - Installs [ code-server] ( https://coder.com/docs/code-server ) , a browser-based
178
+ [ VS Code] ( https://code.visualstudio.com/ ) app that runs in the workspace.
179
+
180
+ We'll give users access to code-server through ` coder_app ` , later.
181
+
182
+ - [ ` env ` block] ( https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#env )
183
+
184
+ - Sets environments variables for the workspace.
185
+
186
+ We use the data source from ` coder_workspace ` to set the environment
187
+ variables based on the workspace's owner. This way, the owner can make git
188
+ commits immediately without any manual configuration.
205
189
206
- Your template can use metadata to show information to the workspace owner. Coder
207
- displays this metadata in the Coder dashboard. Our template has
208
- [ ` metadata ` ] ( ../admin/templates/extending-templates/agent-metadata.md ) blocks
209
- for CPU and RAM usage.
190
+ - [ ` metadata ` ] ( ../admin/templates/extending-templates/agent-metadata.md ) blocks
191
+
192
+ - Your template can use metadata to show information to the workspace owner
193
+ Coder displays this metadata in the Coder dashboard.
194
+
195
+ Our template has ` metadata ` blocks for CPU and RAM usage.
210
196
211
197
## 4. coder_app
212
198
@@ -220,10 +206,9 @@ This is commonly used for
220
206
[ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) such as
221
207
[ code-server] ( https://coder.com/docs/code-server ) , RStudio, and JupyterLab.
222
208
223
- To install code-server in the workspace, remember that we installed it in the
224
- ` startup_script ` argument in ` coder_agent ` . We make it available from a
225
- workspace with a ` coder_app ` resource. See
226
- [ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) for more examples.
209
+ We installed code-server in the ` startup_script ` argument. To add code-server to
210
+ the workspace, make it available in the workspace with a ` coder_app ` resource.
211
+ See [ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) for more examples:
227
212
228
213
``` tf
229
214
resource "coder_app" "code-server" {
@@ -272,10 +257,9 @@ We do this in 2 parts:
272
257
workspace name change, we use an immutable parameter, like
273
258
` data.coder_workspace.me.id ` .
274
259
275
- You'll see later that we make sure that our Docker container is ephemeral with
276
- the Terraform
260
+ Later, we use the Terraform
277
261
[ count] ( https://developer.hashicorp.com/terraform/language/meta-arguments/count )
278
- meta-argument.
262
+ meta-argument to make sure that our Docker container is ephemeral .
279
263
280
264
``` tf
281
265
resource "docker_volume" "home_volume" {
@@ -345,57 +329,58 @@ Save `main.tf` and exit the editor.
345
329
Now that we've created the files for our template, we can add them to our Coder
346
330
deployment.
347
331
348
- We can do this with the Coder CLI or the Coder dashboard. For this tour , we'll
332
+ We can do this with the Coder CLI or the Coder dashboard. In this example , we'll
349
333
use the Coder CLI.
350
334
351
- First, you'll need to log in to your Coder deployment from the CLI. This is
352
- where you need the URL for your deployment:
335
+ 1 . Log in to your Coder deployment from the CLI. This is where you need the URL
336
+ for your deployment:
353
337
354
- ``` sh
355
- $ coder login https://coder.example.com
356
- Your browser has been opened to visit:
338
+ ``` console
339
+ $ coder login https://coder.example.com
340
+ Your browser has been opened to visit:
357
341
358
- https://coder.example.com/cli-auth
342
+ https://coder.example.com/cli-auth
359
343
360
- > Paste your token here:
361
- ```
344
+ > Paste your token here:
345
+ ```
362
346
363
- In your web browser, enter your credentials:
347
+ 1 . In your web browser, enter your credentials:
364
348
365
- ![ Logging in to your Coder deployment] ( ../images/templates/coder-login-web.png )
349
+ ![ Logging in to your Coder deployment] ( ../images/templates/coder-login-web.png )
366
350
367
- Copy the session token into the clipboard:
351
+ 1 . Copy the session token into the clipboard:
368
352
369
- ![ Logging in to your Coder deployment] ( ../images/templates/coder-session-token.png )
353
+ ![ Logging in to your Coder deployment] ( ../images/templates/coder-session-token.png )
370
354
371
- And paste it into the CLI:
355
+ 1 . Paste it into the CLI:
372
356
373
- ``` sh
374
- > Welcome to Coder, marc! You' re authenticated.
375
- $
376
- ```
357
+ ``` output
358
+ > Welcome to Coder, marc! You're authenticated.
359
+ $
360
+ ```
377
361
378
- Now you can add your template files to your Coder deployment:
362
+ 1 . Add your template files to your Coder deployment:
379
363
380
- ```sh
381
- $ pwd
382
- /home/marc /template-tour
383
- $ coder templates create
384
- > Upload "."? (yes/no) yes
385
- ```
364
+ ``` console
365
+ $ pwd
366
+ /home/docs /template-tour
367
+ $ coder templates create
368
+ > Upload "."? (yes/no) yes
369
+ ```
386
370
387
- The Coder CLI tool gives progress information then prompts you to confirm:
371
+ 1 . The Coder CLI tool gives progress information then prompts you to confirm:
388
372
389
- ```sh
390
- > Confirm create? (yes/no) yes
373
+ ``` console
374
+ > Confirm create? (yes/no) yes
391
375
392
- The template-tour template has been created! Developers can provision a workspace with this template using:
376
+ The template-tour template has been created! Developers can provision a workspace with this template using:
393
377
394
- coder create --template="template-tour" [workspace name]
395
- ```
378
+ coder create --template="template-tour" [workspace name]
379
+ ```
380
+
381
+ 1 . In your web browser, log in to your Coder dashboard, select ** Templates** .
396
382
397
- In your web browser, log in to your Coder dashboard, select **Templates**. Your
398
- template is ready to use for new workspaces.
383
+ Your template is ready to use for new workspaces.
399
384
400
385
![ Your new template, ready to use] ( ../images/templates/template-tour.png )
401
386
0 commit comments