Dockerfile.dev 1.5 KB

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