diff --git a/.dockerignore b/.dockerignore index 5bd7cfb..e3d1226 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,8 +1,11 @@ # Ignore Docker Compose configuration files docker-compose.yaml +docker-compose-dev.yaml # Ignore development Dockerfile +Dockerfile Dockerfile.dev +docker-dev.sh # Ignore the data directory data/ diff --git a/Dockerfile.dev b/Dockerfile.dev index 43aa840..7269a73 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -12,17 +12,19 @@ WORKDIR /app # Step 1: Copy the source code COPY . . +# use --mount=type=cache,target=/go/pkg/mod to cache the go mod # Step 2: Download dependencies -RUN go mod tidy && go mod download +RUN --mount=type=cache,target=/go/pkg/mod \ + go mod tidy && go mod download && go install github.com/swaggo/swag/cmd/swag@latest - -# Step 3: Install swag and Run the build script -RUN go install github.com/swaggo/swag/cmd/swag@latest && \ +# Step 3: Run swag build script +RUN --mount=type=cache,target=/go/pkg/mod \ swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin && \ - swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api + swag init -g cmd/apimain.go --output docs/admin --instanceName admin --exclude http/controller/api -# Build the Go application with CGO enabled and specified ldflags -RUN CGO_ENABLED=1 GOOS=linux go build -a \ +# 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 \ -ldflags "-s -w --extldflags '-static -fpic'" \ -installsuffix cgo -o release/apimain cmd/apimain.go @@ -32,13 +34,24 @@ FROM node:18-alpine AS builder-admin-frontend # Set working directory WORKDIR /frontend -RUN apk update && apk add git --no-cache +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 # Clone the frontend repository RUN git clone https://github.com/lejianwen/rustdesk-api-web . -# Install npm dependencies and build the frontend -RUN npm install && npm run build +# 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 + # Stage 2: Final Image FROM alpine:latest @@ -47,7 +60,13 @@ FROM alpine:latest WORKDIR /app # Install necessary runtime dependencies -RUN apk add --no-cache tzdata file +# 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 # Copy the built application and resources from the builder stage COPY --from=builder-backend /app/release /app/ diff --git a/docker-compose-dev.yaml b/docker-compose-dev.yaml index d015bc1..9e30042 100644 --- a/docker-compose-dev.yaml +++ b/docker-compose-dev.yaml @@ -3,6 +3,8 @@ services: build: context: . dockerfile: Dockerfile.dev + args: + COUNTRY: CN # image: lejianwen/rustdesk-api container_name: rustdesk-api environment: diff --git a/docker-dev.sh b/docker-dev.sh new file mode 100755 index 0000000..eefd665 --- /dev/null +++ b/docker-dev.sh @@ -0,0 +1,35 @@ +#!/bin/bash +set -e + +# Define Docker Compose file and cache option +COMPOSE_FILE_NAME="docker-compose-dev.yaml" +CACHE="" +# Uncomment the next line to enable no-cache option +# CACHE="--no-cache" + +# Define the base Docker Compose command +DCS="docker compose -f ${COMPOSE_FILE_NAME}" + +# Function to build and start services +build_and_run() { + echo "Building services..." + if ! $DCS build ${CACHE}; then + echo "Error: Failed to build services" + exit 1 + fi + + echo "Starting services..." + if ! $DCS up -d; then + echo "Error: Failed to start services" + exit 1 + fi + echo "Services started successfully" + echo "If you want to stop the services, run" + echo "docker compose -f ${COMPOSE_FILE_NAME} down" + + echo "If you want to see the logs, run" + echo "docker compose -f ${COMPOSE_FILE_NAME} logs -f" +} + +# Execute build and start function +build_and_run \ No newline at end of file