@@ -3,10 +3,6 @@ FROM golang:1.24-alpine AS builder
33
44WORKDIR /app
55
6- # Install dependencies
7- RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
8- apk add --no-cache git build-base
9-
106# 通过构建参数接收敏感信息
117ARG GOPRIVATE_ARG
128ARG GOPROXY_ARG
@@ -17,15 +13,17 @@ ENV GOPRIVATE=${GOPRIVATE_ARG}
1713ENV GOPROXY=${GOPROXY_ARG}
1814ENV GOSUMDB=${GOSUMDB_ARG}
1915
20- # Copy go mod and sum files
21- COPY go.mod go.sum ./
22- RUN go mod download
16+ # Install dependencies
17+ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories && \
18+ apk add --no-cache git build-base
2319
24- ENV CGO_ENABLED=1
2520# Install migrate tool
2621RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
2722
28- # Copy source code
23+ # Copy go mod and sum files
24+ COPY go.mod .
25+ COPY go.sum .
26+ RUN go mod download
2927COPY . .
3028
3129# Get version and commit info for build injection
@@ -53,31 +51,28 @@ RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/re
5351 apk update && apk upgrade && \
5452 apk add --no-cache build-base postgresql-client mysql-client ca-certificates tzdata sed curl bash vim wget
5553
54+ # Create a non-root user and switch to it
55+ RUN mkdir -p /data/files && \
56+ adduser -D -g '' appuser && \
57+ chown -R appuser:appuser /app /data/files
58+
59+ # Copy migrate tool from builder stage
60+ COPY --from=builder /go/bin/migrate /usr/local/bin/
61+ COPY --from=builder /go/pkg/mod/github.com/yanyiwu /go/pkg/mod/github.com/yanyiwu/
62+
5663# Copy the binary from the builder stage
57- COPY --from=builder /app/WeKnora .
5864COPY --from=builder /app/config ./config
5965COPY --from=builder /app/scripts ./scripts
6066COPY --from=builder /app/migrations ./migrations
6167COPY --from=builder /app/dataset/samples ./dataset/samples
62-
63- # Copy migrate tool from builder stage
64- COPY --from=builder /go/bin/migrate /usr/local/bin/
65- COPY --from=builder /go/pkg/mod/github.com/yanyiwu /go/pkg/mod/github.com/yanyiwu/
68+ COPY --from=builder /app/WeKnora .
6669
6770# Make scripts executable
6871RUN chmod +x ./scripts/*.sh
6972
7073# Expose ports
7174EXPOSE 8080
7275
73- # Set environment variables
74- ENV CGO_ENABLED=1
75-
76- # Create a non-root user and switch to it
77- RUN mkdir -p /data/files && \
78- adduser -D -g '' appuser && \
79- chown -R appuser:appuser /app /data/files
80-
8176# Switch to non-root user and run the application directly
8277USER appuser
8378
0 commit comments