Dockerfile.dev 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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-backend
  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 builder-admin-frontend
  23. # Set working directory
  24. WORKDIR /frontend
  25. ARG COUNTRY
  26. # Install required tools without caching index to minimize image size
  27. RUN if [ "$COUNTRY" = "CN" ] ; then \
  28. echo "It is in China, updating the repositories"; \
  29. sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories; \
  30. fi && \
  31. apk update && apk add --no-cache git
  32. # Clone the frontend repository
  33. RUN git clone https://github.com/lejianwen/rustdesk-api-web .
  34. # Install npm dependencies and build the frontend
  35. RUN npm install && npm run build
  36. # Stage 2: Final Image
  37. FROM alpine:latest
  38. # Set up working directory
  39. WORKDIR /app
  40. # Install necessary runtime dependencies
  41. # Install required tools without caching index to minimize image size
  42. ARG COUNTRY
  43. RUN if [ "$COUNTRY" = "CN" ] ; then \
  44. echo "It is in China, updating the repositories"; \
  45. sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories; \
  46. fi && \
  47. apk update && apk add --no-cache tzdata file
  48. # Copy the built application and resources from the builder stage
  49. COPY --from=builder-backend /app/release /app/
  50. COPY --from=builder-backend /app/conf /app/conf/
  51. COPY --from=builder-backend /app/resources /app/resources/
  52. COPY --from=builder-backend /app/docs /app/docs/
  53. # Copy frontend build from builder2 stage
  54. COPY --from=builder-admin-frontend /frontend/dist/ /app/resources/admin/
  55. # Ensure the binary is correctly built and linked
  56. RUN file /app/apimain && \
  57. mkdir -p /app/data && \
  58. mkdir -p /app/runtime
  59. # Set up a volume for persistent data
  60. VOLUME /app/data
  61. # Expose the necessary port
  62. EXPOSE 21114
  63. # Define the command to run the application
  64. CMD ["./apimain"]