Window Vscode使用dev containers进行容器化开发
【摘要】 目的是进行容器化开发,好处有:相对拟真的环境。资源隔离,资源随时创建,随时销毁,不会弄脏宿主机环境。集成环境更简单,更便捷。远程开发。 前置条件Docker DesktopVscode 或 Jetbrains安装Dev Containers插件 测试官方的示例仓库:git clone https://github.com/microsoft/vscode-remote-try-cpp.git...
目的是进行容器化开发,好处有:
- 相对拟真的环境。
- 资源隔离,资源随时创建,随时销毁,不会弄脏宿主机环境。
- 集成环境更简单,更便捷。
- 远程开发。
前置条件
- Docker Desktop
- Vscode 或 Jetbrains
- 安装Dev Containers插件
测试
官方的示例仓库:
git clone https://github.com/microsoft/vscode-remote-try-cpp.git
在.devcontainer目录下
.devcontainer.json
Dockerfile
reinstall-cmake.sh
.devcontainer.json是dev containers的配置文件,vscode在项目启动后会扫描.devcontainer.json,并提示是否切换到container环境。
完整的文件内容:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the
// README at: https://github.com/devcontainers/templates/tree/main/src/cpp
{
"name": "C++",
"build": {
"dockerfile": "Dockerfile"
},
// Features to add to the dev container. More info: https://containers.dev/features.
// "features": {},
// Configure tool-specific properties.
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker",
"ms-vscode.cpptools-extension-pack"
]
}
}
// Use 'forwardPorts' to make a list of ports inside the container available locally.
// "forwardPorts": [],
// Use 'postCreateCommand' to run commands after the container is created.
// "postCreateCommand": "gcc -v",
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
// "remoteUser": "root"
}
下面内容指定构建的方式是Dockerfile,适合定制化高的项目
{
"build": {
"dockerfile": "Dockerfile"
}
}
也可以指定镜像,简单粗暴
{
"image": "mcr.microsoft.com/devcontainers/javascript-node:1-18-bullseye",
}
extensions是指container环境中的插件
{
"customizations": {
// Configure properties specific to VS Code.
"vscode": {
"settings": {},
"extensions": [
"streetsidesoftware.code-spell-checker",
"ms-vscode.cpptools-extension-pack"
]
}
}
}
可以右键点击插件添加到这里:
Dockerfile文件内容:
FROM mcr.microsoft.com/devcontainers/cpp:1-debian-12
ARG REINSTALL_CMAKE_VERSION_FROM_SOURCE="none"
# Optionally install the cmake for vcpkg
COPY ./reinstall-cmake.sh /tmp/
RUN if [ "${REINSTALL_CMAKE_VERSION_FROM_SOURCE}" != "none" ]; then \
chmod +x /tmp/reinstall-cmake.sh && /tmp/reinstall-cmake.sh ${REINSTALL_CMAKE_VERSION_FROM_SOURCE}; \
fi \
&& rm -f /tmp/reinstall-cmake.sh
# [Optional] Uncomment this section to install additional vcpkg ports.
# RUN su vscode -c "${VCPKG_ROOT}/vcpkg install <your-port-name-here>"
# [Optional] Uncomment this section to install additional packages.
# RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
# && apt-get -y install --no-install-recommends <your-package-list-here>
在没有切换到container环境的时候,运行代码会报错:
切换到container环境:
切换到container环境后,运行代码:
过程中,只有一个槽点,就是镜像下载的慢,整体开发体验还是很棒的,现在越来越多项目使用dev containers进行开发,这事不是偶然。
以后有时间会研究多项目,多容器的使用,还有怎么远程开发,今天就简单体验一下。
参考资料:
https://code.visualstudio.com/docs/devcontainers/tutorial
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)