Dockerfile.dev 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. # use --mount=type=cache,target=/go/pkg/mod to cache the go mod
  12. # Step 2: Download dependencies
  13. RUN --mount=type=cache,target=/go/pkg/mod \
  14. go mod tidy && go mod download && go install github.com/swaggo/swag/cmd/swag@latest
  15. # Step 3: Run swag build script
  16. RUN --mount=type=cache,target=/go/pkg/mod \
  17. swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin && \
  18. swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api
  19. # Step 4: Build the Go application with CGO enabled and specified ldflags
  20. RUN --mount=type=cache,target=/go/pkg/mod \
  21. CGO_ENABLED=1 GOOS=linux go build -a \
  22. -ldflags "-s -w --extldflags '-static -fpic'" \
  23. -installsuffix cgo -o release/apimain cmd/apimain.go
  24. # Stage 2: Frontend Build Stage (builder2)
  25. FROM node:18-alpine AS builder-admin-frontend
  26. # Set working directory
  27. WORKDIR /frontend
  28. ARG COUNTRY
  29. # Install required tools without caching index to minimize image size
  30. RUN if [ "$COUNTRY" = "CN" ] ; then \
  31. echo "It is in China, updating the repositories"; \
  32. sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories; \
  33. fi && \
  34. apk update && apk add --no-cache git
  35. ARG FREONTEND_GIT_REPO=https://github.com/lejianwen/rustdesk-api-web.git
  36. ARG FRONTEND_GIT_BRANCH=master
  37. # Clone the frontend repository
  38. RUN git clone -b $FRONTEND_GIT_BRANCH $FREONTEND_GIT_REPO .
  39. # Install required tools without caching index to minimize image size
  40. RUN if [ "$COUNTRY" = "CN" ] ; then \
  41. echo "It is in China, updating NPM_CONFIG_REGISTRY"; \
  42. export NPM_CONFIG_REGISTRY="https://mirrors.huaweicloud.com/repository/npm/"; \
  43. fi && \
  44. npm install && npm run build
  45. # Stage 2: Final Image
  46. FROM alpine:latest
  47. # Set up working directory
  48. WORKDIR /app
  49. # Install necessary runtime dependencies
  50. # Install required tools without caching index to minimize image size
  51. ARG COUNTRY
  52. RUN if [ "$COUNTRY" = "CN" ] ; then \
  53. echo "It is in China, updating the repositories"; \
  54. sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories; \
  55. fi && \
  56. apk update && apk add --no-cache tzdata file
  57. # Copy the built application and resources from the builder stage
  58. COPY --from=builder-backend /app/release /app/
  59. COPY --from=builder-backend /app/conf /app/conf/
  60. COPY --from=builder-backend /app/resources /app/resources/
  61. COPY --from=builder-backend /app/docs /app/docs/
  62. COPY --from=builder-backend /app/http/templates /app/http/templates
  63. # Copy frontend build from builder2 stage
  64. COPY --from=builder-admin-frontend /frontend/dist/ /app/resources/admin/
  65. # Ensure the binary is correctly built and linked
  66. RUN file /app/apimain && \
  67. mkdir -p /app/data && \
  68. mkdir -p /app/runtime
  69. # Set up a volume for persistent data
  70. VOLUME /app/data
  71. # Expose the necessary port
  72. EXPOSE 21114
  73. # Define the command to run the application
  74. CMD ["./apimain"]