Compare commits

...

5 Commits

Author SHA1 Message Date
dc947beafa Add Go support to dev containers and Makefile 2026-02-25 14:07:16 +08:00
d04243817e Add shellcheck, tmux, and jq to dev Dockerfiles 2026-01-10 20:42:44 +08:00
3f6384325a feat(devcontainer): add enhanced Maven development environment with Java 21, Bun, and SSH support
- Add Docker Compose configuration for Maven + Java 21 development container
- Include SSH server support for remote development (Zed editor compatible)
- Install Bun package manager and opencode-ai for enhanced development workflow
- Provide Makefile with commands for build, up, down, restart, logs, shell, ssh, and clean
- Add .env.example template for configurable ports and paths
- Include .gitignore for Java/Maven artifacts and IDE files
- Configure USTC mirrors for faster package installation
- Support persistent volume mounting for root user configuration (.ssh, .m2 cache)
2025-12-27 00:43:28 +08:00
b250a3693b fix: keep bun, opencode coommand in env 2025-12-25 17:27:28 +08:00
35034f5e2d Remove .env from tracking 2025-12-25 17:26:32 +08:00
11 changed files with 213 additions and 66 deletions

View File

@@ -0,0 +1,20 @@
# ==============================================
# Maven + Java + Bun 开发容器配置模板
# 使用方法: 复制此文件为 .env 并修改具体值
# cp .env.example .env
# ==============================================
# === 端口配置 ===
# SSH 连接端口 (宿主机)
SSH_PORT=2222
# Web 应用端口 (宿主机)
APP_PORT=9090
# === 路径配置 ===
# 项目源码挂载路径 (本地路径)
PROJECT_DIR=/workspace
# === 镜像信息 ===
IMAGE_NAME=maven-java21-bun-dev
IMAGE_TAG=1.0
CONTAINER_NAME=maven-devcontainer

View File

@@ -0,0 +1,20 @@
# === 敏感配置与本地环境 ===
.env
.DS_Store
# === Java/Maven 编译产物 ===
target/
*.class
*.jar
*.war
*.ear
*.log
# === IDE 配置文件 (可选,视团队规范而定) ===
.idea/
.vscode/
*.iml
.classpath
.project
.settings/

View File

@@ -0,0 +1,55 @@
FROM maven:3.9-eclipse-temurin-21
# 1. 优化 APT源 (保持你原有的逻辑,但增加了错误处理的安全性)
# 注意eclipse-temurin-21 基于 Ubuntusources 路径正确
RUN sed -i 's/security.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list.d/ubuntu.sources && \
sed -i 's@//.*archive.ubuntu.com@//mirrors.ustc.edu.cn@g' /etc/apt/sources.list.d/ubuntu.sources && \
sed -i 's/http:/https:/g' /etc/apt/sources.list.d/ubuntu.sources
# 2. 安装基础工具 + SSH Server
#添加 openssh-server, locales, sudo 等开发常用包
RUN apt-get update && \
apt-get install -y \
git curl vim ripgrep unzip netcat-openbsd bash-completion \
openssh-server locales sudo wget && \
# 清理缓存以减小镜像体积
rm -rf /var/lib/apt/lists/*
# ============================
# 2. 安装 Bun 和 Opencode
# ============================
# [关键] 设置 BUN_INSTALL 到 /opt 目录,防止被 /root 挂载卷覆盖
ENV BUN_INSTALL="/opt/bun"
ENV PATH="$BUN_INSTALL/bin:$PATH"
# 安装 Bun
RUN curl -fsSL https://bun.sh/install | bash
# 使用 Bun 安装 opencode
RUN bun install -g opencode-ai
# 3. 配置 SSH 服务
RUN mkdir /var/run/sshd && \
# 允许 root 登录 (Zed 需要连接用户)
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config && \
# 修复 SSH 登录后的环境变量问题
sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd
# 5. 安装 Opencode (Vibe Coding 工具)
# 假设 Opencode 是一个可以通过 curl 下载的二进制文件。
# 请将下方的 URL 替换为 Opencode 的真实下载链接。
# 如果 Opencode 是 npm 包,则需要先安装 nodejs。
# 这里演示二进制安装模式:
# RUN curl -L "https://path/to/opencode-linux-x64" -o /usr/local/bin/opencode && \
# chmod +x /usr/local/bin/opencode
# 6. 设置工作目录
WORKDIR /workspace
# 7. 配置容器启动命令
# 启动 SSH 服务并挂起容器
CMD ["/bin/sh", "-c", "/usr/sbin/sshd -D"]

View File

@@ -0,0 +1,62 @@
# 引用 .env 文件中的变量
include .env
export $(shell sed 's/=.*//' .env)
.PHONY: help build up down restart logs shell clean ssh
# 默认目标:显示帮助
help:
@echo "🛠️ Maven + Java + Bun 开发环境管理工具"
@echo "=================================================="
@echo "make build - 构建镜像"
@echo "make up - 启动容器 (后台模式)"
@echo "make down - 停止并移除容器"
@echo "make restart - 重启容器"
@echo "make logs - 查看容器日志"
@echo "make shell - 进入容器终端 (Bash)"
@echo "make ssh - 使用 SSH 连接容器 (测试连接)"
@echo "make clean - ⚠️ 深度清理 (移除容器、镜像和挂载的数据目录)"
@echo "=================================================="
@echo "当前配置:"
@echo " SSH端口: $(SSH_PORT)"
@echo " 代码路径: $(PROJECT_DIR)"
# 构建镜像
build:
@echo "📦 正在构建镜像 $(IMAGE_NAME):$(IMAGE_TAG)..."
@docker compose build
# 启动容器
up:
@echo "🚀 正在启动开发环境..."
@docker compose up -d
@echo "✅ 服务已启动!"
@echo " - SSH 连接: ssh -p $(SSH_PORT) root@localhost (密码: root)"
@echo " - Zed 连接: ssh://root@localhost:$(SSH_PORT)"
# 停止容器
down:
@echo "🛑 正在停止容器..."
@docker compose down
# 重启
restart: down up
# 查看日志
logs:
@docker compose logs -f
# 进入 Shell (通过 Docker exec)
shell:
@docker exec -it $(CONTAINER_NAME) bash
# SSH 连接测试
ssh:
@ssh -p $(SSH_PORT) -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null root@localhost
# 深度清理 (危险操作)
clean: down
@echo "⚠️ 正在清理..."
@read -p "确定要删除持久化数据目录 $(DATA_DIR) 吗? [y/N] " ans && [ $${ans:-N} = y ] || (echo "已取消"; exit 1)
@rm -rf $(DATA_DIR)
@echo "🧹 清理完成"

View File

@@ -0,0 +1,17 @@
services:
maven-dev-env:
image: maven-java21-dev:3.9-ssh
build:
context: .
dockerfile: Containerfile
container_name: maven-devcontainer
hostname: maven-devcontainer
restart: always
ports:
- "${APP_PORT:-9090}:9090"
- "${SSH_PORT:-2322}:22" # [新增] SSH 端口,宿主机通过 localhost:2222 连接
volumes:
- ${PROJECT_DIR:-/workspace}:/workspace
# 持久化 Root 用户配置(包含 .ssh, .m2 缓存, .config/zed 等)
- /workspace/devcontainer-vols/maven-devcontainer-root:/root

View File

@@ -1,52 +0,0 @@
# =================================================================
# Vibe Coding 开发环境 - 环境变量模板
# 使用说明:
# 1. 复制此文件并重命名为 .env
# 2. 在 .env 中填写你真实的 API Key 和配置
# 3. 注意:不要将 .env 文件上传到 Git 仓库!
# =================================================================
# --- [SSH 配置] ---
# 宿主机连接容器时使用的端口
SSH_PORT=2222
# --- [AI 工具 API Keys] ---
# Aider / OpenCode-AI 需要使用的模型密钥
# 常用模型提供商Anthropic (Claude), OpenAI (GPT), 智谱AI (GLM)
# Anthropic API (用于 Claude 3.5 Sonnet, Aider 强烈推荐)
ANTHROPIC_API_KEY=your_anthropic_key_here
# OpenAI API (用于 GPT-4o 等)
OPENAI_API_KEY=your_openai_key_here
# OpenCode-AI 专用 (例如智谱 AI Key)
OPENCODE_API_KEY=your_opencode_provider_key_here
# --- [开发环境设置 (可选)] ---
# 你可以在这里指定镜像构建时使用的版本(如果 Dockerfile 支持 args
RUST_VERSION=stable
BUN_VERSION=latest
# --- [其他工具配置] ---
# 例如某些工具需要的环境变量
# GITHUB_TOKEN=your_github_pat_here
# 开发者信息
# GIT_USER_NAME=YourName
# GIT_USER_EMAIL=youremail@example.com
# --- [路径配置] ---
# 宿主机项目代码目录 (默认为当前目录 .)
HOST_PROJECT_PATH=/workspace
# 容器内的工作目录绝对路径 (建议保持为 /workspace)
CONTAINER_WORKSPACE_PATH=/workspace
# --- [SSH 密钥配置] ---
# 指定你宿主机上的私钥文件名 (例如 id_ed25519 或 id_rsa)
SSH_KEY_NAME=id_ed25519
# 宿主机上公钥的完整路径 (用于 Compose 挂载)
SSH_PUB_KEY_PATH=~/.ssh/id_ed25519.pub

View File

@@ -2,21 +2,28 @@ FROM fedora:latest
# 1. 系统工具与编译环境 # 1. 系统工具与编译环境
RUN dnf update -y && dnf install -y \ RUN dnf update -y && dnf install -y \
curl git wget unzip procps-ng \ curl git wget unzip procps-ng shellcheck\
gcc gcc-c++ make cmake openssl-devel \ gcc gcc-c++ make cmake openssl-devel jq tmux vim\
zsh sudo python3 python3-pip \ zsh sudo python3 python3-pip \
openssh-server tar gzip \ openssh-server tar gzip \
&& dnf clean all && dnf clean all
# 2. 环境变量配置 # 2. 环境变量配置
ENV CARGO_HOME=/usr/local/cargo \ ENV CARGO_HOME=/usr/local/cargo \
RUSTUP_HOME=/usr/local/rustup \ RUSTUP_HOME=/usr/local/rustup \
BUN_INSTALL="/root/.bun" BUN_INSTALL="/root/.bun" \
ENV PATH="$BUN_INSTALL/bin:/usr/local/cargo/bin:${PATH}" GOPATH="/root/go"
ENV PATH="$BUN_INSTALL/bin:/usr/local/cargo/bin:/usr/local/go/bin:$GOPATH/bin:${PATH}"
# 3. 安装 Rust & Bun RUN chsh -s /bin/zsh root
# 3. 安装 Rust & Bun & Go
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
RUN curl -fsSL https://bun.sh/install | bash RUN curl -fsSL https://bun.sh/install | bash
RUN curl -fsSL https://go.dev/dl/go1.24.4.linux-amd64.tar.gz | tar -C /usr/local -xzf - \
&& go install golang.org/x/tools/gopls@latest \
&& go install github.com/go-delve/delve/cmd/dlv@latest
# 4. 全局安装 AI 工具 (Vibe Coding 核心) # 4. 全局安装 AI 工具 (Vibe Coding 核心)
RUN bun install -g opencode-ai RUN bun install -g opencode-ai
@@ -26,7 +33,7 @@ RUN ssh-keygen -A && \
mkdir -p /root/.zed /root/.local/share/zed && \ mkdir -p /root/.zed /root/.local/share/zed && \
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# 6. Shell 美化 RUN sed -i 's/#PermitUserEnvironment no/PermitUserEnvironment yes/' /etc/ssh/sshd_config
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
# 接收来自 compose 的参数 # 接收来自 compose 的参数

View File

@@ -2,20 +2,25 @@ FROM fedora:latest
# 1. 基础工具 (包含 Zed 远程运行所需的 procps-ng, tar) # 1. 基础工具 (包含 Zed 远程运行所需的 procps-ng, tar)
RUN dnf update -y && dnf install -y \ RUN dnf update -y && dnf install -y \
curl git wget unzip procps-ng tar gzip \ curl git wget unzip procps-ng tar gzip shellcheck vim\
gcc gcc-c++ make cmake openssl-devel \ gcc gcc-c++ make cmake openssl-devel tmux jq\
zsh sudo python3 python3-pip openssh-server \ zsh sudo python3 python3-pip openssh-server \
&& dnf clean all && dnf clean all
# 2. 环境变量 # 2. 环境变量
ENV CARGO_HOME=/usr/local/cargo \ ENV CARGO_HOME=/usr/local/cargo \
RUSTUP_HOME=/usr/local/rustup \ RUSTUP_HOME=/usr/local/rustup \
BUN_INSTALL="/root/.bun" BUN_INSTALL="/root/.bun" \
ENV PATH="$BUN_INSTALL/bin:/usr/local/cargo/bin:${PATH}" GOPATH="/root/go"
ENV PATH="$BUN_INSTALL/bin:/usr/local/cargo/bin:/usr/local/go/bin:$GOPATH/bin:${PATH}"
RUN chsh -s /bin/zsh root
# 3. 安装 Rust & Bun # 3. 安装 Rust & Bun & Go
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
RUN curl -fsSL https://bun.sh/install | bash RUN curl -fsSL https://bun.sh/install | bash
RUN curl -fsSL https://go.dev/dl/go1.24.4.linux-amd64.tar.gz | tar -C /usr/local -xzf - \
&& go install golang.org/x/tools/gopls@latest \
&& go install github.com/go-delve/delve/cmd/dlv@latest
# 4. 全局安装 AI 工具 # 4. 全局安装 AI 工具
RUN bun install -g opencode-ai RUN bun install -g opencode-ai
@@ -25,6 +30,7 @@ RUN ssh-keygen -A && \
mkdir -p /root/.zed /root/.local/share/zed && \ mkdir -p /root/.zed /root/.local/share/zed && \
sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
RUN sed -i 's/#PermitUserEnvironment no/PermitUserEnvironment yes/' /etc/ssh/sshd_config
# 6. Shell 美化 # 6. Shell 美化
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended

View File

@@ -30,6 +30,7 @@ restart: down up
build: build:
@echo "🛠️ 正在重新构建镜像..." @echo "🛠️ 正在重新构建镜像..."
docker compose build --no-cache docker compose build --no-cache
docker compose down
docker compose up -d docker compose up -d
# --- 连接与交互 --- # --- 连接与交互 ---
@@ -64,6 +65,7 @@ logs:
check: check:
@echo "🔍 执行工具自检..." @echo "🔍 执行工具自检..."
@docker exec -it $(CONTAINER_NAME) rustc --version || echo "❌ Rust 未就绪" @docker exec -it $(CONTAINER_NAME) rustc --version || echo "❌ Rust 未就绪"
@docker exec -it $(CONTAINER_NAME) go version || echo "❌ Go 未就绪"
@docker exec -it $(CONTAINER_NAME) bun --version || echo "❌ Bun 未就绪" @docker exec -it $(CONTAINER_NAME) bun --version || echo "❌ Bun 未就绪"
@docker exec -it $(CONTAINER_NAME) aider --version || echo "❌ Aider 未就绪" @docker exec -it $(CONTAINER_NAME) aider --version || echo "❌ Aider 未就绪"
@docker exec -it $(CONTAINER_NAME) opencode version || echo "❌ OpenCode 未就绪" @docker exec -it $(CONTAINER_NAME) opencode version || echo "❌ OpenCode 未就绪"

View File

@@ -3,34 +3,42 @@ services:
build: build:
context: . context: .
# 根据需要切换 Dockerfile.fedora 或 Dockerfile.ubuntu # 根据需要切换 Dockerfile.fedora 或 Dockerfile.ubuntu
dockerfile: Dockerfile.fedora dockerfile: Dockerfile.fedora
args: args:
# 将路径传递给 Dockerfile 阶段 # 将路径传递给 Dockerfile 阶段
CONTAINER_WORKSPACE: ${CONTAINER_WORKSPACE_PATH:-/workspace} CONTAINER_WORKSPACE: ${CONTAINER_WORKSPACE_PATH:-/workspace}
container_name: vibe-coding-env container_name: vibe-coding-env
hostname: vibe-coding
env_file: .env env_file: .env
environment: environment:
# 让容器内部程序也能感知工作目录 # 让容器内部程序也能感知工作目录
- WORKSPACE=${CONTAINER_WORKSPACE_PATH:-/workspace} - WORKSPACE=${CONTAINER_WORKSPACE_PATH:-/workspace}
- SHELL=/bin/zsh
- TERM=xterm-256color
ports: ports:
- "${SSH_PORT:-2222}:22" - "${SSH_PORT:-2222}:22"
- "6300:3000" - "6300:3000"
- "6173-6175:5173-5175" - "6173-6175:5173-5175"
volumes: volumes:
- ${HOST_PROJECT_PATH:-.}:${CONTAINER_WORKSPACE_PATH:-/workspace}:cached - ${HOST_PROJECT_PATH:-.}:${CONTAINER_WORKSPACE_PATH:-/workspace}:cached
# 1. 使用命名卷持久化整个 .ssh 目录
- ssh-data:/root/.ssh
- ${SSH_PUB_KEY_PATH:-~/.ssh/id_ed25519.pub}:/tmp/host_id_ed25519.pub:ro - ${SSH_PUB_KEY_PATH:-~/.ssh/id_ed25519.pub}:/tmp/host_id_ed25519.pub:ro
- cargo-cache:/usr/local/cargo/registry - cargo-cache:/usr/local/cargo/registry
- bun-cache:/root/.bun/install/cache - bun-cache:/root/.bun/install/cache
- go-cache:/root/go
# --- 新增Zed 远程开发缓存 --- # --- 新增Zed 远程开发缓存 ---
- zed-server:/root/.zed - zed-server:/root/.zed_server
- zed-share:/root/.local/share/zed - zed-share:/root/.local/share/zed
stdin_open: true stdin_open: true
tty: true tty: true
restart: always restart: always
volumes: volumes:
ssh-data:
cargo-cache: cargo-cache:
bun-cache: bun-cache:
go-cache:
zed-server: zed-server:
zed-share: zed-share:

View File

@@ -5,7 +5,9 @@ set -e
mkdir -p /root/.ssh mkdir -p /root/.ssh
chmod 700 /root/.ssh chmod 700 /root/.ssh
touch /root/.ssh/authorized_keys touch /root/.ssh/authorized_keys
touch /root/.ssh/environment
printenv | grep -E '^(PATH|BUN|CARGO|RUST|ANTHROPIC|OPENAI|OPENCODE|TERM|SHELL|WORKSPACE)' > /root/.ssh/environment
# 2. 动态注入挂载的公钥 (解决只读冲突的核心逻辑) # 2. 动态注入挂载的公钥 (解决只读冲突的核心逻辑)
if [ -f /tmp/host_id_ed25519.pub ]; then if [ -f /tmp/host_id_ed25519.pub ]; then
echo "检测到挂载的公钥,正在注入..." echo "检测到挂载的公钥,正在注入..."
@@ -20,7 +22,7 @@ fi
# 3. 强制修复权限 (SSH 对此非常敏感) # 3. 强制修复权限 (SSH 对此非常敏感)
chmod 600 /root/.ssh/authorized_keys chmod 600 /root/.ssh/authorized_keys
chown root:root /root/.ssh/authorized_keys chown -R root:root /root/.ssh
# 根据不同 OS 启动 SSH # 根据不同 OS 启动 SSH