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 as a Docker container with Ubuntu.
8
6
9
7
##Before you start
10
8
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/ ) .
9
+ You'll need a computer or cloud computing instance with both[ Docker] ( https://docs.docker.com/get-docker/ ) and[ Coder] ( ../install/index.md ) installed on it.
25
10
26
11
##What's in a template
27
12
@@ -37,22 +22,21 @@ started, or stopped.
37
22
> [ Getting Started Guides] ( https://developer.hashicorp.com/terraform/tutorials ) .
38
23
39
24
Here's a simplified diagram that shows the main parts of the template we'll
40
- create.
25
+ create:
41
26
42
27
![ Template architecture] ( ../images/templates/template-architecture.png )
43
28
44
29
##1. Create template files
45
30
46
31
On your local computer, create a directory for your template and create the
47
- ` Dockerfile ` .
32
+ ` Dockerfile ` . You will upload the files to your Coder instance later.
48
33
49
34
``` sh
50
35
mkdir -p template-tour/build&& cd $_
51
- nano Dockerfile
52
36
```
53
37
54
- You'll enter a simple ` Dockerfile ` that starts with the
55
- [ official Ubuntu image] ( https://hub.docker.com/_/ubuntu/ ) . Inthe editor, enter
38
+ Enter content into a ` Dockerfile ` that starts with the
39
+ [ official Ubuntu image] ( https://hub.docker.com/_/ubuntu/ ) . Inyour editor, enter
56
40
and save the following text in` Dockerfile ` then exit the editor:
57
41
58
42
``` dockerfile
@@ -72,21 +56,17 @@ USER ${USER}
72
56
WORKDIR /home/${USER}
73
57
```
74
58
75
- Notice how ` Dockerfile ` adds a few things to the parent` ubuntu ` image, which
59
+ ` Dockerfile ` adds a few things to the parent` ubuntu ` image, which
76
60
your template needs later:
77
61
78
62
- It installs the` sudo ` and` curl ` packages.
79
63
- It adds a` coder ` user, including a home directory.
80
64
81
65
##2. Set up template providers
82
66
83
- Edit the Terraform file to provision the workspace's resources:
84
-
85
- ``` shell
86
- nano main.tf
87
- ```
67
+ Edit the Terraform` main.tf ` file to provision the workspace's resources.
88
68
89
- We'll start by setting upour providers. At a minimum, we need the` coder `
69
+ Start by setting upthe providers. At a minimum, we need the` coder `
90
70
provider. For this template, we also need the` docker ` provider:
91
71
92
72
``` tf
126
106
[ ` coder_workspace ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/workspace )
127
107
data source provides details about the state of a workspace, such as its name,
128
108
owner, and so on. The data source also lets us know when a workspace is being
129
- started or stopped. We'lltake advantage of this information in later steps to:
109
+ started or stopped. We'lluse this information in later steps to:
130
110
131
111
- Set some environment variables based on the workspace owner.
132
112
- Manage ephemeral and persistent storage.
@@ -140,10 +120,9 @@ runs inside the compute aspect of your workspace, typically a VM or container.
140
120
In our case, it will run in Docker.
141
121
142
122
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.
123
+ needs` curl ` access to the Coder server.
145
124
146
- Add this snippetbelow the last closing` } ` in` main.tf ` to create the agent:
125
+ Add this snippetafter the last closing` } ` in` main.tf ` to create the agent:
147
126
148
127
``` tf
149
128
resource "coder_agent" "main" {
@@ -184,29 +163,26 @@ resource "coder_agent" "main" {
184
163
185
164
Because Docker is running locally in the Coder server, there is no need to
186
165
authenticate` coder_agent ` . But if your` coder_agent ` is running on a remote
187
- host, your templatewould need
166
+ host, your templatewill need
188
167
[ authentication credentials] ( ../admin/external-auth.md ) .
189
168
190
169
This template's agent also runs a startup script, sets environment variables,
191
170
and provides metadata.
192
171
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.
172
+ - [ ` startup script ` ] ( https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#startup_script )
173
+ - Installs[ code-server] ( https://coder.com/docs/code-server ) , a browser-based[ VS Code] ( https://code.visualstudio.com/ ) app that runs in the workspace.
198
174
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.
175
+ We'll give users access to code-server through` coder_app ` , later.
176
+
177
+ - [ ` env ` block] ( https://registry.terraform.io/providers/coder/coder/latest/docs/resources/agent#env )
178
+ - Sets environments variables for the workspace.
179
+
180
+ We use the data source from` coder_workspace ` to set the environment variables based on the workspace's owner. This way, the owner can make git commits immediately without any manual configuration.
181
+
182
+ - [ ` metadata ` ] ( ../admin/templates/extending-templates/agent-metadata.md ) blocks
183
+ - Your template can use metadata to show information to the workspace owner Coder displays this metadata in the Coder dashboard.
205
184
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.
185
+ Our template has` metadata ` blocks for CPU and RAM usage.
210
186
211
187
##4. coder_app
212
188
@@ -220,10 +196,7 @@ This is commonly used for
220
196
[ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) such as
221
197
[ code-server] ( https://coder.com/docs/code-server ) , RStudio, and JupyterLab.
222
198
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.
199
+ We installed code-server in the` startup_script ` argument. To add code-server to the workspace, make it available from a workspace with a` coder_app ` resource. See[ web IDEs] ( ../user-guides/workspace-access/web-ides.md ) for more examples.
227
200
228
201
``` tf
229
202
resource "coder_app" "code-server" {