From 4baa8d392e9b15cc925ff5e838f743d664633a7d Mon Sep 17 00:00:00 2001 From: Tao Chen Date: Tue, 29 Oct 2024 11:50:55 +0800 Subject: [PATCH] optimize docker --- .dockerignore | 4 ++++ Dockerfile.dev | 42 ++++++++++++++++++++++++++++++++++++++++++ docker-compose.yaml | 8 ++++++-- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile.dev diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..d3c29bd --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +docker-compose.yaml +Dcokerfile +Dcokerfile.dev +data \ No newline at end of file diff --git a/Dockerfile.dev b/Dockerfile.dev new file mode 100644 index 0000000..667aa55 --- /dev/null +++ b/Dockerfile.dev @@ -0,0 +1,42 @@ +# Use build arguments for Go version and architecture +ARG GO_VERSION=1.23.2 +ARG BUILDARCH + +# Stage 1: Build the Go application with swag +FROM golang:${GO_VERSION} AS builder + +# Set up working directory +WORKDIR /app + +# Install dependencies and copy the source code +COPY go.mod ./ +RUN go install github.com/swaggo/swag/cmd/swag@latest +RUN go mod download +COPY . . + +#run the build script +RUN chmod +x build.sh && ./build.sh + +# Stage 2: Prepare the final image +FROM alpine:latest + +# Set up working directory +WORKDIR /app + +# Install necessary dependencies +RUN apk add --no-cache tzdata file + +# Copy the built application from the builder stage +COPY --from=builder /app/release /app/ + +# Ensure the binary is correctly built +RUN file /app/apimain + +# Set up a volume for persistent data +VOLUME /app/data + +# Expose the necessary port +EXPOSE 21114 + +# Define the command to run the application +CMD ["app/apimain"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml index bd95588..9328fcd 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -1,5 +1,8 @@ services: rustdesk-api: + build: + context: . + dockerfile: Dockerfile.dev image: lejianwen/rustdesk-api container_name: rustdesk-api environment: @@ -11,5 +14,6 @@ services: ports: - 21114:21114 volumes: - - /data/rustdesk/api:/app/data #将数据库挂载出来方便备份 - restart: unless-stopped \ No newline at end of file + - ./data/rustdesk/api:/app/data #将数据库挂载出来方便备份 + restart: unless-stopped + command: sleep infnite \ No newline at end of file