|
|
@@ -1,36 +1,50 @@
|
|
1
|
1
|
# Use build arguments for Go version and architecture
|
|
2
|
|
-ARG GO_VERSION=1.23.2
|
|
3
|
|
-ARG BUILDARCH
|
|
|
2
|
+ARG GO_VERSION=1.22
|
|
|
3
|
+ARG BUILDARCH=amd64
|
|
4
|
4
|
|
|
5
|
|
-# Stage 1: Build the Go application with swag
|
|
6
|
|
-FROM golang:${GO_VERSION} AS builder
|
|
|
5
|
+# Stage 1: Builder Stage
|
|
|
6
|
+# FROM golang:${GO_VERSION}-alpine AS builder
|
|
|
7
|
+FROM crazymax/xgo:${GO_VERSION} AS builder
|
|
7
|
8
|
|
|
8
|
9
|
# Set up working directory
|
|
9
|
10
|
WORKDIR /app
|
|
10
|
11
|
|
|
11
|
|
-# Install dependencies and copy the source code
|
|
12
|
|
-COPY go.mod ./
|
|
13
|
|
-RUN go install github.com/swaggo/swag/cmd/swag@latest
|
|
14
|
|
-RUN go mod download
|
|
|
12
|
+# Step 1: Copy the source code
|
|
15
|
13
|
COPY . .
|
|
16
|
14
|
|
|
17
|
|
-#run the build script
|
|
18
|
|
-RUN chmod +x build.sh && ./build.sh
|
|
|
15
|
+# Step 2: Download dependencies
|
|
|
16
|
+RUN go mod tidy && go mod download
|
|
19
|
17
|
|
|
20
|
|
-# Stage 2: Prepare the final image
|
|
|
18
|
+
|
|
|
19
|
+# Step 3: Install swag and Run the build script
|
|
|
20
|
+RUN go install github.com/swaggo/swag/cmd/swag@latest && \
|
|
|
21
|
+ swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin && \
|
|
|
22
|
+ swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api
|
|
|
23
|
+
|
|
|
24
|
+# Build the Go application with CGO enabled and specified ldflags
|
|
|
25
|
+RUN CGO_ENABLED=1 GOOS=linux go build -a \
|
|
|
26
|
+ -ldflags "-s -w --extldflags '-static -fpic'" \
|
|
|
27
|
+ -installsuffix cgo -o release/apimain cmd/apimain.go
|
|
|
28
|
+
|
|
|
29
|
+
|
|
|
30
|
+# Stage 2: Final Image
|
|
21
|
31
|
FROM alpine:latest
|
|
22
|
32
|
|
|
23
|
33
|
# Set up working directory
|
|
24
|
34
|
WORKDIR /app
|
|
25
|
35
|
|
|
26
|
|
-# Install necessary dependencies
|
|
|
36
|
+# Install necessary runtime dependencies
|
|
27
|
37
|
RUN apk add --no-cache tzdata file
|
|
28
|
38
|
|
|
29
|
|
-# Copy the built application from the builder stage
|
|
|
39
|
+# Copy the built application and resources from the builder stage
|
|
30
|
40
|
COPY --from=builder /app/release /app/
|
|
|
41
|
+COPY --from=builder /app/conf /app/conf/
|
|
|
42
|
+COPY --from=builder /app/resources /app/resources/
|
|
31
|
43
|
|
|
32
|
|
-# Ensure the binary is correctly built
|
|
33
|
|
-RUN file /app/apimain
|
|
|
44
|
+# Ensure the binary is correctly built and linked
|
|
|
45
|
+RUN file /app/apimain && \
|
|
|
46
|
+ mkdir -p /app/data && \
|
|
|
47
|
+ mkdir -p /app/runtime
|
|
34
|
48
|
|
|
35
|
49
|
# Set up a volume for persistent data
|
|
36
|
50
|
VOLUME /app/data
|
|
|
@@ -39,4 +53,4 @@ VOLUME /app/data
|
|
39
|
53
|
EXPOSE 21114
|
|
40
|
54
|
|
|
41
|
55
|
# Define the command to run the application
|
|
42
|
|
-CMD ["app/apimain"]
|
|
|
56
|
+CMD ["./apimain"]
|