From 55c05ec41d51c0223e3feca27e768607a0bca045 Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 10 Apr 2026 16:56:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(docker):=20=E6=B7=BB=E5=8A=A0=E5=AE=B9?= =?UTF-8?q?=E5=99=A8=E5=8C=96=E9=83=A8=E7=BD=B2=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Dockerfile: 多阶段构建,最小化镜像体积(scratch) - docker-compose.yml: 开发环境配置,含健康检查 - .dockerignore: 排除不必要的构建上下文 Co-Authored-By: Claude Opus 4.6 --- .dockerignore | 78 ++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 51 ++++++++++++++++++++++++++++++ docker-compose.yml | 41 ++++++++++++++++++++++++ 3 files changed, 170 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..e1920fc --- /dev/null +++ b/.dockerignore @@ -0,0 +1,78 @@ +# Build artifacts +bin/ +dist/ +*.exe +*.exe~ +*.dll +*.so +*.dylib + +# Go cache and modules +vendor/ + +# Test artifacts +*.test +*.out +coverage.html +coverage.out + +# IDE and editor +.idea/ +.vscode/ +*.swp +*.swo +*~ + +# OS generated +.DS_Store +.DS_Store? +._* +.Spotlight-V100 +.Trashes +ehthumbs.db +Thumbs.db + +# Environment and secrets +.env +.env.local +.env.*.local + +# Logs +*.log +logs/ + +# Temporary +tmp/ +temp/ + +# Claude/OMC state +.omc/ +.claude/ +CLAUDE.md + +# Git submodule (large) +lua-nginx-module/ + +# Development configs (use production configs in container) +lolly.yaml +config.yaml + +# Compiled binary (will be built in container) +lolly + +# Benchmark and profiling data +default.pgo +benchmark-*.txt + +# Documentation (not needed in runtime) +docs/ + +# Scripts (not needed in runtime) +scripts/ + +# HTML test directory +html/ + +# Git +.git/ +.gitignore \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f53ca9e --- /dev/null +++ b/Dockerfile @@ -0,0 +1,51 @@ +# ---- Builder stage ---- +FROM golang:1.26-alpine AS builder + +WORKDIR /build + +# 安装构建依赖 +RUN apk add --no-cache git make + +# 依赖缓存层 +COPY go.mod go.sum ./ +RUN go mod download + +# 构建参数(版本信息) +ARG VERSION=0.2.0 +ARG GIT_COMMIT=unknown +ARG GIT_BRANCH=unknown +ARG BUILD_TIME=unknown +ARG GO_VERSION=unknown +ARG BUILD_PLATFORM=linux/amd64 + +# 构建 +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build \ + -ldflags="-s -w \ + -X 'rua.plus/lolly/internal/app.Version=${VERSION}' \ + -X 'rua.plus/lolly/internal/app.GitCommit=${GIT_COMMIT}' \ + -X 'rua.plus/lolly/internal/app.GitBranch=${GIT_BRANCH}' \ + -X 'rua.plus/lolly/internal/app.BuildTime=${BUILD_TIME}' \ + -X 'rua.plus/lolly/internal/app.GoVersion=${GO_VERSION}' \ + -X 'rua.plus/lolly/internal/app.BuildPlatform=${BUILD_PLATFORM}'" \ + -trimpath \ + -o /build/lolly \ + main.go + +# ---- Runtime stage ---- +FROM scratch + +# CA 证书(出站 HTTPS 代理需要) +COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ + +# 二进制文件 +COPY --from=builder /build/lolly /lolly + +# 优雅关闭:SIGQUIT 触发 30s graceful stop +STOPSIGNAL SIGQUIT + +# HTTP/1.1, HTTP/2, HTTP/3 (QUIC) +EXPOSE 8080/tcp 443/tcp 443/udp + +ENTRYPOINT ["/lolly"] +CMD ["-c", "/etc/lolly/lolly.yaml"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..cc19bbc --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,41 @@ +services: + lolly: + build: . + # 防止僵尸进程(lolly 无 PID 1 处理) + init: true + ports: + - "8080:8080" + # HTTP/3 (QUIC) - 需要 SSL 证书 + # - "443:443/tcp" + # - "443:443/udp" + volumes: + - ./lolly.yaml:/etc/lolly/lolly.yaml:ro + # 静态文件目录 + - ./html:/var/www/html:ro + # SSL 证书(可选) + # - ./certs:/etc/lolly/certs:ro + # 日志持久化(可选) + # - ./logs:/var/log/lolly + environment: + # 配合容器内存限制设置 GC 目标 + - GOMEMLIMIT=750MiB + # 资源限制(可选) + # deploy: + # resources: + # limits: + # memory: 1G + # reservations: + # memory: 256M + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "wget -q --spider http://localhost:8080/_status || exit 1"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s + + # 示例后端服务(用于反向代理测试) + # backend: + # image: nginx:alpine + # ports: + # - "8000:80" \ No newline at end of file