```
feat(devcontainer): 优化 Go 开发容器配置并预装 VS Code Server - 使用 ghcr.io/devcontainers/base:ubuntu 基础镜像并通过 Dev Containers Feature 安装 Go 1.25 - 在 Dockerfile 中预装 VS Code Server 避免运行时下载卡顿,支持 ARM64/AMD64 架构 - 自动安装 dlv 调试工具并配置 vscode 用户权限 - 更新 README 文档说明备用本地 Dockerfile 方案和预装 VS Code Server 选项 ```
This commit is contained in:
parent
52da1fc926
commit
4cbf3531bc
@ -1,16 +1,33 @@
|
||||
# Dev Container built locally to avoid registry blockers
|
||||
# Use official Golang image (Debian bookworm) for Go 1.25
|
||||
# Go 1.25 + VS Code Server (ARM64/AMD64) Dev Container
|
||||
FROM golang:1.25-bookworm
|
||||
|
||||
# Install essentials
|
||||
# 安装常用工具
|
||||
RUN apt-get update \
|
||||
&& apt-get install -y --no-install-recommends \
|
||||
git sudo ca-certificates curl unzip \
|
||||
&& rm -rf /var/lib/apt/lists/*
|
||||
|
||||
# Create vscode user
|
||||
# 创建 vscode 用户
|
||||
RUN useradd -m -s /bin/bash vscode \
|
||||
&& echo "vscode ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/90-vscode \
|
||||
&& chmod 0440 /etc/sudoers.d/90-vscode
|
||||
|
||||
WORKDIR /workspaces
|
||||
|
||||
# 预装 VS Code Server,避免运行时下载卡顿
|
||||
ARG VSCODE_COMMIT="994fd12f8d3a5aa16f17d42c041e5809167e845a"
|
||||
RUN set -eux; \
|
||||
arch=$(dpkg --print-architecture); \
|
||||
case "$arch" in \
|
||||
amd64) server_arch=linux-x64 ;; \
|
||||
arm64) server_arch=linux-arm64 ;; \
|
||||
*) echo "Unsupported architecture: $arch"; exit 1 ;; \
|
||||
esac; \
|
||||
mkdir -p /home/vscode/.vscode-server/bin/${VSCODE_COMMIT}; \
|
||||
curl -fsSL "https://update.code.visualstudio.com/commit:${VSCODE_COMMIT}/server-${server_arch}/stable" -o /tmp/vscode-server.tar.gz; \
|
||||
tar -xzf /tmp/vscode-server.tar.gz -C /home/vscode/.vscode-server/bin/${VSCODE_COMMIT} --strip-components=1; \
|
||||
chown -R vscode:vscode /home/vscode/.vscode-server
|
||||
|
||||
# 容器启动后自动安装 dlv 并下载依赖
|
||||
USER vscode
|
||||
RUN go install github.com/go-delve/delve/cmd/dlv@latest
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
{
|
||||
"name": "Go Dev Container (demo)",
|
||||
"build": {
|
||||
"dockerfile": "Dockerfile"
|
||||
"dockerfile": "Dockerfile",
|
||||
"args": {
|
||||
"VSCODE_COMMIT": "994fd12f8d3a5aa16f17d42c041e5809167e845a"
|
||||
}
|
||||
},
|
||||
"remoteUser": "vscode",
|
||||
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
|
||||
"postCreateCommand": "cd demo && go mod download && go install github.com/go-delve/delve/cmd/dlv@latest",
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"extensions": ["golang.go"],
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
- 打开文件夹:在 VS Code 中打开 `day01/devcontainer` 目录。
|
||||
- 重新在容器中打开:命令面板运行 “Dev Containers: Reopen in Container”。
|
||||
- 首次启动会执行依赖下载:`postCreateCommand` 会在 `demo/` 下执行 `go mod download`。
|
||||
- 已改为本地 Dockerfile 构建:基于官方 `golang:1.25-bookworm`,避免 MCR/GHCR 拉取受限。
|
||||
- 当前使用 `ghcr.io/devcontainers/base:ubuntu` 基础镜像,并通过 Dev Containers Feature 安装 Go(1.25)。若网络再遇阻,可切换到本地 Dockerfile 方案作为备用。
|
||||
|
||||
### 容器内开发
|
||||
- 代码位置:`/workspaces/<当前打开的文件夹>/demo`。
|
||||
@ -23,6 +23,10 @@
|
||||
- 直接在 “Run and Debug” 视图选择 “Debug demo/main.go” 或 “Debug demo tests”。
|
||||
- 断点与变量查看可在容器内正常使用。
|
||||
|
||||
### 预装 VS Code Server(可选,避免下载卡顿)
|
||||
- 如需避免容器运行时下载 VS Code Server,可切换到本地 Dockerfile 方案并在构建期预装 Server(见 [day01/devcontainer/.devcontainer/Dockerfile](day01/devcontainer/.devcontainer/Dockerfile))。
|
||||
- 也可以在宿主机下载后手动注入到容器:将 server 包复制到 `~/.vscode-server/bin/<commit>` 目录。
|
||||
|
||||
### 备注
|
||||
- 如需额外工具(如 `golangci-lint` 的安装或 `dlv` 调试器),可在 `.devcontainer/Dockerfile` 或 `postCreateCommand` 中添加。
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user