这个插件允许我们在容器中运行 Visual Studio Code。
在项目的根目录中,您需要创建一个名为 .devcontainer 的文件夹。我们将在此处存储环境设置。
然后在此文件夹中创建两个文件,devcontainer.json 和一个 Dockerfile。
命名很重要,因为 Visual Studio Code 希望我们提供一些文件夹和文件名才能成功运行容器。
您的文件夹结构应如下:
在 Dockerfile 中,我们选择 Docker 镜像并在安装镜像后运行所需的任何命令(例如全局安装)。
FROM node:12.14.1-stretch
RUN npm install -g eslint prettier
然后,在 devcontainer.json 中,我们可以配置所有设置。
devcontainer.json 基本上是一个配置文件,该文件确定如何构建和启动 dev 容器。
{
"name": "Node.js Sample",
"dockerFile": "Dockerfile",
"appPort": 3000,
"extensions": ["dbaeumer.vscode-eslint"],
"settings": {
"terminal.integrated.shell.linux": "/bin/bash"
},
"postCreateCommand": "yarn install",
// Comment out the next line to run as root instead. Linux users, update
// Dockerfile with your user's UID/GID if not 1000.
"runArgs": ["-u", "node"]
}
这是 devcontainer.json 选项的完整列表。
检查一下
容器运行并连接后,您应该在状态栏的左下方看到远程上下文(remote context)更改:
安装了“Remote — Containers”扩展后,您将在最左侧看到一个新的状态栏项目。
远程状态栏项目可以快速向您显示在哪个上下文中运行VS Code(本地或远程),单击该项目将弹出“Remote — Containers”命令。
选择在容器中重新打开。
等待容器构建
如果这是您的第一次连接,则将下载并构建 Docker 镜像,并将启动运行 VS Code Server 副本的容器。第一次可能需要几分钟,但以后的连接仅需几秒钟。
检查环境
在容器中进行开发的有用的事情之一是,您可以使用应用程序所需的特定版本的依赖关系,而不会影响本地开发环境。
node --version
npm --version
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有