Skip to content

feat(dev): Add Dockerfile and devcontainer configuration#3

Merged
qzhhhi merged 1 commit intodev/v3from
feature/devcontainer
Dec 17, 2025
Merged

feat(dev): Add Dockerfile and devcontainer configuration#3
qzhhhi merged 1 commit intodev/v3from
feature/devcontainer

Conversation

@qzhhhi
Copy link
Copy Markdown
Member

@qzhhhi qzhhhi commented Dec 17, 2025

功能概述

本PR为项目添加并配置了完整的本地开发容器与 Docker 开发镜像,旨在为开发者提供标准化、可复现的交叉编译与编辑器开发环境。

主要改动

1. Dockerfile

  • 基于 Ubuntu 24.04,使用 /bin/bash 作为默认 shell,设置时区为 UTC 与 UTF-8 locale,DEBIAN_FRONTEND=noninteractive。
  • 安装广泛的开发工具与依赖(vim、wget、curl、zsh、cmake、make、ninja-build、git、sudo、python3 及其常用包、libusb 等)。
  • 通过 dpkg-divert 与 update-alternatives 将系统默认 gcc/g++ 指向 gcc-14/g++-14。
  • 下载并安装 riscv32-elf 交叉工具链(解压到 /opt/riscv),并将其 bin 加入 PATH。
  • 下载并安装 ARM GNU 工具链(版本 15.2.rel1,解压并移动到 /opt/arm-none-eabi),并将其 bin 加入 PATH。
  • 添加 LLVM apt 源并动态检测/安装最新版本的 clangd,设置 update-alternatives。
  • 提供可选的中国镜像源配置文件(Tsinghua)。
  • 切换默认用户为 ubuntu、设置工作目录 /home/ubuntu、授予无密码 sudo 权限。
  • 安装 Oh My Zsh,并将主题设置为 af-magic,创建指向仓库中 .devcontainer/.zsh_history 的 zsh 历史文件符号链接。

2. .devcontainer/devcontainer.json

  • 定义名为 "librmcs-develop" 的 devcontainer,基于镜像 qzhhhi/librmcs-develop:latest。
  • 启用 privileged 模式、使用主机网络(--network=host)。
  • 挂载 /dev、/tmp/.X11-unix,以及将宿主 HOME 绑定到容器的 /mnt/home。
  • onCreateCommand:在容器内创建 ~/.codex 到 /mnt/home/.codex 的符号链接;postCreateCommand:尝试在 VS Code Server 环境中安装 openai.chatgpt 扩展(若存在)。
  • 传递 DISPLAY 和代理相关环境变量(HTTP(S)_PROXY / NO_PROXY 的大小写形式)。
  • 设置 remote.autoForwardPorts 为 false,并预装一组 VS Code 扩展(clangd、ms-python.pylance、ms-python.python、ms-python.debugpy、CMake 支持、拼写检查、git-graph)。

3. .gitignore

  • 新增/确保忽略 compile_commands.json 和 .devcontainer/.zsh_history(以及 .vscode/、.cache/、build/ 等)。

审查难度

  • Dockerfile:高(工具链下载/安装、系统替换/alternatives、镜像体积与缓存清理、安全与镜像可信性需关注)
  • devcontainer 配置:中(镜像来源、绑定挂载与特权模式、postCreate 的扩展安装逻辑需验证)
  • .gitignore:低

变更性质

仅添加/配置开发环境相关基础设施,不影响仓库业务代码或导出 API。

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 17, 2025

Walkthrough

本次 PR 新增并配置开发容器与镜像:加入 .devcontainer/devcontainer.json、完整的基于 Ubuntu 24.04 的 Dockerfile,用于构建含 RISC‑V 与 ARM 交叉工具链、clangd、用户配置与开发工具的开发环境;并更新 .gitignore 忽略项。

Changes

凝聚 / 文件 变更摘要
开发容器配置
​.devcontainer/devcontainer.json
新增 devcontainer 配置:镜像 qzhhhi/librmcs-develop:latest、特权模式、设备挂载(/dev/tmp/.X11-unix${localEnv:HOME}/mnt/home)、环境变量透传(DISPLAY、HTTP/S_PROXY 等)、host 网络、VS Code 远程设置与扩展列表,包含 onCreate/postCreate 命令
构建镜像 / 工具链
​Dockerfile
新增 Dockerfile(基于 Ubuntu 24.04):设置时区/本地化、安装大量开发依赖、通过 dpkg-divert/update-alternatives 固定 GCC14、下载并安装 riscv32-elf 与 arm-none-eabi 工具链(/opt/...),添加 LLVM 仓库并安装 clangd、添加 CN apt 源、创建 ubuntu 用户并配置无密码 sudo、安装 Oh My Zsh 与相关配置
版本控制忽略规则
​.gitignore
更新 .gitignore,添加/调整条目以忽略 compile_commands.json.devcontainer/.zsh_history

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 分钟

可能需要特别关注的区域:

  • Dockerfile 中多阶段安装、外部二进制下载与校验(版本锁定、校验和或签名缺失)
  • 环境变量与 PATH 注入(GNURISCV_TOOLCHAIN_PATH / GNUARM_TOOLCHAIN_PATH)和 update-alternatives 配置的正确性
  • devcontainer 挂载点与特权模式(安全性、主机兼容性)以及 postCreate 的 VS Code server 扩展安装逻辑
  • CN apt 源添加方式是否影响通用性与镜像可复现性

Poem

🐰 在容器里我画圈,工具链像胡萝卜串连,
下载、配置、别怨烦,终有一天编译成甜甜圈,
代码跳舞,clangd在唱歌,终端闪烁像星辰✨

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确总结了主要变更:添加Dockerfile和devcontainer配置,与所有修改文件(Dockerfile、.devcontainer/devcontainer.json、.gitignore)的内容相符。
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feature/devcontainer

Comment @coderabbitai help to get the list of available commands and usage tips.

@qzhhhi
Copy link
Copy Markdown
Member Author

qzhhhi commented Dec 17, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 17, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (3)
.devcontainer/devcontainer.json (2)

7-12: 挂载 /dev 需要谨慎考虑。

将整个 /dev 挂载到容器会暴露所有主机设备。如果只需要特定设备(如 USB 串口),可以考虑仅挂载需要的设备路径,例如 /dev/ttyUSB0/dev/bus/usb


24-25: postCreateCommand 中的 glob 模式可能不可靠。

使用 ls ... | head -n 1 查找 code-server 二进制文件可能因路径变化而失败。考虑使用更稳定的方法:

-    "postCreateCommand": "bash -c 'if [ -d ~/.codex ]; then $(ls ~/.vscode-server*/bin/*/bin/code-server* | head -n 1) --install-extension openai.chatgpt || true; fi;'",
+    "postCreateCommand": "bash -c 'if [ -d ~/.codex ] && command -v code-server &>/dev/null; then code-server --install-extension openai.chatgpt || true; fi;'",

或者直接依赖 VS Code 的扩展自动安装机制,将扩展添加到 extensions 列表中。

Dockerfile (1)

51-56: 建议对下载的工具链进行校验。

从外部 URL 下载二进制文件存在供应链风险。建议添加 SHA256 校验:

 RUN wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/latest/download/riscv32-elf-ubuntu-24.04-gcc.tar.xz \
+    && echo "<expected_sha256>  riscv32-elf-ubuntu-24.04-gcc.tar.xz" | sha256sum -c - \
     && tar -xvf riscv32-elf-ubuntu-24.04-gcc.tar.xz -C /opt/ \
     && rm riscv32-elf-ubuntu-24.04-gcc.tar.xz

如果使用 latest 版本导致校验困难,可以考虑固定版本号。

📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1f85a2c and f6fe92b.

📒 Files selected for processing (3)
  • .devcontainer/devcontainer.json (1 hunks)
  • .gitignore (1 hunks)
  • Dockerfile (1 hunks)
🔇 Additional comments (7)
.gitignore (1)

5-7: LGTM!

忽略 compile_commands.json.zsh_history 是合理的做法,前者是 CMake 构建产物,后者是用户特定的 shell 历史记录。

.devcontainer/devcontainer.json (3)

6-6: 安全提示:privileged: true 授予容器完整的主机能力。

特权模式会禁用大部分容器安全隔离机制。如果仅需访问 USB 设备进行固件开发,考虑使用更精细的权限控制:

-    "privileged": true,
+    "capAdd": ["SYS_RAWIO"],
+    "securityOpt": ["seccomp=unconfined"],

或者仅挂载特定设备而非整个 /dev。如果确实需要完整的主机访问权限(如调试硬件),请确认这是有意为之。


35-37: --network=host 会禁用容器网络隔离。

主机网络模式虽然方便,但会让容器直接使用主机网络栈。如果只是为了代理或 X11 转发,当前的 containerEnv 配置已经足够。请确认是否确实需要完整的主机网络访问。


43-56: 扩展配置清晰合理。

扩展列表涵盖了 C++、Python、CMake 开发所需的核心工具,分类注释清晰。

Dockerfile (3)

68-77: clangd 动态版本检测实现良好。

使用 apt-cache search 配合 sort -V 自动获取最新版本是个好方法。update-alternatives 配置也正确设置了默认 clangd。


79-86: CN 镜像配置作为备用是合理的。

将镜像配置保存为 .bak 文件供用户手动启用是个好做法,不会影响默认行为。


96-98: Oh My Zsh 安装脚本未经校验直接执行。

从网络下载脚本并直接执行存在安全风险。虽然这在开发环境中较为常见,但建议记录此风险或考虑使用官方维护的替代方案。

Comment thread Dockerfile Outdated
Comment thread Dockerfile
Comment thread Dockerfile
@qzhhhi qzhhhi force-pushed the feature/devcontainer branch from f6fe92b to fd353ca Compare December 17, 2025 16:32
@qzhhhi
Copy link
Copy Markdown
Member Author

qzhhhi commented Dec 17, 2025

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 17, 2025

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

♻️ Duplicate comments (1)
Dockerfile (1)

58-66: 与过往评论一致:建议添加 SHA256 校验。

ARM 工具链版本 15.2.rel1 已确认可用。如之前讨论,建议下载 .sha256asc 文件并验证完整性以增强供应链安全,但这不是必需的。

🧹 Nitpick comments (2)
.devcontainer/devcontainer.json (1)

24-25: postCreateCommand 中的路径匹配模式较为脆弱。

~/.vscode-server*/bin/*/bin/code-server* 这个 glob 模式依赖 VS Code Server 的内部目录结构,未来版本更新可能导致匹配失败。虽然 || true 避免了失败中断,但扩展可能静默安装失败。

考虑使用 VS Code 的内置机制:

-    "postCreateCommand": "bash -c 'if [ -d ~/.codex ]; then $(ls ~/.vscode-server*/bin/*/bin/code-server* | head -n 1) --install-extension openai.chatgpt || true; fi;'",
+    "postCreateCommand": "bash -c 'if [ -d ~/.codex ]; then code --install-extension openai.chatgpt || true; fi;'",

或将扩展添加到 extensions 列表中(如果不需要条件安装)。

Dockerfile (1)

51-56: 建议为 RISC-V 工具链添加版本固定和完整性校验。

从 GitHub releases 下载 latest 版本并缺乏完整性校验,存在供应链安全风险。建议将版本固定到特定的 release 标签,或使用 git 获取源码。如采用下载预编译二进制的方式,需将 SHA256 校验和硬编码在 Dockerfile 中(GitHub releases 页面显示校验和但不提供可下载的 .sha256 文件)。

-RUN wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/latest/download/riscv32-elf-ubuntu-24.04-gcc.tar.xz \
-    && tar -xvf riscv32-elf-ubuntu-24.04-gcc.tar.xz -C /opt/ \
-    && rm riscv32-elf-ubuntu-24.04-gcc.tar.xz
+RUN wget https://github.com/riscv-collab/riscv-gnu-toolchain/releases/download/2025.11.27-nightly/riscv32-elf-ubuntu-24.04-gcc.tar.xz \
+    && echo "6c440ea909e41ab7ef6c8007abb993b7bd5db32fdac5bbe177ac6f79e202ebe9  riscv32-elf-ubuntu-24.04-gcc.tar.xz" | sha256sum -c - \
+    && tar -xvf riscv32-elf-ubuntu-24.04-gcc.tar.xz -C /opt/ \
+    && rm riscv32-elf-ubuntu-24.04-gcc.tar.xz
📜 Review details

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f6fe92b and fd353ca.

📒 Files selected for processing (3)
  • .devcontainer/devcontainer.json (1 hunks)
  • .gitignore (1 hunks)
  • Dockerfile (1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-12-17T16:33:54.232Z
Learnt from: qzhhhi
Repo: Alliance-Algorithm/librmcs PR: 3
File: Dockerfile:99-99
Timestamp: 2025-12-17T16:33:54.232Z
Learning: In the librmcs project, the devcontainer uses a fixed workspace path `/workspaces/librmcs` that is hardcoded by design and won't change.

Applied to files:

  • .devcontainer/devcontainer.json
  • Dockerfile
🔇 Additional comments (5)
.gitignore (1)

5-7: LGTM!

正确地忽略了构建生成的 compile_commands.json 和容器内的 zsh 历史文件,与 Dockerfile 中的 .zsh_history 符号链接配置保持一致。

.devcontainer/devcontainer.json (2)

6-12: privileged: true 与完整 /dev 挂载组合存在安全风险。

完整挂载 /dev 加上特权模式会将主机设备完全暴露给容器。如果仅需 USB 设备访问(用于嵌入式开发),建议限制挂载范围或使用 --device 指定具体设备。

如果确实需要完整设备访问权限,请确认这是有意为之。


38-58: LGTM!

VS Code 设置和扩展配置合理:禁用端口自动转发与 --network=host 配合正确,扩展选择覆盖了 C++ 嵌入式开发所需的语言支持和工具。

Dockerfile (2)

79-86: LGTM!

将清华镜像配置保存为 .bak 后缀是一个很好的设计,用户可以根据需要重命名启用,而不影响默认行为。


88-99: LGTM!

用户配置和 Oh My Zsh 安装正确。根据项目约定,/workspaces/librmcs 是固定路径,硬编码是合理的。

Comment thread Dockerfile
Comment on lines +68 to +77
# Install latest clangd
RUN wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc \
&& echo "deb https://apt.llvm.org/noble/ llvm-toolchain-noble main" > /etc/apt/sources.list.d/llvm.list \
&& apt-get update \
&& version=$(apt-cache search clangd- | grep clangd- | awk -F' ' '{print $1}' | sort -V | tail -1 | cut -d- -f2) \
&& apt-get install -y --no-install-recommends clangd-$version \
&& apt-get autoremove -y \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* \
&& update-alternatives --install /usr/bin/clangd clangd /usr/bin/clangd-$version 50
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

clangd 版本检测缺少错误处理。

如果 apt-cache search 未返回结果,$version 将为空,导致 apt-get install clangd- 失败或产生意外行为。

     && version=$(apt-cache search clangd- | grep clangd- | awk -F' ' '{print $1}' | sort -V | tail -1 | cut -d- -f2) \
+    && [ -n "$version" ] || { echo "Failed to detect clangd version"; exit 1; } \
     && apt-get install -y --no-install-recommends clangd-$version \
🤖 Prompt for AI Agents
In Dockerfile around lines 68 to 77, the shell pipeline that sets version from
`apt-cache search clangd-` has no error handling so if no results are returned
`$version` will be empty and subsequent `apt-get install` and
`update-alternatives` will fail or behave unexpectedly; change the RUN step to
validate the computed $version (e.g., test -n "$version"), and if empty either
select a safe default package name or abort with a clear error (exit 1) and
message; only run `apt-get install clangd-$version` and `update-alternatives`
when $version is non-empty, and cleanly handle failures (log and exit) to avoid
continuing the build with an invalid state.

@qzhhhi qzhhhi merged commit fd353ca into dev/v3 Dec 17, 2025
1 check passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in RMCS Slave SDK Dec 17, 2025
@qzhhhi qzhhhi deleted the feature/devcontainer branch December 17, 2025 16:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant