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 <noreply@anthropic.com>
This commit is contained in:
Michael Dong
2026-02-05 18:35:45 +08:00
parent 86d3a8c419
commit faf9f3abcf
4 changed files with 58 additions and 7 deletions

View File

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

37
docker-compose.deploy.yml Normal file
View File

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

View File

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

View File

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