|
| 1 | +# Add a dev container template to Coder |
| 2 | + |
| 3 | +A Coder administrator adds a dev container-compatible template to Coder |
| 4 | +(Envbuilder). This allows the template to prompt for the developer for their dev |
| 5 | +container repository's URL as a |
| 6 | +[parameter](../../extending-templates/parameters.md) when they create their |
| 7 | +workspace. Envbuilder clones the repo and builds a container from the |
| 8 | +`devcontainer.json` specified in the repo. |
| 9 | + |
| 10 | +You can create template files through the Coder dashboard, CLI, or you can |
| 11 | +choose a template from the |
| 12 | +[Coder registry](https://registry.coder.com/templates): |
| 13 | + |
| 14 | +<div class="tabs"> |
| 15 | + |
| 16 | +## Dashboard |
| 17 | + |
| 18 | +1. In the Coder dashboard, select **Templates** then **Create Template**. |
| 19 | +1. Use a |
| 20 | + [starter template](https://github.com/coder/coder/tree/main/examples/templates) |
| 21 | + or create a new template: |
| 22 | + |
| 23 | + - Starter template: |
| 24 | + |
| 25 | + 1. Select **Choose a starter template**. |
| 26 | + 1. Choose a template from the list or select **Devcontainer** from the |
| 27 | + sidebar to display only dev container-compatible templates. |
| 28 | + 1. Select **Use template**, enter the details, then select **Create |
| 29 | + template**. |
| 30 | + |
| 31 | + - To create a new template, select **From scratch** and enter the templates |
| 32 | + details, then select **Create template**. |
| 33 | + |
| 34 | +1. Edit the template files to fit your deployment. |
| 35 | + |
| 36 | +## CLI |
| 37 | + |
| 38 | +1. Use the `template init` command to initialize your choice of image: |
| 39 | + |
| 40 | + ```shell |
| 41 | + coder template init --id devcontainer-kubernetes |
| 42 | + ``` |
| 43 | + |
| 44 | + A list of available templates is shown in the |
| 45 | + [templates_init](../../../../reference/cli/templates.md) reference. |
| 46 | + |
| 47 | +1. `cd` into the directory and push the template to your Coder deployment: |
| 48 | + |
| 49 | + ```shell |
| 50 | + cd devcontainer-kubernetes && coder templates push |
| 51 | + ``` |
| 52 | + |
| 53 | + You can also edit the files or make changes to the files before you push them |
| 54 | + to Coder. |
| 55 | + |
| 56 | +## Registry |
| 57 | + |
| 58 | +1. Go to the [Coder registry](https://registry.coder.com/templates) and select a |
| 59 | + dev container-compatible template. |
| 60 | + |
| 61 | +1. Copy the files to your local device, then edit them to fit your needs. |
| 62 | + |
| 63 | +1. Upload them to Coder through the CLI or dashboard: |
| 64 | + |
| 65 | + - CLI: |
| 66 | + |
| 67 | + ```shell |
| 68 | + coder templates push <template-name> -d <path to folder containing main.tf> |
| 69 | + ``` |
| 70 | + |
| 71 | + - Dashboard: |
| 72 | + |
| 73 | + 1. Create a `.zip` of the template files: |
| 74 | + |
| 75 | + - On Mac or Windows, highlight the files and then right click. A |
| 76 | + "compress" option is available through the right-click context menu. |
| 77 | + |
| 78 | + - To zip the files through the command line: |
| 79 | + |
| 80 | + ```shell |
| 81 | + zip templates.zip Dockerfile main.tf |
| 82 | + ``` |
| 83 | + |
| 84 | + 1. Select **Templates**. |
| 85 | + 1. Select **Create Template**, then **Upload template**: |
| 86 | + |
| 87 | +  |
| 88 | + |
| 89 | + 1. Drag the `.zip` file into the **Upload template** section and fill out the |
| 90 | + details, then select **Create template**. |
| 91 | + |
| 92 | +  |
| 93 | + |
| 94 | +</div> |
| 95 | + |
| 96 | +To set variables such as the namespace, go to the template in your Coder |
| 97 | +dashboard and select **Settings** from the **⋮** (vertical ellipsis) menu: |
| 98 | + |
| 99 | +<Image height="255px" src="../../../../images/templates/template-menu-settings.png" alt="Choose Settings from the template's menu" align="center" /> |
| 100 | + |
| 101 | +## Envbuilder Terraform provider |
| 102 | + |
| 103 | +When using the |
| 104 | +[Envbuilder Terraform provider](https://registry.terraform.io/providers/coder/envbuilder/latest/docs), |
| 105 | +a previously built and cached image can be reused directly, allowing dev |
| 106 | +containers to start instantaneously. |
| 107 | + |
| 108 | +Developers can edit the `devcontainer.json` in their workspace to customize |
| 109 | +their development environments: |
| 110 | + |
| 111 | +```json |
| 112 | +# … |
| 113 | +{ |
| 114 | + "features": { |
| 115 | + "ghcr.io/devcontainers/features/common-utils:2": {} |
| 116 | + } |
| 117 | +} |
| 118 | +# … |
| 119 | +``` |
| 120 | + |
| 121 | +## Example templates |
| 122 | + |
| 123 | +| Template | Description | |
| 124 | +| ------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 125 | +| [Docker dev containers](https://github.com/coder/coder/tree/main/examples/templates/devcontainer-docker) | Docker provisions a development container. | |
| 126 | +| [Kubernetes dev containers](https://github.com/coder/coder/tree/main/examples/templates/devcontainer-kubernetes) | Provisions a development container on the Kubernetes cluster. | |
| 127 | +| [Google Compute Engine dev container](https://github.com/coder/coder/tree/main/examples/templates/gcp-devcontainer) | Runs a development container inside a single GCP instance. It also mounts the Docker socket from the VM inside the container to enable Docker inside the workspace. | |
| 128 | +| [AWS EC2 dev container](https://github.com/coder/coder/tree/main/examples/templates/aws-devcontainer) | Runs a development container inside a single EC2 instance. It also mounts the Docker socket from the VM inside the container to enable Docker inside the workspace. | |
| 129 | + |
| 130 | +Your template can prompt the user for a repo URL with |
| 131 | +[parameters](../../extending-templates/parameters.md): |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | +## Dev container lifecycle scripts |
| 136 | + |
| 137 | +The `onCreateCommand`, `updateContentCommand`, `postCreateCommand`, and |
| 138 | +`postStartCommand` lifecycle scripts are run each time the container is started. |
| 139 | +This could be used, for example, to fetch or update project dependencies before |
| 140 | +a user begins using the workspace. |
| 141 | + |
| 142 | +Lifecycle scripts are managed by project developers. |
| 143 | + |
| 144 | +## Next steps |
| 145 | + |
| 146 | +- [Dev container security and caching](./devcontainer-security-caching.md) |
| 147 | + |
| 148 | +``` |
| 149 | +
|
| 150 | +``` |
0 commit comments