fix:优化docker构建速度

This commit is contained in:
2025-11-13 15:27:23 +08:00
parent b6de82387c
commit 1f21fbfe16
3 changed files with 50 additions and 3 deletions

37
.dockerignore Normal file
View File

@@ -0,0 +1,37 @@
# Speed up docker build by shrinking context and avoiding host artifacts
.git
.gitignore
node_modules
pnpm-debug.log
npm-debug.log
yarn-debug.log
yarn-error.log
.DS_Store
**/.DS_Store
# Monorepo: ignore nested installs and build outputs
apps/**/node_modules
apps/**/dist
apps/**/.next
apps/**/.nuxt
apps/**/build
apps/**/.cache
# Editor/OS noise
.vscode
.idea
*.swp
*.swo
Thumbs.db
# Docker
Dockerfile*
!Dockerfile
docker-compose*.yml
!.dockerignore
# Tests and coverage
coverage
**/coverage
*.lcov

View File

@@ -36,7 +36,6 @@ RUN pnpm --filter backend build
FROM base AS runtime
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/*
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
COPY apps/backend/package.json ./apps/backend/
# Copy node_modules from deps stage and prune to production to avoid network

View File

@@ -10,14 +10,23 @@ services:
MONGO_INITDB_DATABASE: ai-bill
volumes:
- mongo-data:/data/db
healthcheck:
test: ["CMD-SHELL", "mongosh --quiet --eval 'db.runCommand({ ping: 1 }).ok' | grep 1"]
interval: 10s
timeout: 5s
retries: 10
backend:
build:
context: .
dockerfile: Dockerfile
args:
# 可根据网络情况切换为国内镜像源
NPM_REGISTRY: ${NPM_REGISTRY:-https://registry.npmjs.org/}
container_name: ai-bill-backend
depends_on:
- mongo
mongo:
condition: service_healthy
environment:
NODE_ENV: production
HOST: 0.0.0.0
@@ -30,8 +39,10 @@ services:
ports:
- "4010:4000"
restart: unless-stopped
stop_signal: SIGTERM
stop_grace_period: 20s
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:4000/api/health"]
test: ["CMD", "node", "-e", "fetch('http://localhost:4000/api/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"]
interval: 30s
timeout: 5s
retries: 5