|
| 1 | +--- |
| 2 | +title: "Workspace templates" |
| 3 | +description: "Learn how to write a template for creating environments." |
| 4 | +state: beta |
| 5 | +--- |
| 6 | + |
| 7 | +Workspaces as code (WAC) allows you to define and create new environments using |
| 8 | +**workspace templates**. |
| 9 | + |
| 10 | +Workspace templates are written as YAML and have a `.yaml` or `.yml` extension. |
| 11 | +Coder looks for your workspace template at the following path: |
| 12 | + |
| 13 | +```text |
| 14 | +<repository-root>/.coder/coder.yaml |
| 15 | +``` |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## Workspace template sample |
| 20 | + |
| 21 | +The following is a sample workspace template that makes use of all available |
| 22 | +fields. Depending on your use case, you may not need all of the options |
| 23 | +available. |
| 24 | + |
| 25 | +For detailed information on the fields available, see the |
| 26 | +[subsequent sections](#workspace-template-fields) of this article |
| 27 | + |
| 28 | +```yaml |
| 29 | +version: 0.1 |
| 30 | +workspace: |
| 31 | + type: kubernetes |
| 32 | + spec: |
| 33 | + image: index.docker.io/ubuntu:18.04 |
| 34 | + container-based-vm: true |
| 35 | + cpu: 4 |
| 36 | + memory: 16 |
| 37 | + disk: 128 |
| 38 | + gpuCount: 1 |
| 39 | + labels: |
| 40 | + com.coder.custom.hello: "hello" |
| 41 | + com.coder.custom.world: "world" |
| 42 | + tolerations: |
| 43 | + - key: my-key |
| 44 | + operator: Equal |
| 45 | + value: my-value |
| 46 | + effect: NoExecute |
| 47 | + tolerationSeconds: 3600 |
| 48 | + configure: |
| 49 | + - name: "install curl" |
| 50 | + run: | |
| 51 | + apt update |
| 52 | + apt install -y curl |
| 53 | + - name: "install Go binary" |
| 54 | + run: "go install" |
| 55 | + directory: /home/coder/go/src/github.com/my-project |
| 56 | + shell: "bash" |
| 57 | + env: |
| 58 | + GOPATH: /home/coder/go |
| 59 | +``` |
| 60 | +
|
| 61 | +## Workspace template fields |
| 62 | +
|
| 63 | +### version |
| 64 | +
|
| 65 | +The version number of the config file being used. The current version is `0.1`. |
| 66 | + |
| 67 | +### workspace |
| 68 | + |
| 69 | +**Required**. The section containing all configuration information related to |
| 70 | +the environment. |
| 71 | + |
| 72 | +#### workspace.type |
| 73 | + |
| 74 | +**Required**. Determines the type of workspace to be created. Currently, the |
| 75 | +only accepted value is `kubernetes`. |
| 76 | + |
| 77 | +#### workspace.spec |
| 78 | + |
| 79 | +**Required**. This section contains configuration information specific to the |
| 80 | +`workspace.type`. |
| 81 | + |
| 82 | +#### workspace.spec.image |
| 83 | + |
| 84 | +**Required**. The image to use for the environment. The image should include the |
| 85 | +registry and (optionally) the tag, i.e. `docker.io/ubuntu:18.04`. If you omit |
| 86 | +the tag, Coder uses the default value of `latest`. |
| 87 | + |
| 88 | +You must have [imported the image](../../images/importing.md) into Coder, |
| 89 | +otherwise the environment will fail to build. |
| 90 | + |
| 91 | +#### workspace.spec.labels |
| 92 | + |
| 93 | +The |
| 94 | +[Kubernetes labels](https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/) |
| 95 | +to be added to the environment pod. |
| 96 | + |
| 97 | +```yaml |
| 98 | +workspace: |
| 99 | + labels: |
| 100 | + com.coder.custom.hello: hello |
| 101 | + com.coder.custom.world: world |
| 102 | +``` |
| 103 | + |
| 104 | +#### workspace.spec.tolerations |
| 105 | + |
| 106 | +This section defines the |
| 107 | +[Kubernetes tolerations](https://kubernetes.io/docs/concepts/scheduling-eviction/taint-and-toleration/) |
| 108 | +to be added to the pod hosting the environment. |
| 109 | + |
| 110 | +```yaml |
| 111 | +workspace: |
| 112 | + tolerations: |
| 113 | + - key: my-key |
| 114 | + operator: equals |
| 115 | + value: my-value |
| 116 | + effect: NoExecute |
| 117 | + tolerationSeconds: 3600 |
| 118 | +``` |
| 119 | + |
| 120 | +#### workspace.spec.gpucount |
| 121 | + |
| 122 | +The number of GPUs to inject into the environment. |
| 123 | + |
| 124 | +#### workspace.spec.container-based-vm |
| 125 | + |
| 126 | +Determines whether the environment should be created as a |
| 127 | +[container-based virtual machine (CVM)](../cvms.md). Default is `false`. |
| 128 | + |
| 129 | +#### workspace.spec.cpu |
| 130 | + |
| 131 | +**Required**. The number of cores to allocate to the environment. |
| 132 | + |
| 133 | +#### workspace.spec.memory |
| 134 | + |
| 135 | +**Required**. The amount of memory (in GB) to allocate to the environment. |
| 136 | + |
| 137 | +#### workspace.spec.disk |
| 138 | + |
| 139 | +**Required**. The amount of disk space (in GB) to allocate to the environment. |
| 140 | + |
| 141 | +#### workspace.configure |
| 142 | + |
| 143 | +This section lists the commands that run within the environment after Coder |
| 144 | +builds the environment. See [Configure](../../images/configure.md) for more |
| 145 | +information. |
| 146 | + |
| 147 | +#### workspace.configure.start |
| 148 | + |
| 149 | +The list of commands to run when Coder _starts_ an environment. |
| 150 | + |
| 151 | +#### workspace.configure.start[*].command |
| 152 | + |
| 153 | +**Required**. Runs the provided command within the environment (Coder supports |
| 154 | +the use of both single-line and multi-line commands). |
| 155 | + |
| 156 | +- Single-line command: |
| 157 | + |
| 158 | + ```yaml |
| 159 | + - name: Install curl |
| 160 | + run: apt install -y curl |
| 161 | + ``` |
| 162 | + |
| 163 | +- Multi-line command: |
| 164 | + |
| 165 | + ```yaml |
| 166 | + - name: Update and install curl |
| 167 | + run: | |
| 168 | + apt update |
| 169 | + apt install -y curl |
| 170 | + ``` |
| 171 | + |
| 172 | +#### workspace.configure.start[*].name |
| 173 | + |
| 174 | +The name of the command being run. |
| 175 | + |
| 176 | +#### workspace.configure.start[*].shell |
| 177 | + |
| 178 | +The shell Coder should use to run the command. |
| 179 | + |
| 180 | +```yaml |
| 181 | +start: |
| 182 | + - name: First step |
| 183 | + shell: /bin/bash |
| 184 | +``` |
| 185 | + |
| 186 | +#### workspace.configure.start[*].directory |
| 187 | + |
| 188 | +The working directory from which Coder should run the command. |
| 189 | + |
| 190 | +```yaml |
| 191 | +start: |
| 192 | + - name: First step |
| 193 | + directory: /home/coder |
| 194 | +``` |
| 195 | + |
| 196 | +#### workspace.configure.start[*].env |
| 197 | + |
| 198 | +The map of environment variables to set for the command. |
| 199 | + |
| 200 | +```yaml |
| 201 | +start: |
| 202 | + - name: First step |
| 203 | + env: |
| 204 | + HOME: /home/coder |
| 205 | + GOPATH: /home/coder/go |
| 206 | +``` |
0 commit comments