This commit is contained in:
panshuxiao
2025-04-06 19:52:57 +08:00
commit adefecc0bd
5 changed files with 124 additions and 0 deletions

36
.devcontainer/Dockerfile Normal file
View File

@@ -0,0 +1,36 @@
FROM golang:1.21-bullseye
# 安装基本工具
RUN apt-get update && apt-get install -y \
git \
curl \
wget \
vim \
sudo \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# 创建一个非root用户
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
# Go工具设置
ENV GOPATH=/go
ENV PATH=$GOPATH/bin:/usr/local/go/bin:$PATH
RUN mkdir -p "$GOPATH/src" "$GOPATH/bin" "$GOPATH/pkg" \
&& chmod -R 777 "$GOPATH" \
&& go install -v golang.org/x/tools/gopls@latest \
&& go install -v github.com/go-delve/delve/cmd/dlv@latest \
&& go install -v honnef.co/go/tools/cmd/staticcheck@latest
# 设置工作目录
WORKDIR /workspace
# 使用创建的非root用户
USER $USERNAME

View File

@@ -0,0 +1,23 @@
{
"name": "Go Dev Environment",
"build": {
"dockerfile": "Dockerfile",
"context": ".."
},
"customizations": {
"vscode": {
"settings": {
"go.toolsManagement.checkForUpdates": "local",
"go.useLanguageServer": true,
"go.gopath": "/go"
},
"extensions": [
"golang.go",
"ms-vscode.go",
]
}
},
"forwardPorts": [8080],
"postCreateCommand": "go mod tidy",
"remoteUser": "vscode"
}

26
README.md Normal file
View File

@@ -0,0 +1,26 @@
# DevContainer测试应用
这是一个简单的Go应用用于测试DevContainer功能。
## 功能
- 提供一个基本的HTTP服务器
- 显示服务器时间和请求信息
- 包含健康检查端点(/health)
## 使用方法
1. 在DevStar中启动该DevContainer
2. 容器启动后访问映射的端口默认8080
3. 你应该能看到一个简单的问候页面
## 开发
DevContainer已经配置好了Go开发环境包括
- Go语言支持
- 代码补全
- 调试工具
- 常用命令行工具
只需在VS Code中打开或通过DevStar提供的编辑器接口访问。

3
go.mod Normal file
View File

@@ -0,0 +1,3 @@
module test-devcontainer
go 1.23.3

36
main.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
)
func main() {
// 获取环境变量或使用默认值
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
// 创建一个简单的HTTP服务器
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
hostname, _ := os.Hostname()
fmt.Fprintf(w, "Hello from DevContainer!\n")
fmt.Fprintf(w, "Server time: %s\n", time.Now().Format(time.RFC1123))
fmt.Fprintf(w, "Hostname: %s\n", hostname)
fmt.Fprintf(w, "Remote Address: %s\n", r.RemoteAddr)
fmt.Fprintf(w, "User-Agent: %s\n", r.UserAgent())
})
http.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "Healthy")
})
// 启动服务器
log.Printf("Starting server on port %s", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}