From 503e908eb6ae9e13a10b9806cf6b4ce94d8b2c97 Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Fri, 1 Nov 2024 00:28:43 +0800 Subject: [PATCH 1/5] add bash to run dev-docker --- docker-dev.sh | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100755 docker-dev.sh diff --git a/docker-dev.sh b/docker-dev.sh new file mode 100755 index 0000000..44ac107 --- /dev/null +++ b/docker-dev.sh @@ -0,0 +1,8 @@ +#!/bin/bash + +FILE_NAME="docker-compose-dev.yaml" + +# CACHE="--no-cache" + +docker compose -f ${FILE_NAME} build ${CACHE} +docker compose -f ${FILE_NAME} up -d \ No newline at end of file From d21b5809c4b28b7ce139eac325398fbc8fe86abb Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Fri, 1 Nov 2024 00:29:33 +0800 Subject: [PATCH 2/5] add ARG CONTRY=CN to improve the alpinelinux install speed --- Dockerfile.dev | 16 ++++++++++++++-- docker-compose-dev.yaml | 2 ++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/Dockerfile.dev b/Dockerfile.dev index 43aa840..741f98e 100644 --- a/Dockerfile.dev +++ b/Dockerfile.dev @@ -32,7 +32,13 @@ 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 . @@ -47,7 +53,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: From a6a6c3aefe3e20fd964f2340816cf968e3c1accc Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Fri, 1 Nov 2024 01:32:45 +0800 Subject: [PATCH 3/5] optimize build speed, like cache and mirror --- Dockerfile.dev | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/Dockerfile.dev b/Dockerfile.dev index 741f98e..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 @@ -43,8 +45,13 @@ RUN if [ "$COUNTRY" = "CN" ] ; then \ # 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 From 2627f6fb0647b35dc58aeacc1c36245b5a798410 Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Fri, 1 Nov 2024 01:33:05 +0800 Subject: [PATCH 4/5] optimize scripts --- docker-dev.sh | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/docker-dev.sh b/docker-dev.sh index 44ac107..eefd665 100755 --- a/docker-dev.sh +++ b/docker-dev.sh @@ -1,8 +1,35 @@ #!/bin/bash +set -e -FILE_NAME="docker-compose-dev.yaml" - +# 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" -docker compose -f ${FILE_NAME} build ${CACHE} -docker compose -f ${FILE_NAME} up -d \ No newline at end of file +# 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 From 51186b66dcb86ad3dbad7e7a2ac40391b05f55a5 Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Fri, 1 Nov 2024 01:33:08 +0800 Subject: [PATCH 5/5] add more --- .dockerignore | 3 +++ 1 file changed, 3 insertions(+) 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/