使用 VSCode Dev Containers 进行开发

在开发 Flower 框架时,我们希望确保所有贡献者使用相同的开发环境来格式化代码或运行测试。为此,我们使用了 VSCode 远程容器扩展。这是什么?请阅读下面这段话:

The Visual Studio Code Remote - Containers extension lets you use a Docker container as a fully-featured development environment. It allows you to open any folder inside (or mounted into) a container and take advantage of Visual Studio Code's full feature set. A devcontainer.json file in your project tells VS Code how to access (or create) a development container with a well-defined tool and runtime stack. This container can be used to run an application or to separate tools, libraries, or runtimes needed for working with a codebase.

工作区文件从本地文件系统加载,或复制或克隆到容器中。扩展在容器内安装和运行,在容器内它们可以完全访问工具、平台和文件系统。这意味着,只需连接到不同的容器,就能无缝切换整个开发环境。

Source: Official VSCode documentation

开始

Configuring and setting up the Dockerfile as well the configuration for the devcontainer can be a bit more involved. The good thing is you don't have to do it. Usually it should be enough to install Docker on your system and ensure its available on your command line. Additionally, install the VSCode Containers Extension.

现在你应该可以开始了。启动 VSCode 时,它会要求你在容器环境中运行,如果你确认,它会自动构建容器并使用它。要手动指示 VSCode 使用 devcontainer,可以在安装扩展后,点击 VSCode 窗口左下角的绿色区域,然后选择 "(重新)在容器中打开文件夹"选项。

在某些情况下,您的设置可能更复杂。有关这些情况,请参考以下资料: