Просмотр исходного кода

Merge pull request #42 from IamTaoChen/docker

Docker Optimize
1 год назад
Родитель
Сommit
b9c6f17e3f
4 измененных файлов с 70 добавлено и 11 удалено
  1. 3 0
      .dockerignore
  2. 30 11
      Dockerfile.dev
  3. 2 0
      docker-compose-dev.yaml
  4. 35 0
      docker-dev.sh

+ 3 - 0
.dockerignore

@@ -1,8 +1,11 @@
1
 # Ignore Docker Compose configuration files
1
 # Ignore Docker Compose configuration files
2
 docker-compose.yaml
2
 docker-compose.yaml
3
+docker-compose-dev.yaml
3
 
4
 
4
 # Ignore development Dockerfile
5
 # Ignore development Dockerfile
6
+Dockerfile
5
 Dockerfile.dev
7
 Dockerfile.dev
8
+docker-dev.sh
6
 
9
 
7
 # Ignore the data directory
10
 # Ignore the data directory
8
 data/
11
 data/

+ 30 - 11
Dockerfile.dev

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

+ 2 - 0
docker-compose-dev.yaml

@@ -3,6 +3,8 @@ services:
3
     build: 
3
     build: 
4
       context: .
4
       context: .
5
       dockerfile: Dockerfile.dev
5
       dockerfile: Dockerfile.dev
6
+      args:
7
+        COUNTRY: CN
6
     # image: lejianwen/rustdesk-api
8
     # image: lejianwen/rustdesk-api
7
     container_name: rustdesk-api
9
     container_name: rustdesk-api
8
     environment:
10
     environment:

+ 35 - 0
docker-dev.sh

@@ -0,0 +1,35 @@
1
+#!/bin/bash
2
+set -e
3
+
4
+# Define Docker Compose file and cache option
5
+COMPOSE_FILE_NAME="docker-compose-dev.yaml"
6
+CACHE=""
7
+# Uncomment the next line to enable no-cache option
8
+# CACHE="--no-cache"
9
+
10
+# Define the base Docker Compose command
11
+DCS="docker compose -f ${COMPOSE_FILE_NAME}"
12
+
13
+# Function to build and start services
14
+build_and_run() {
15
+    echo "Building services..."
16
+    if ! $DCS build ${CACHE}; then
17
+        echo "Error: Failed to build services"
18
+        exit 1
19
+    fi
20
+
21
+    echo "Starting services..."
22
+    if ! $DCS up -d; then
23
+        echo "Error: Failed to start services"
24
+        exit 1
25
+    fi
26
+    echo "Services started successfully"
27
+    echo "If you want to stop the services, run"
28
+    echo "docker compose -f ${COMPOSE_FILE_NAME} down"
29
+
30
+    echo "If you want to see the logs, run"
31
+    echo "docker compose -f ${COMPOSE_FILE_NAME} logs -f"
32
+}
33
+
34
+# Execute build and start function
35
+build_and_run