Skip to content
Published at:

VSCode + Container构建开发环境

Docker的Container为环境而生:编译环境、测试环境、部署环境;另外日常用Mac,有时候需要用到Linux开发环境,记录下:

VSCode Developing inside a Container:https://code.visualstudio.com/docs/remote/containers

Development Containers:https://containers.dev/

目录

bash
# 目录
$ cd
$ mkdir linux && cd linux
$ mkdir .devcontainer

# 目录中的文件
$ tree .devcontainer
.devcontainer
├── Dockerfile
└── devcontainer.json

Dockerfile文件:

dockerfile
# https://hub.docker.com/r/docker/dev-environments-default
FROM docker/dev-environments-default:latest

RUN apt-get update

# tools
RUN apt install libdbus-1-dev pkg-config curl tree vim -y

# C/C++ env
RUN apt install build-essential cmake valgrind -y

# LLVM
RUN apt install lsb-release wget software-properties-common gnupg -y
RUN bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"
ENV PATH="/usr/lib/llvm-14/bin/:${PATH}"

# for Rust
RUN curl https://sh.rustup.rs -sSf | bash -s -- -y
ENV PATH="/root/.cargo/bin:${PATH}"

WORKDIR /workspace

CMD [ "sleep", "infinity" ]

Devcontainer.json文件:

配置:https://containers.dev/implementors/json_reference/

json
// For format details, see https://aka.ms/devcontainer.json.
{
  "name": "Linux",
  "build": {
    "dockerfile": "Dockerfile"
  },
  // Set *default* container specific settings.json values on container create.
  "settings": {},
  // Add the IDs of extensions you want installed when the container is created.
  "extensions": [
    "llvm-vs-code-extensions.vscode-clangd", // clangd
    "twxs.cmake", // CMake
    "ms-vscode.cmake-tools", // CMake Tools
    "vadimcn.vscode-lldb", // CodeLLDB
    "rust-lang.rust-analyzer", // rust-analyzer
    "serayuzgur.crates", // crates
    "cschlosser.doxdocgen", // Doxygen Documentation Generator
    "usernamehw.errorlens", // Error Lens
    "GitHub.copilot" // GitHub Copilot
  ]
  // 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": "uname -a",
  // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
  // "remoteUser": "vscode"
}

环境构建

方式一:VSCode

  • 用VSCode打开目录:code linux
  • VSCode命令:>Remote-Containers: Reopen in Container
    • 构建镜像Image
    • 创建容器Contianer
    • 在容器中安装vscode-server

方式二:Docker命令构建

bash
$ docker build -t linux:latest .

$ docker run -d -w /workspace \
    -v $(pwd):/workspace \
    linux:latest

VSCode命令:>Remote-Containers: Attach to Running Container

其它

https://apt.llvm.org/

bash
# install LLVM
$ apt install lsb-release wget software-properties-common
$ bash -c "$(wget -O - https://apt.llvm.org/llvm.sh)"

Updated at: