gomog/Dockerfile

56 lines
1.1 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 构建阶段
FROM golang:1.21-alpine AS builder
# 安装必要的构建依赖
RUN apk add --no-cache git gcc musl-dev
# 设置工作目录
WORKDIR /build
# 复制 go.mod 和 go.sum
COPY go.mod go.sum ./
# 下载依赖
RUN go mod download
# 复制源代码
COPY . .
# 构建应用
RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o gomog ./cmd/server
# 运行阶段
FROM alpine:latest
# 安装运行时依赖SQLite 需要)
RUN apk add --no-cache ca-certificates libgcc
# 创建非 root 用户
RUN addgroup -g 1000 gomog && \
adduser -D -u 1000 -G gomog gomog
# 设置工作目录
WORKDIR /app
# 从构建阶段复制二进制文件
COPY --from=builder /build/gomog .
# 复制默认配置
COPY --from=builder /build/config.yaml.example ./config.yaml
# 创建数据目录
RUN mkdir -p /app/data && chown -R gomog:gomog /app
# 切换到非 root 用户
USER gomog
# 暴露端口
EXPOSE 8080 27017
# 健康检查
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
# 启动应用
CMD ["./gomog", "-config", "config.yaml"]