Merge pull request #42 from IamTaoChen/docker

Docker Optimize
This commit is contained in:
2024-11-01 13:10:59 +08:00
committed by GitHub
4 changed files with 70 additions and 11 deletions

View File

@@ -1,8 +1,11 @@
# Ignore Docker Compose configuration files # Ignore Docker Compose configuration files
docker-compose.yaml docker-compose.yaml
docker-compose-dev.yaml
# Ignore development Dockerfile # Ignore development Dockerfile
Dockerfile
Dockerfile.dev Dockerfile.dev
docker-dev.sh
# Ignore the data directory # Ignore the data directory
data/ data/

View File

@@ -12,17 +12,19 @@ WORKDIR /app
# Step 1: Copy the source code # Step 1: Copy the source code
COPY . . COPY . .
# use --mount=type=cache,target=/go/pkg/mod to cache the go mod
# Step 2: Download dependencies # 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: Run swag build script
# Step 3: Install swag and Run the build script RUN --mount=type=cache,target=/go/pkg/mod \
RUN go install github.com/swaggo/swag/cmd/swag@latest && \
swag init -g cmd/apimain.go --output docs/api --instanceName api --exclude http/controller/admin && \ 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 # Step 4: Build the Go application with CGO enabled and specified ldflags
RUN CGO_ENABLED=1 GOOS=linux go build -a \ RUN --mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=1 GOOS=linux go build -a \
-ldflags "-s -w --extldflags '-static -fpic'" \ -ldflags "-s -w --extldflags '-static -fpic'" \
-installsuffix cgo -o release/apimain cmd/apimain.go -installsuffix cgo -o release/apimain cmd/apimain.go
@@ -32,13 +34,24 @@ FROM node:18-alpine AS builder-admin-frontend
# Set working directory # Set working directory
WORKDIR /frontend 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 # Clone the frontend repository
RUN git clone https://github.com/lejianwen/rustdesk-api-web . RUN git clone https://github.com/lejianwen/rustdesk-api-web .
# Install npm dependencies and build the frontend # Install required tools without caching index to minimize image size
RUN npm install && npm run build 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 # Stage 2: Final Image
FROM alpine:latest FROM alpine:latest
@@ -47,7 +60,13 @@ FROM alpine:latest
WORKDIR /app WORKDIR /app
# Install necessary runtime dependencies # 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 the built application and resources from the builder stage
COPY --from=builder-backend /app/release /app/ COPY --from=builder-backend /app/release /app/

View File

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

35
docker-dev.sh Executable file
View File

@@ -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