Skip to main content

Adding features to a devcontainer.json file

With features, you can quickly add tools, runtimes, or libraries to your dev container configuration.

功能是指安装代码和开发容器配置的独立单元,其设计目的是跨各种基本容器映像工作。 可以使用功能将工具、运行时或库快速添加到 codespace 映像。 有关详细信息,请参阅 containers.dev 上的可用功能功能规范

可以从 VS Code 或 GitHub.com 上的存储库向 devcontainer.json 文件添加功能。 Use the tabs in this article to display instructions for each of these ways of adding features.

Adding features to a devcontainer.json file

  1. Navigate to your repository on GitHub.com, find your devcontainer.json file, and click to edit the file.

    If you don't already have a devcontainer.json file, you can create one now. For more information, see "Introduction to dev containers."

  2. To the right of the file editor, in the Marketplace tab, browse or search for the feature you want to add, then click the name of the feature.

    Screenshot of the Terraform feature in the Marketplace tab, with "Terra" in the search bar

  3. Under "Installation," click the code snippet to copy it to your clipboard, then paste the snippet into the features object in your devcontainer.json file.

    Screenshot of a code block in the Installation section of the Marketplace tab

    "features": {
         ...
         "ghcr.io/devcontainers/features/terraform:1": {},
         ...
    }
    
  4. By default, the latest version of the feature will be used. To choose a different version, or configure other options for the feature, expand the properties listed under "Options" to view the available values, then add the options by manually editing the object in your devcontainer.json file.

    Screenshot of the Options section of the Marketplace tab, with "version" and "tflint" expanded

    "features": {
         ...
         "ghcr.io/devcontainers/features/terraform:1": {
             "version": "1.1",
             "tflint": "latest"
         },
         ...
    }
    
  5. Commit the changes to your devcontainer.json file.

The configuration changes will take effect in new codespaces created from the repository. To make the changes take effect in existing codespaces, you will need to pull the updates to the devcontainer.json file into your codespace, then rebuild the container for the codespace. For more information, see "Introduction to dev containers."

To add features in VS Code while you are working locally, and not connected to a codespace, you must have the "Dev Containers" extension installed and enabled. For more information about this extension, see the VS Code Marketplace.

  1. 使用 Shift+Command+P (Mac) 或 Ctrl+Shift+P (Windows/Linux) 访问 VS Code Command Palette。

  2. Start typing "configure" and click Codespaces: Configure Dev Container.

    Screenshot of the 'Configure Dev Container' option in the Command Palette

  3. Click Modify your active configuration.

    Screenshot of the 'Modify your active configuration' option in the Command Palette

  4. Update your feature selections, then click OK.

    Screenshot of the additional features menu during container configuration

  5. If you're working in a codespace, a prompt will appear in the lower-right corner. To rebuild the container and apply the changes to the codespace you're working in, click Rebuild Now.

    Screenshot of the 'Codespaces: Rebuild Container' option in the Command Palette