Files
full/module/base/initial/Dockerfile

74 lines
1.6 KiB
Docker

# 使用官方Go镜像作为构建环境
FROM golang:1.26.5-alpine AS builder
# 设置工作目录
WORKDIR /app
# 安装必要的工具
RUN apk add --no-cache git ca-certificates tzdata
# 设置Go环境变量
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct \
GOPRIVATE=git.apinb.com/* \
GONOPROXY=git.apinb.com/* \
GOINSECURE=git.apinb.com/* \
GONOSUMDB=git.apinb.com/* \
GOEXPERIMENT=jsonv2
# 复制go.mod和go.sum文件
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download
# 复制源代码
COPY . .
# 构建应用
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build \
-ldflags='-w -s -extldflags "-static"' \
-a -installsuffix cgo \
-o initial \
./cmd/main/main.go
# 使用最小的alpine镜像作为运行环境
FROM alpine:latest
# 安装必要的包
RUN apk --no-cache add ca-certificates tzdata
# 设置时区
ENV TZ=Asia/Shanghai
# 创建非root用户
RUN addgroup -g 1001 -S appgroup && \
adduser -u 1001 -S appuser -G appgroup
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /app/initial .
# 复制配置文件和其他必要文件
COPY --from=builder /app/etc ./etc
COPY --from=builder /app/swagger ./swagger
# 创建日志目录
RUN mkdir -p /app/logs && \
chown -R appuser:appgroup /app
# 切换到非root用户
USER appuser
# 暴露端口
EXPOSE 12101 12102
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:12102/health || exit 1
# 启动应用
CMD ["./initial"]