添加docker-compose.prod

This commit is contained in:
xie7654
2025-07-04 22:52:22 +08:00
parent 87d8d5ffb4
commit d705a88bd3
19 changed files with 536 additions and 70 deletions

View File

@@ -1,7 +1,55 @@
**/node_modules
.git
.gitignore
*.md
dist
**/.idea/
.idea/**
**/*.pyc
__pycache__/
build/
*.egg-info/
.python-version
.pytest_cache/
dist/
eggs/
lib/
lib64/
.DS_Store
docs/_build/
static/
.nitro
.output
coverage
**/.vitepress/cache
.cache
.turbo
dist.zip
.temp
.stylelintcache
yarn.lock
package-lock.json
.VSCodeCounter
**/backend-mock/data
# local env files
.eslintcache
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
vite.config.mts.*
vite.config.mjs.*
vite.config.js.*
vite.config.ts.*
# Editor directories and files
.idea
# .vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?
.history
**/node_modules

View File

@@ -1,46 +1,62 @@
# syntax=docker/dockerfile:1
####################
# 生产阶段
# 公共基础阶段
####################
FROM nginx:alpine AS prod
# 拷贝编译后的静态文件到 nginx
COPY --from=build /app/dist /usr/share/nginx/html
# 拷贝 nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf
####################
# 开发阶段
####################
FROM node:22.17.0 AS dev
FROM node:22.17.0 AS base
WORKDIR /app
ENV PNPM_SKIP_PROMPT=true
# 拷贝项目
COPY . .
COPY /apps/web-antd/.env.docker /apps/web-antd/.env.local
# 安装 pnpm(官方推荐 corepack更好
# 安装 pnpm
RUN corepack enable && corepack prepare pnpm@latest --activate
# 安装依赖(一定要在 monorepo 根目录,保证 workspace 有效)
# 清理依赖缓存
RUN pnpm store prune && rm -rf $(pnpm store path) && \
rm -rf node_modules .npmrc package-lock.json pnpm-lock.yaml .pnpm-store .turbo && \
find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +
RUN npm run clean
RUN yes | pnpm recursive install
# 安装依赖
RUN pnpm install --force
# 设置前端工作目录(根据实际情况修改)
# 暴露前端 dev server 端口
EXPOSE 5678
#CMD ["tail", "-f", "/dev/null"]
####################
# 开发阶段
####################
# 默认启动 dev server
FROM base AS dev
# 暴露端口(根据需要)
EXPOSE 5678
# CMD ["tail", "-f", "/dev/null"]
# 启动开发服务器
CMD ["npm", "run", "dev:antd"]
# --- 构建阶段 ---
FROM base AS build
# 构建生产版本
RUN npm run build:antd
# --- 生产阶段 ---
FROM nginx:1.25-alpine AS prod
# 删除默认配置
RUN rm -rf /usr/share/nginx/html/*
# 从构建阶段拷贝打包产物到 nginx html 目录
COPY --from=build /app/apps/web-antd/dist /usr/share/nginx/html
# 如果有需要,也可以拷贝自定义 nginx 配置
COPY nginx.conf /etc/nginx/nginx.conf
EXPOSE 5268
# 默认启动 nginx
CMD ["nginx", "-g", "daemon off;"]

View File

@@ -1 +0,0 @@
VITE_BACKEND_URL=http://backend:8000

View File

@@ -1,7 +1,7 @@
VITE_BASE=/
# 接口地址
VITE_GLOB_API_URL=http://127.0.0.1:8000/api
VITE_GLOB_API_URL=/api
# 是否开启压缩,可以设置为 none, brotli, gzip
VITE_COMPRESS=gzip

View File

@@ -24,7 +24,7 @@ const MOCK_USER_OPTIONS: BasicOption[] = [
},
{
label: 'User',
value: 'xj',
value: 'chenze',
},
];

View File

@@ -1,10 +1,13 @@
import { defineConfig } from '@vben/vite-config';
import { loadEnv } from 'vite';
import * as console from "node:console";
export default defineConfig(async ({ mode }) => {
// eslint-disable-next-line n/prefer-global/process
const env = loadEnv(mode, process.cwd(), '');
const env = loadEnv(mode, process.cwd());
// 这样获取
const backendUrl = env.VITE_BACKEND_URL;
console.log(backendUrl)
return {
application: {},
vite: {
@@ -13,7 +16,7 @@ export default defineConfig(async ({ mode }) => {
port: 5678,
proxy: {
'/api': {
target: env.VITE_BACKEND_URL,
target: backendUrl,
changeOrigin: true,
},
},

72
web/nginx.conf Normal file
View File

@@ -0,0 +1,72 @@
worker_processes 1;
events { worker_connections 1024; }
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name _;
# 前端静态资源
root /usr/share/nginx/html;
# 健康检查
location /healthz {
return 200 'ok';
add_header Content-Type text/plain;
}
# 代理 Django 静态文件
location /static/ {
alias /app/static/;
expires 30d;
add_header Cache-Control "public";
}
# 代理 Django 媒体文件
location /media/ {
alias /app/media/;
expires 30d;
add_header Cache-Control "public";
}
# 代理后端 API
location /api/ {
proxy_pass http://backend:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
client_max_body_size 100m;
client_body_buffer_size 128k;
proxy_connect_timeout 180;
proxy_send_timeout 180;
proxy_read_timeout 180;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
#proxy_cache_bypass $http_upgrade;
#proxy_cache_key $uri$request_body; #增加此行
#proxy_cache_methods GET POST; #增加此行
#add_header Access-Control-Allow-Headers X-API-Token;
access_log /var/log/nginx/assets_access.log;
error_log /var/log/nginx/assets_error.log;
}
# 前端 history 路由
location / {
try_files $uri $uri/ /index.html;
}
# gzip
gzip on;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css application/json;
gzip_min_length 1k;
gzip_comp_level 2;
}
}