From faf9f3abcff384c3244ce63a1151ccea9005d361 Mon Sep 17 00:00:00 2001 From: Michael Dong Date: Thu, 5 Feb 2026 18:35:45 +0800 Subject: [PATCH] Fix Docker configuration for deployment - Update backend Dockerfile to use Rust 1.85 (edition 2024 support) - Add Aliyun mirrors for Alpine packages - Add rsproxy.cn mirror for cargo crates - Enable static OpenSSL linking - Fix frontend Dockerfile to use NEXT_PUBLIC_API_BASE - Add mkdir -p public to ensure public dir exists - Update docker-compose files to use correct env var names - Add docker-compose.deploy.yml for production deployment Co-Authored-By: Claude Opus 4.5 --- backend_rust/Dockerfile | 17 ++++++++++++++--- docker-compose.deploy.yml | 37 +++++++++++++++++++++++++++++++++++++ docker-compose.prod.yml | 4 ++-- frontend/Dockerfile | 7 +++++-- 4 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 docker-compose.deploy.yml diff --git a/backend_rust/Dockerfile b/backend_rust/Dockerfile index c4b1d77..2486423 100644 --- a/backend_rust/Dockerfile +++ b/backend_rust/Dockerfile @@ -1,7 +1,16 @@ # Build stage -FROM rust:1.84-alpine AS builder +FROM rust:1.85-alpine AS builder -RUN apk add --no-cache musl-dev pkgconfig openssl-dev +# Use Aliyun mirrors for faster package downloads in China +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories + +RUN apk add --no-cache musl-dev pkgconfig openssl-dev openssl-libs-static + +# Configure cargo to use rsproxy.cn mirror for crates +RUN mkdir -p /usr/local/cargo && printf '[source.crates-io]\nreplace-with = "rsproxy-sparse"\n\n[source.rsproxy-sparse]\nregistry = "sparse+https://rsproxy.cn/index/"\n\n[net]\ngit-fetch-with-cli = true\n' > /usr/local/cargo/config.toml + +# Static OpenSSL linking +ENV OPENSSL_STATIC=1 WORKDIR /app @@ -30,13 +39,15 @@ RUN cargo build --release # Runtime stage FROM alpine:3.21 +# Use Aliyun mirrors +RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories + RUN apk add --no-cache ca-certificates libgcc WORKDIR /app # Copy the binary from builder COPY --from=builder /app/target/release/backend_rust /app/backend_rust -COPY --from=builder /app/target/release/migration /app/migration # Create uploads directory RUN mkdir -p /app/uploads/avatars diff --git a/docker-compose.deploy.yml b/docker-compose.deploy.yml new file mode 100644 index 0000000..8c855e1 --- /dev/null +++ b/docker-compose.deploy.yml @@ -0,0 +1,37 @@ +version: "3.8" + +services: + backend: + build: + context: ./backend_rust + dockerfile: Dockerfile + container_name: notify-backend + restart: unless-stopped + extra_hosts: + - "host.docker.internal:host-gateway" + environment: + DATABASE_URL: postgresql://${POSTGRES_USER:-postgres}:${POSTGRES_PASSWORD}@host.docker.internal:5432/${POSTGRES_DB:-notify} + JWT_SECRET: ${JWT_SECRET:?JWT_SECRET is required} + BASE_URL: ${BASE_URL:-http://localhost} + ports: + - "127.0.0.1:4000:4000" + volumes: + - uploads_data:/app/uploads + + frontend: + build: + context: ./frontend + dockerfile: Dockerfile + args: + NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-http://localhost} + container_name: notify-frontend + restart: unless-stopped + depends_on: + - backend + environment: + NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-http://localhost} + ports: + - "127.0.0.1:3001:3000" + +volumes: + uploads_data: diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 35cda0d..9464ab6 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -44,14 +44,14 @@ services: dockerfile: Dockerfile args: # 构建时注入 API URL(用于 Next.js 静态优化) - NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-} + NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-} container_name: notify-frontend restart: unless-stopped depends_on: - backend environment: # 运行时 API URL(用于 SSR) - NEXT_PUBLIC_API_URL: ${NEXT_PUBLIC_API_URL:-} + NEXT_PUBLIC_API_BASE: ${NEXT_PUBLIC_API_BASE:-} ports: - "127.0.0.1:3000:3000" diff --git a/frontend/Dockerfile b/frontend/Dockerfile index 1edcf5b..39709a6 100644 --- a/frontend/Dockerfile +++ b/frontend/Dockerfile @@ -4,8 +4,8 @@ FROM node:20-alpine AS builder WORKDIR /app # 构建参数(NEXT_PUBLIC_* 变量需要在构建时注入) -ARG NEXT_PUBLIC_API_URL -ENV NEXT_PUBLIC_API_URL=${NEXT_PUBLIC_API_URL} +ARG NEXT_PUBLIC_API_BASE +ENV NEXT_PUBLIC_API_BASE=${NEXT_PUBLIC_API_BASE} # Copy package files COPY package*.json ./ @@ -16,6 +16,9 @@ RUN npm ci # Copy source code COPY . . +# Ensure public directory exists +RUN mkdir -p public + # Build Next.js app RUN npm run build