|
| 1 | +# devcontainer template ... |
| 2 | + |
| 3 | +{:toc} |
| 4 | + |
| 5 | +## Setting up templates |
| 6 | + |
| 7 | +To use the `devcontainer template` commands you need to configure some templates. |
| 8 | + |
| 9 | +A good starting point is the the VS Code devcontainers repo. Choose a directory, and clone the repo using `git clone https://github.com/microsoft/vscode-dev-containers` |
| 10 | + |
| 11 | +Next, we need to tell the `devcontainer` CLI to use this folder. If you haven't previously created a config file, run `devcontainer config write` to save a config file and then open `~/.devcontainer-cli/devcontainer-cli.json` in your favourite editor. |
| 12 | + |
| 13 | +The starting configuration will look something like: |
| 14 | + |
| 15 | +```json |
| 16 | +{ |
| 17 | + "templatepaths": [] |
| 18 | +} |
| 19 | +``` |
| 20 | + |
| 21 | +Update to include the path to the `containers` folder in the `vscode-dev-containers` repo you just cloned: |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "templatepaths": ["$HOME/source/vscode-dev-containers/containers"] |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +## Listing templates |
| 30 | + |
| 31 | +Running `devcontainer template list` will show the templates that `devcontainer` discovered |
| 32 | + |
| 33 | +## Adding a devcontainer definition |
| 34 | + |
| 35 | +To add the files for a devcontainer definition to your project, change directory to the folder you want to add the devcontainer to and then run: |
| 36 | + |
| 37 | +```bash |
| 38 | +# Add the go template |
| 39 | +devcontainer template add go |
| 40 | +``` |
| 41 | + |
| 42 | +This will copy in the template files for you to modify as you wish. |
| 43 | + |
| 44 | +## Adding a link to a devcontainer |
| 45 | + |
| 46 | +If you are working with a codebase that you don't want to commit the devcontainer definition to (e.g. an OSS project that doesn't want a devcontainer definition), you can use the `template add-link` command. Instead of copying template files it creates symlinks to the template files and adds a `.gitignore` file to avoid accidental git commits. |
| 47 | + |
| 48 | +As with `template add`, run this from the folder you want to add the devcontainer to: |
| 49 | + |
| 50 | +```bash |
| 51 | +# Symlink to the go template |
| 52 | +devcontainer template add-link go |
| 53 | +``` |
| 54 | + |
| 55 | +See the [repository containers](#repository-containers) section for an alternative to template links. |
| 56 | + |
| 57 | +## Creating your own templates |
| 58 | + |
| 59 | +`devcontainer` can be configured to scan multiple folders to find templates. It is designed to work with folders structured in the same was as the [containers from in github.com/microsoft/vscode-dev-containers](https://github.com/microsoft/vscode-dev-containers/tree/master/containers), e.g.: |
| 60 | + |
| 61 | + |
| 62 | +```misc |
| 63 | +template-collection-folder |
| 64 | + |-template1 |
| 65 | + | |-.devcontainer |
| 66 | + | | |-devcontainer.json |
| 67 | + | | |-Dockerfile |
| 68 | + | | |-<other content for the template> |
| 69 | + |-misc-folder |
| 70 | + |-<misc content that is ignored as there is no .devcontainer folder> |
| 71 | + |-<README or other files that are ignore> |
| 72 | +``` |
| 73 | + |
| 74 | +Assuming you cloned [github.com/microsoft/vscode-dev-containers/](https://github.com/microsoft/vscode-dev-containers/) into your `~/source/` folder and set up a custom devcontainer folder in `~/source/devcontainers` then you can configure your template paths as shown below. The sub-folder names are used as the template name and when duplicates are found the first matching folder is taken, so in the example below the `~/source/devcontainers` templates take precedence. |
| 75 | + |
| 76 | +```json |
| 77 | +{ |
| 78 | + "templatepaths": [ |
| 79 | + "$HOME/source/devcontainers", |
| 80 | + "$HOME/source/vscode-dev-containers/containers" |
| 81 | + ] |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +## Repository containers |
| 86 | + |
| 87 | +VS Code dev containers have another feature called "Repository containers". These are a set of dev container definitions that VS Code will automatically apply to a project based on its git repo. |
| 88 | + |
| 89 | +The default definitions are in the [microsoft/vscode-dev-containers](https://github.com/microsoft/vscode-dev-containers/tree/master/repository-containers) repo. If you look at the repo, you will see a `github.com` folder followed by paths for `<org>/<repo>`, e.g. `django/django`. The `https://github.com/django/django` repo doesn't contain a dev container definition, but VS Code will use the repository container definition from the `microsoft/vscode-dev-containers` repo. |
| 90 | + |
| 91 | +You can also configure VS Code to look for additional local paths for repository containers by providing a value for the VS Code `remote.containers.repository-container-paths` setting (see [this issue](https://github.com/microsoft/vscode-remote-release/issues/3218) for more details). |
| 92 | + |
0 commit comments