Skip to content

Commit 7973128

Browse files
committed
chore(build): Optimize Makefile and Dockerfile.app build configuration
1 parent 8ed050b commit 7973128

2 files changed

Lines changed: 18 additions & 23 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ build-prod:
181181
BUILD_TIME=$${BUILD_TIME:-unknown}; \
182182
GO_VERSION=$${GO_VERSION:-unknown}; \
183183
LDFLAGS="-X 'github.com/Tencent/WeKnora/internal/handler.Version=$$VERSION' -X 'github.com/Tencent/WeKnora/internal/handler.CommitID=$$COMMIT_ID' -X 'github.com/Tencent/WeKnora/internal/handler.BuildTime=$$BUILD_TIME' -X 'github.com/Tencent/WeKnora/internal/handler.GoVersion=$$GO_VERSION'"; \
184-
GOOS=linux go build -v -installsuffix cgo -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
184+
go build -ldflags="-w -s $$LDFLAGS" -o $(BINARY_NAME) $(MAIN_PATH)
185185

186186
clean-db:
187187
@echo "Cleaning database..."

docker/Dockerfile.app

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ FROM golang:1.24-alpine AS builder
33

44
WORKDIR /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
# 通过构建参数接收敏感信息
117
ARG GOPRIVATE_ARG
128
ARG GOPROXY_ARG
@@ -17,15 +13,17 @@ ENV GOPRIVATE=${GOPRIVATE_ARG}
1713
ENV GOPROXY=${GOPROXY_ARG}
1814
ENV 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
2621
RUN 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
2927
COPY . .
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 .
5864
COPY --from=builder /app/config ./config
5965
COPY --from=builder /app/scripts ./scripts
6066
COPY --from=builder /app/migrations ./migrations
6167
COPY --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
6871
RUN chmod +x ./scripts/*.sh
6972

7073
# Expose ports
7174
EXPOSE 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
8277
USER appuser
8378

0 commit comments

Comments
 (0)