Dockerfile.dev 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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: Frontend Build Stage (builder2)
  22. FROM node:18-alpine AS builder2
  23. # Set working directory
  24. WORKDIR /frontend
  25. RUN apk update && apk add git --no-cache
  26. # Clone the frontend repository
  27. RUN git clone https://github.com/lejianwen/rustdesk-api-web .
  28. # Install npm dependencies and build the frontend
  29. RUN npm install && npm run build
  30. # Stage 2: Final Image
  31. FROM alpine:latest
  32. # Set up working directory
  33. WORKDIR /app
  34. # Install necessary runtime dependencies
  35. RUN apk add --no-cache tzdata file
  36. # Copy the built application and resources from the builder stage
  37. COPY --from=builder /app/release /app/
  38. COPY --from=builder /app/conf /app/conf/
  39. COPY --from=builder /app/resources /app/resources/
  40. COPY --from=builder /app/docs /app/docs/
  41. # Copy frontend build from builder2 stage
  42. COPY --from=builder2 /frontend/dist/ /app/resources/admin/
  43. # Ensure the binary is correctly built and linked
  44. RUN file /app/apimain && \
  45. mkdir -p /app/data && \
  46. mkdir -p /app/runtime
  47. # Set up a volume for persistent data
  48. VOLUME /app/data
  49. # Expose the necessary port
  50. EXPOSE 21114
  51. # Define the command to run the application
  52. CMD ["./apimain"]