VS Code Dev Containers 使用教程

简介

Development Containers(简写为dev container)可以让你使用容器作为一个完整的开发环境。用容器可以隔离不同的开发环境,允同时使用不同的开发环境。便于分享开发环境,提高开发效率。

VS Code支持dev container. 本教程将介绍如何使用VS Code的dev container功能。

环境要求

配置步骤

1. 安装"Remote Development"插件

在VS Code插件市场中搜索"Remote Development"插件并安装。

或者使用命令行安装:

code --install-extension ms-vscode-remote.vscode-remote-extensionpack

2. 使用已有镜像

  1. 在工作目录下创建一个.devcontainer文件夹
mkdir .devcontainer
  1. 然后在.devcontainer文件夹下创建一个devcontainer.json文件,内容如下:
{
    "name": "Dev Container Python",
    "image": "mcr.microsoft.com/devcontainers/python:3.11"
}

此时,目录结构如下:

$ tree -a .
.
└── .devcontainer
    └── devcontainer.json

2 directories, 1 file
  1. 按照如下步骤打开dev container:

    1. 打开VS Code的命令面板(Ctrl+Shift+P)
    2. 搜索"Remote-Containers: Reopen in Container"
    3. 选择"Remote-Containers: Reopen in Container".

    此时VS Code会自动下载镜像并启动一个python的dev container。

一些公共的镜像
语言镜像
C++mcr.microsoft.com/devcontainers/cpp:ubuntu-22.04
Gomcr.microsoft.com/devcontainers/go:1.21
Javamcr.microsoft.com/devcontainers/java:21
Pythonmcr.microsoft.com/devcontainers/python:3.11

更多的镜像可以参考: Dev Container Images

3. 从Dockerfile构建镜像并使用

如果需要自定义镜像,则需要将image改为build,并指定构建镜像的Dockerfile

{
    "name": "Dev Container Python",
    "build": {
        "dockerfile": "Dockerfile"
    },
    "runArgs":["--name", "devcontainer-python"]
}

Dockerfile 内容为

FROM ubuntu:latest

# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive

# This Dockerfile adds a non-root 'vscode' user with sudo access. However, for Linux,
# this user's GID/UID must match your local user UID/GID to avoid permission issues
# with bind mounts. Update USER_UID / USER_GID if yours is not 1000. See
# https://aka.ms/vscode-remote/containers/non-root-user for details.
ARG USER_UID=1000
ARG USER_GID=$USER_UID

RUN apt update -y && \
    apt -y install --no-install-recommends apt-utils dialog && \
    apt -y install \
    build-essential \
    iproute2 \
    poppler-utils \
    procps \
    python3 \
    python3-pip \
    sudo \
    && apt autoremove -y \
    && apt clean -y

# Create a non-root user to use if preferred
# see https://aka.ms/vscode-remote/containers/non-root-user.
RUN groupadd --gid $USER_GID vscode \
    && useradd -s /bin/bash --uid $USER_UID --gid $USER_GID -m vscode \
    && echo vscode ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/vscode \
    && chmod 0440 /etc/sudoers.d/vscode

# Switch back to dialog for any ad-hoc use of apt-get
ENV DEBIAN_FRONTEND=dialog

此时的文件结构如下:

$ tree -a .
.
`-- .devcontainer
    |-- Dockerfile
    `-- devcontainer.json

1 directory, 2 files

需要注意的是, 自定义的镜像中需要支持如下命令:

  • curl
  • freetype
  • gcompat
  • libxext
  • libxi
  • libxrender
  • libxtst
  • procps
  • ps
  • unzip

参考资料

  1. devcontainer.json Reference, by Docker
  2. Developing inside a Container, by Microsoft
  • 8
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值