Golang_demo/Dockerfile
2025-06-25 14:05:12 +08:00

37 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.

# 参考Java项目的成功模式使用单阶段构建直接复制Jenkins已构建的二进制文件
FROM alpine:latest
# 安装必要工具curl用于健康检查ca-certificates用于HTTPS请求
RUN apk add --no-cache curl ca-certificates tzdata
# 设置时区
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 创建非root用户
RUN addgroup -g 1000 goapp && adduser -u 1000 -G goapp -s /bin/sh -D goapp
# 创建应用目录
WORKDIR /app
# 直接复制已构建的二进制文件在Jenkins中已经通过go build构建完成
COPY golang-demo .
# 复制配置文件(如果存在)
COPY .env* ./
# 创建日志目录并设置权限
RUN mkdir -p /app/logs && chown -R goapp:goapp /app && chmod +x /app/golang-demo
# 切换到非root用户
USER goapp
# 暴露端口
EXPOSE 8080
# 健康检查 - 使用Go应用的标准端点
HEALTHCHECK --interval=30s --timeout=3s --start-period=30s --retries=3 \
CMD curl -f http://localhost:8080/health || curl -f http://localhost:8080/ping || exit 1
# 启动应用
ENTRYPOINT ["./golang-demo"]