|
| 1 | +# Managing Coder templates |
| 2 | + |
| 3 | +Coder templates are the DNA used to create workspaces. They are typically managed by a platform/infrastructure group and allow developers to focus on their projects instead of the underlying infrastructure for their workspaces. |
| 4 | + |
| 5 | +[](./static/templates.png) |
| 6 | + |
| 7 | +## Organizing your templates |
| 8 | + |
| 9 | +There are many ways to organize your templates: |
| 10 | + |
| 11 | +- **Team based templates**: A template for each of your teams (e.g. a template for your _frontend_ team and a template for your _backend_ team.) |
| 12 | + |
| 13 | +- **Project based templates**: A template for each of your projects (e.g. a template for your _cool-project_ and a template for your _awesome-project_.) |
| 14 | + |
| 15 | +- **Image based templates**: A single template that is used for all your projects but with a different image for each project. |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +## What is the difference between a template and an image? |
| 20 | + |
| 21 | +A template is a collection of infrastructure as code (Terraform) used to create workspaces. An image is a collection of software (e.g. Docker) that is used to create a workspace. A template can use one or more images. For example, you can have a template that uses the _golang_ image and the _node_ image, and the user will have the choice of which image to use when creating a workspace. Choices are managed by a terraform variable: |
| 22 | + |
| 23 | +```hcl |
| 24 | +variable "image" { |
| 25 | + type = string |
| 26 | + description = "The image to use for the workspace" |
| 27 | + default = "node" |
| 28 | + validation { |
| 29 | + condition = contains(["node", "java"], var.image) |
| 30 | + error_message = "The image must be either ubuntu or node" |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +## Creating your first template |
| 36 | + |
| 37 | +1. Create a new repository in your GitHub account. This will be the repository that contains your Coder templates. |
| 38 | + |
| 39 | +2. Create a new directory in your repository called `deeplearning`. |
| 40 | + |
| 41 | +3. Create a new file in the `deeplearning` directory called `main.tf`. This is the terraform file that will be used to create your template. |
| 42 | + |
| 43 | +```hcl |
| 44 | +terraform { |
| 45 | + required_providers { |
| 46 | + coder = { |
| 47 | + source = "coder/coder" |
| 48 | + version = "0.6.14" |
| 49 | + } |
| 50 | + docker = { |
| 51 | + source = "kreuzwerker/docker" |
| 52 | + version = "3.0.1" |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | +... |
| 57 | +``` |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | +4. Create a rich-parameter variable of the form `data coder_parameter <name>`. This will be used to create a rich parameter in the template. For example: |
| 64 | + |
| 65 | + > See full list of available parameters [here](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/parameter) |
| 66 | +
|
| 67 | +```hcl |
| 68 | + data "coder_parameter" "framework" { |
| 69 | + name = "Framework" |
| 70 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/memory.svg" |
| 71 | + description = "Choose your preffered framework" |
| 72 | + type = "string" |
| 73 | + mutable = false |
| 74 | + default = "no-conda" |
| 75 | + option { |
| 76 | + name = "PyTorch" |
| 77 | + description = "PyTorch" |
| 78 | + value = "pytorch" |
| 79 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/pytorch.svg" |
| 80 | + } |
| 81 | + option { |
| 82 | + name = "PyTorch Nightly" |
| 83 | + description = "PyTorch Nightly" |
| 84 | + value = "pytorch-nightly" |
| 85 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/pytorch.svg" |
| 86 | + } |
| 87 | + option { |
| 88 | + name = "Tensorflow" |
| 89 | + description = "Tensorflow" |
| 90 | + value = "tensorflow" |
| 91 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/tensorflow.svg" |
| 92 | + } |
| 93 | + option { |
| 94 | + name = "Tensorflow + PyTorch" |
| 95 | + description = "Tensorflow + PyTorch" |
| 96 | + value = "no-conda" |
| 97 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/tf-torch.svg" |
| 98 | + } |
| 99 | + option { |
| 100 | + name = "Tensorflow + PyTorch + conda" |
| 101 | + description = "Tensorflow + PyTorch + conda" |
| 102 | + value = "conda" |
| 103 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/tf-torch-conda.svg" |
| 104 | + } |
| 105 | + option { |
| 106 | + name = "Conda" |
| 107 | + description = "Only conda (install whatever you need)" |
| 108 | + value = "conda-base" |
| 109 | + icon = "https://raw.githubusercontent.com/matifali/logos/main/conda.svg" |
| 110 | + } |
| 111 | + } |
| 112 | +``` |
| 113 | + |
| 114 | +5. For this example, we will use the pre-built [DockerDL](https://github.com/matifali/dockerdl) images. |
| 115 | + |
| 116 | +> **Note**: You can use any image you want. You can use a pre-built image or coder can build an image for you from the Dockerfile in _images_ directory. See |
| 117 | +
|
| 118 | +```hcl |
| 119 | +data "docker_registry_image" "dockerdl" { |
| 120 | + name = "matifali/dockerdl:${data.coder_parameter.framework.value}" |
| 121 | +} |
| 122 | +
|
| 123 | +resource "docker_image" "dockerdl" { |
| 124 | + name = data.docker_registry_image.dockerdl.name |
| 125 | + pull_triggers = [data.docker_registry_image.dockerdl.sha256_digest] |
| 126 | + # Keep alive for other workspaces to use upon deletion |
| 127 | + keep_locally = true |
| 128 | +} |
| 129 | +
|
| 130 | +``` |
| 131 | + |
| 132 | +> Full example is available [here](https://github.com/matifali/coder-templates/blob/main/deeplearning/main.tf) |
| 133 | +
|
| 134 | +## Next steps |
| 135 | + |
| 136 | +[How to keep your templates up to date](../keep-up-to-date): A guide on how to keep your templates up to date with the latest changes in the upstream repository using GitHub Actions. This will allow you to keep your templates up to date without having to manually update them. [Read more](../keep-up-to-date) |
0 commit comments