如何在VS Code中创建一个开发容器,并将其用于多个项目?

huangapple go评论76阅读模式
英文:

How do I create one dev container in VS Code and use it for multiple projects?

问题

我发现自己在VS Code上同时处理多个项目,每个项目都有自己的代码库,我想要在这些项目中使用相似/相同的开发容器。

例如,有几个Python + Ansible项目,我想要一个Python 3开发容器,其中包括一些常见的Python包和Ansible。

在每个项目中不断重新定义基本相同的.devcontainer文件,然后在我的主机上拥有多个几乎相同的Docker容器似乎是浪费和冗余的。

如何定义和维护一个单一的开发容器,每个项目都可以引用和使用?

英文:

I find myself working on a multitude of projects in VS Code, each with separate codebases in their own repositories, for which I want to use similar / identical Dev Containers.

For example, several Python + Ansible projects where I want a Python 3 Dev Container with a few common Python packages and Ansible.

It seems wasteful / redundant to keep redefining basically identical .devcontainer files in each of these projects, and then have multiple almost-identical Docker containers littering my host machine.

How can I define and maintain a single Dev Container that each of these projects can reference and use?

答案1

得分: 1

我找到了相关的文档:为多个项目配置单独的容器

总结(引用链接文档中的所有内容):

  1. 创建一个顶层文件夹,您的项目文件夹将位于其中。

  2. 向顶层文件夹添加开发容器配置文件(您可以重新利用现有项目中的文件,或者使用VS Code的工具来创建它们)。

  3. 构建并连接到容器。如果您使用了VS Code的工具添加配置文件,它应该会自动构建并连接。如果您添加了自己的文件,您可能需要使用“在容器中重新打开文件夹”的操作。

  4. 现在,在容器中打开您的一个项目。它将位于容器工作区路径的子文件夹中,例如/workspaces/python/my-cool-project

  5. 注意加粗的句子(我强调了这一点):您需要先在容器中打开每个项目的子文件夹,以便它们显示在Remote Explorer中的容器下。之后,您可以使用Remote Explorer(或VS Code的最近文件夹列表)直接在常规开发容器中打开所需的项目!

英文:

I found the relevant documentation: Configure separate containers for multiple projects

To summarize (all quotes from the linked docs):

  1. Create a top-level folder beneath which your project folders will live.

> Imagine you had your source code across multiple repositories in a single folder for a given toolset.
> Let's set up a container for use with all of the Python projects in the ./Repos/python folder.
> Start Visual Studio Code, select Dev Containers: Open Folder in Container... from the Command Palette (F1) or quick actions Status bar item, and select the ./Repos/python folder.

  1. Add Dev Container config files to the top-level folder (I repurposed existing files from my projects, or you can use VS Code's tooling to create them).

> Now pick a starting point for your dev container. You can either select a base dev container definition from a filterable list, or use an existing Dockerfile or Docker Compose file if one exists in the folder you selected.
> After picking the starting point for your container, VS Code will add the dev container configuration files to the ./Repos/python/.devcontainer folder.

  1. Build and connect to the container. If you used VS Code's tooling to add the config files, it should build and connect automatically. If you added your own files, you might need to use the Reopen folder in container action.

  2. Now, open one of your projects inside the container. It'll be in a subfolder of the container's workspace path, e.g. /workspaces/python/my-cool-project.

> Once connected use File > Open... / Open Folder... to select one of the folders under ./Repos/python.
> In a moment, VS Code will open the folder inside the same container. In the future, you can use the Remote Explorer in the Activity Bar to open this sub-folder in the container directly.

  1. Note that bolded sentence (emphasis mine): you'll need to open each of your project sub-folders in the container once so that they show up underneath the container in Remote Explorer. After that, you can use Remote Explorer (or VS Code's recent folders list) to open your desired project in the common Dev Container directly!

huangapple
  • 本文由 发表于 2023年3月4日 07:51:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75632770.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定