Files
rustdesk-api/Dockerfile.dev
T

95 lines
3.2 KiB
Docker
Raw Permalink Normal View History

2024-10-29 11:50:55 +08:00
# Use build arguments for Go version and architecture
2024-10-29 14:27:15 +08:00
ARG GO_VERSION=1.22
ARG BUILDARCH=amd64
2024-10-29 11:50:55 +08:00
2024-10-29 14:27:15 +08:00
# Stage 1: Builder Stage
# FROM golang:${GO_VERSION}-alpine AS builder
2024-10-31 09:21:43 +08:00
FROM crazymax/xgo:${GO_VERSION} AS builder-backend
2024-10-29 11:50:55 +08:00
# Set up working directory
WORKDIR /app
2024-10-29 14:27:15 +08:00
# Step 1: Copy the source code
2024-10-29 11:50:55 +08:00
COPY . .
2024-11-01 01:32:45 +08:00
# use --mount=type=cache,target=/go/pkg/mod to cache the go mod
2024-10-29 14:27:15 +08:00
# Step 2: Download dependencies
2024-11-01 01:32:45 +08:00
RUN --mount=type=cache,target=/go/pkg/mod \
go mod tidy && go mod download && go install github.com/swaggo/swag/cmd/swag@latest
2024-10-29 11:50:55 +08:00
2024-11-01 01:32:45 +08:00
# Step 3: Run swag build script
RUN --mount=type=cache,target=/go/pkg/mod \
2024-10-29 14:27:15 +08:00
swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin && \
2024-11-01 01:32:45 +08:00
swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api
2024-10-29 14:27:15 +08:00
2024-11-01 01:32:45 +08:00
# Step 4: Build the Go application with CGO enabled and specified ldflags
RUN --mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=1 GOOS=linux go build -a \
2024-10-29 14:27:15 +08:00
-ldflags "-s -w --extldflags '-static -fpic'" \
-installsuffix cgo -o release/apimain cmd/apimain.go
2024-10-29 18:48:37 +08:00
# Stage 2: Frontend Build Stage (builder2)
2024-10-31 09:21:43 +08:00
FROM node:18-alpine AS builder-admin-frontend
2024-10-29 18:48:37 +08:00
# Set working directory
WORKDIR /frontend
ARG COUNTRY
# Install required tools without caching index to minimize image size
RUN if [ "$COUNTRY" = "CN" ] ; then \
echo "It is in China, updating the repositories"; \
sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories; \
fi && \
apk update && apk add --no-cache git
2024-10-29 18:48:37 +08:00
ARG FRONTEND_GIT_REPO=https://github.com/lejianwen/rustdesk-api-web.git
2024-11-03 16:34:50 +08:00
ARG FRONTEND_GIT_BRANCH=master
2024-10-29 18:48:37 +08:00
# Clone the frontend repository
2024-11-03 16:34:50 +08:00
RUN git clone -b $FRONTEND_GIT_BRANCH $FRONTEND_GIT_REPO .
2024-10-29 18:48:37 +08:00
2024-11-01 01:32:45 +08:00
# Install required tools without caching index to minimize image size
RUN if [ "$COUNTRY" = "CN" ] ; then \
echo "It is in China, updating NPM_CONFIG_REGISTRY"; \
export NPM_CONFIG_REGISTRY="https://mirrors.huaweicloud.com/repository/npm/"; \
fi && \
npm install && npm run build
2024-10-29 14:27:15 +08:00
# Stage 2: Final Image
2024-10-29 11:50:55 +08:00
FROM alpine:latest
# Set up working directory
WORKDIR /app
2024-10-29 14:27:15 +08:00
# Install necessary runtime dependencies
# Install required tools without caching index to minimize image size
ARG COUNTRY
RUN if [ "$COUNTRY" = "CN" ] ; then \
echo "It is in China, updating the repositories"; \
sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' /etc/apk/repositories; \
fi && \
apk update && apk add --no-cache tzdata file
2024-10-29 11:50:55 +08:00
2024-10-29 14:27:15 +08:00
# Copy the built application and resources from the builder stage
2024-10-31 09:21:43 +08:00
COPY --from=builder-backend /app/release /app/
COPY --from=builder-backend /app/conf /app/conf/
COPY --from=builder-backend /app/resources /app/resources/
COPY --from=builder-backend /app/docs /app/docs/
2024-10-29 18:48:37 +08:00
# Copy frontend build from builder2 stage
2024-10-31 09:21:43 +08:00
COPY --from=builder-admin-frontend /frontend/dist/ /app/resources/admin/
2024-10-29 11:50:55 +08:00
2024-10-29 14:27:15 +08:00
# Ensure the binary is correctly built and linked
RUN file /app/apimain && \
mkdir -p /app/data && \
mkdir -p /app/runtime
2024-10-29 11:50:55 +08:00
# Set up a volume for persistent data
VOLUME /app/data
# Expose the necessary port
EXPOSE 21114
# Define the command to run the application
CMD ["./apimain"]