Skip to content

Commit 9dccc84

Browse files
committed
build: refactor: container setup and development environment configuration
- Switch base Docker image from `mcr.microsoft.com/devcontainers/base:ubuntu` to `ubuntu:22.04` and remove Nix-related setup. - Add user creation logic to the Dockerfile, including sudo configuration and path mapping for host user directories. - Expand the list of installed utilities in the Dockerfile, adding `ripgrep`, `fd-find`, `tree`, `jq`, `wget`, `git`, `ca-certificates`, `gnupg`, and `nodejs`. - Incorporate `uv` package manager installation and `Claude Code` setup into the Dockerfile. - Configure `devcontainer.json` to pass the host user as an argument. - Add mount configurations for SSH keys, Git, Claude Code, and Downloads directories. - Configure `devcontainer.json` to use host network, set `TERM` environment variable, and specify VSCode extensions for Python, ESlint, and Prettier. - Remove the `post-create.sh` script.
1 parent e79f9ce commit 9dccc84

File tree

3 files changed

+127
-66
lines changed

3 files changed

+127
-66
lines changed

.devcontainer/Dockerfile

Lines changed: 83 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,85 @@
1-
FROM mcr.microsoft.com/devcontainers/base:ubuntu
1+
FROM ubuntu:22.04
22

3-
# Install Nix
3+
# 避免交互式提示
4+
ENV DEBIAN_FRONTEND=noninteractive
5+
ENV TZ=Asia/Shanghai
6+
7+
# ============================================================================
8+
# ARG 声明区域:集中管理所有构建参数
9+
# ============================================================================
10+
ARG USERNAME=node
11+
ARG USER_UID=1000
12+
ARG USER_GID=1000
13+
ARG HOST_USER
14+
15+
# ============================================================================
16+
# 用户创建:Ubuntu 无预置 node 用户,需手动创建
17+
# ============================================================================
18+
RUN \
19+
# 创建开发用户组和用户
20+
groupadd --gid ${USER_GID} ${USERNAME} \
21+
&& useradd --uid ${USER_UID} --gid ${USER_GID} --shell /bin/bash --create-home ${USERNAME} \
22+
# 安装 sudo 并配置权限
23+
&& apt-get update \
24+
&& apt-get install -y sudo \
25+
&& echo "${USERNAME} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/${USERNAME} \
26+
&& chmod 0440 /etc/sudoers.d/${USERNAME} \
27+
&& rm -rf /var/lib/apt/lists/* \
28+
# 路径映射:解决 macOS 配置文件中的绝对路径问题
29+
# Claude Code 等工具的配置文件可能包含宿主机绝对路径
30+
# 通过软链接让容器内可以解析 /Users/$HOST_USER 和 /home/$HOST_USER
31+
&& ([ -z "${HOST_USER}" ] || \
32+
(mkdir -p /home /Users && \
33+
ln -snf /home/${USERNAME} /home/${HOST_USER} && \
34+
ln -snf /home/${USERNAME} /Users/${HOST_USER}))
35+
36+
# ============================================================================
37+
# 基础工具安装:命令行工具和开发依赖
38+
# ============================================================================
439
RUN apt-get update && apt-get install -y \
5-
curl \
6-
xz-utils \
7-
sudo \
8-
&& rm -rf /var/lib/apt/lists/*
9-
10-
# Create vscode user if it doesn't exist
11-
RUN if ! id -u vscode > /dev/null 2>&1; then \
12-
useradd -m -s /bin/bash vscode && \
13-
echo "vscode ALL=(ALL) NOPASSWD:ALL" >> /etc/sudoers; \
14-
fi
15-
16-
# Switch to vscode user for Nix installation
17-
USER vscode
18-
WORKDIR /home/vscode
19-
20-
# Install Nix in single-user mode
21-
RUN curl -L https://nixos.org/nix/install | sh -s -- --no-daemon
22-
23-
# Add Nix to PATH and configure for the shell
24-
RUN echo '. /home/vscode/.nix-profile/etc/profile.d/nix.sh' >> /home/vscode/.bashrc && \
25-
mkdir -p /home/vscode/.config/nix && \
26-
echo 'experimental-features = nix-command flakes' >> /home/vscode/.config/nix/nix.conf
27-
28-
# Keep container running
29-
CMD ["sleep", "infinity"]
40+
# 命令行工具
41+
ripgrep \
42+
fd-find \
43+
tree \
44+
jq \
45+
curl \
46+
wget \
47+
git \
48+
ca-certificates \
49+
gnupg \
50+
&& mkdir -p /etc/apt/keyrings \
51+
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg \
52+
&& echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list \
53+
&& apt-get update \
54+
&& apt-get install -y nodejs \
55+
# 彻底清理 APT 缓存、临时文件和文档
56+
&& apt-get clean \
57+
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* /usr/share/man/*
58+
59+
# ============================================================================
60+
# Python:使用 uv 安装器(全局单次安装)
61+
# ============================================================================
62+
RUN curl -LsSf https://astral.sh/uv/install.sh | sh \
63+
&& mv /root/.local/bin/uv* /usr/local/bin/ \
64+
&& rm -rf /root/.local /root/.cargo /root/.rustup
65+
ENV PATH="/usr/local/bin:${PATH}"
66+
67+
# ============================================================================
68+
# Claude Code 安装
69+
# ============================================================================
70+
RUN npm install -g \
71+
@anthropic-ai/claude-code \
72+
# 清理 npm 缓存和文档
73+
&& npm cache clean --force \
74+
&& rm -rf ~/.npm
75+
76+
# ============================================================================
77+
# 环境变量配置
78+
# ============================================================================
79+
80+
# 工作目录
81+
WORKDIR /workspace
82+
RUN chown ${USERNAME}:${USERNAME} /workspace
83+
84+
# 切换到开发用户
85+
USER ${USERNAME}

.devcontainer/devcontainer.json

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,55 @@
11
{
2-
"name": "claudecode.nvim Development",
2+
"name": "nanodev-claude-code",
33
"build": {
4-
"dockerfile": "Dockerfile"
4+
"dockerfile": "Dockerfile",
5+
"args": {
6+
"HOST_USER": "${localEnv:USER}"
7+
}
58
},
6-
"features": {
7-
"ghcr.io/devcontainers/features/git:1": {}
9+
10+
// 挂载配置:直接映射用户配置文件
11+
"mounts": [
12+
// SSH 密钥和配置(只读,安全第一)
13+
"source=${localEnv:HOME}/.ssh,target=/home/node/.ssh,type=bind,readonly",
14+
15+
// Git 配置(可写,需要在容器内提交)
16+
"source=${localEnv:HOME}/.gitconfig,target=/home/node/.gitconfig,type=bind",
17+
18+
// Claude Code 配置(路径映射解决 macOS 绝对路径问题)
19+
"source=${localEnv:HOME}/.claude,target=/home/node/.claude,type=bind",
20+
21+
// 下载目录
22+
"source=${localEnv:HOME}/Downloads,target=/home/node/Downloads,type=bind"
23+
],
24+
25+
// 容器运行时设置
26+
"runArgs": [
27+
// 使用主机网络:容器和主机共享网络栈
28+
"--network=host"
29+
],
30+
31+
// 环境变量
32+
"containerEnv": {
33+
"TERM": "xterm-256color"
834
},
35+
36+
// 容器启动后执行的命令(可选:安装额外工具)
37+
"postCreateCommand": "sudo npm install -g @anthropic-ai/claude-code",
38+
39+
// VS Code 扩展(推荐的开发工具扩展)
940
"customizations": {
1041
"vscode": {
42+
"extensions": [
43+
"ms-python.python",
44+
"dbaeumer.vscode-eslint",
45+
"esbenp.prettier-vscode"
46+
],
1147
"settings": {
1248
"terminal.integrated.defaultProfile.linux": "bash"
13-
},
14-
"extensions": ["jnoortheen.nix-ide"]
49+
}
1550
}
1651
},
17-
"postCreateCommand": "bash .devcontainer/post-create.sh",
18-
"remoteUser": "vscode"
52+
53+
// 使用开发用户(与宿主机 UID/GID 匹配)
54+
"remoteUser": "node"
1955
}

.devcontainer/post-create.sh

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)