2025-08-05 15:08:07 +08:00
|
|
|
# Build stage
|
|
|
|
|
FROM golang:1.24-alpine AS builder
|
|
|
|
|
|
|
|
|
|
WORKDIR /app
|
|
|
|
|
|
|
|
|
|
# 通过构建参数接收敏感信息
|
|
|
|
|
ARG GOPRIVATE_ARG
|
|
|
|
|
ARG GOPROXY_ARG
|
|
|
|
|
ARG GOSUMDB_ARG=off
|
|
|
|
|
|
|
|
|
|
# 设置Go环境变量
|
|
|
|
|
ENV GOPRIVATE=${GOPRIVATE_ARG}
|
|
|
|
|
ENV GOPROXY=${GOPROXY_ARG}
|
|
|
|
|
ENV GOSUMDB=${GOSUMDB_ARG}
|
|
|
|
|
|
2025-09-18 14:40:07 +08:00
|
|
|
# Install dependencies
|
|
|
|
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
|
|
|
|
|
apk add --no-cache git build-base
|
2025-08-05 15:08:07 +08:00
|
|
|
|
|
|
|
|
# Install migrate tool
|
|
|
|
|
RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
|
|
|
|
|
|
2025-09-18 14:40:07 +08:00
|
|
|
# Copy go mod and sum files
|
2025-09-18 17:23:05 +08:00
|
|
|
COPY go.mod go.sum ./
|
|
|
|
|
RUN --mount=type=cache,target=/go/pkg/mod go mod download
|
2025-09-18 17:26:02 +08:00
|
|
|
COPY . .
|
2025-08-05 15:08:07 +08:00
|
|
|
|
2025-09-17 16:02:08 +08:00
|
|
|
# Get version and commit info for build injection
|
|
|
|
|
ARG VERSION_ARG
|
|
|
|
|
ARG COMMIT_ID_ARG
|
|
|
|
|
ARG BUILD_TIME_ARG
|
|
|
|
|
ARG GO_VERSION_ARG
|
|
|
|
|
|
|
|
|
|
# Set build-time variables
|
|
|
|
|
ENV VERSION=${VERSION_ARG}
|
|
|
|
|
ENV COMMIT_ID=${COMMIT_ID_ARG}
|
|
|
|
|
ENV BUILD_TIME=${BUILD_TIME_ARG}
|
|
|
|
|
ENV GO_VERSION=${GO_VERSION_ARG}
|
|
|
|
|
|
|
|
|
|
# Build the application with version info
|
2025-09-18 17:23:05 +08:00
|
|
|
RUN --mount=type=cache,target=/go/pkg/mod make build-prod
|
2025-09-18 19:53:18 +08:00
|
|
|
RUN --mount=type=cache,target=/go/pkg/mod cp -r /go/pkg/mod/github.com/yanyiwu/ /app/yanyiwu/
|
2025-08-05 15:08:07 +08:00
|
|
|
|
|
|
|
|
# Final stage
|
|
|
|
|
FROM alpine:3.17
|
|
|
|
|
|
|
|
|
|
WORKDIR /app
|
|
|
|
|
|
|
|
|
|
# Install runtime dependencies
|
2025-08-14 00:45:19 +08:00
|
|
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
|
|
|
|
|
apk update && apk upgrade && \
|
2025-09-11 23:08:13 +08:00
|
|
|
apk add --no-cache build-base postgresql-client mysql-client ca-certificates tzdata sed curl bash vim wget
|
2025-08-05 15:08:07 +08:00
|
|
|
|
2025-09-18 14:40:07 +08:00
|
|
|
# Create a non-root user and switch to it
|
|
|
|
|
RUN mkdir -p /data/files && \
|
|
|
|
|
adduser -D -g '' appuser && \
|
|
|
|
|
chown -R appuser:appuser /app /data/files
|
|
|
|
|
|
|
|
|
|
# Copy migrate tool from builder stage
|
|
|
|
|
COPY --from=builder /go/bin/migrate /usr/local/bin/
|
2025-09-18 17:23:05 +08:00
|
|
|
COPY --from=builder /app/yanyiwu/ /go/pkg/mod/github.com/yanyiwu/
|
2025-09-18 14:40:07 +08:00
|
|
|
|
2025-08-05 15:08:07 +08:00
|
|
|
# Copy the binary from the builder stage
|
|
|
|
|
COPY --from=builder /app/config ./config
|
|
|
|
|
COPY --from=builder /app/scripts ./scripts
|
|
|
|
|
COPY --from=builder /app/migrations ./migrations
|
|
|
|
|
COPY --from=builder /app/dataset/samples ./dataset/samples
|
2025-09-18 14:40:07 +08:00
|
|
|
COPY --from=builder /app/WeKnora .
|
2025-08-05 15:08:07 +08:00
|
|
|
|
|
|
|
|
# Make scripts executable
|
|
|
|
|
RUN chmod +x ./scripts/*.sh
|
|
|
|
|
|
|
|
|
|
# Expose ports
|
|
|
|
|
EXPOSE 8080
|
|
|
|
|
|
2025-09-11 23:08:13 +08:00
|
|
|
# Switch to non-root user and run the application directly
|
|
|
|
|
USER appuser
|
|
|
|
|
|
|
|
|
|
CMD ["./WeKnora"]
|