Dockerfile 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Build Stage
  2. FROM golang:1.24-alpine AS builder
  3. # 1. 设置 Alpine 国内镜像源 (阿里云)
  4. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  5. # 2. 安装必要构建工具 (如 git, 因为 go mod 可能需要)
  6. RUN apk add --no-cache git postgresql-client
  7. WORKDIR /app
  8. # Copy dependency files
  9. COPY go.mod ./
  10. COPY go.sum ./
  11. # 3. 设置 Go 国内代理
  12. RUN go env -w GOPROXY=https://goproxy.io,direct
  13. # Download dependencies first to cache them
  14. RUN go mod download
  15. # Copy all source code
  16. COPY . .
  17. # Run tidy to ensure everything is consistent
  18. RUN go mod tidy
  19. # Build the binary
  20. RUN CGO_ENABLED=0 GOOS=linux go build -o ems-server .
  21. # Runtime Stage
  22. FROM alpine:latest
  23. # 4. 运行时镜像也设置国内源 (方便排查问题安装工具)
  24. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  25. # 设置时区为上海,并安装 postgresql-client
  26. RUN apk add --no-cache tzdata postgresql-client && \
  27. cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
  28. echo "Asia/Shanghai" > /etc/timezone
  29. WORKDIR /app
  30. COPY --from=builder /app/ems-server .
  31. COPY db/migrations /app/migrations
  32. EXPOSE 8080
  33. CMD ["./ems-server"]