增加docker配置
Change-Id: I9fa70e1256227f84973fc1bf5d5175e57f5cd225
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000..d604a03
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,6 @@
+# .dockerignore
+node_modules
+npm-debug.log
+yarn-error.log
+dist
+build
\ No newline at end of file
diff --git a/.env b/.env
new file mode 100644
index 0000000..cb6f082
--- /dev/null
+++ b/.env
@@ -0,0 +1,2 @@
+PORT=3004 # 设置前端端口
+API_PROXY_TARGET=http://localhost:5004 # 设置代理后端地址
\ No newline at end of file
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..385cb2f
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,14 @@
+# 构建阶段
+FROM node:16 as build
+WORKDIR /app
+COPY package*.json ./
+RUN npm install
+COPY . .
+RUN npm run build
+
+# 生产阶段
+FROM nginx:alpine
+COPY --from=build /app/dist /usr/share/nginx/html
+COPY nginx.conf /etc/nginx/conf.d/default.conf
+EXPOSE 3004
+CMD ["nginx", "-g", "daemon off;"]
\ No newline at end of file
diff --git a/config/config.ts b/config/config.ts
index f0004a8..511894a 100644
--- a/config/config.ts
+++ b/config/config.ts
@@ -8,6 +8,7 @@
const { REACT_APP_ENV = 'dev' } = process.env;
export default defineConfig({
+
/**
* @name 开启 hash 模式
* @description 让 build 之后的产物包含 hash 后缀。通常用于增量发布和避免浏览器加载缓存。
diff --git a/config/oneapi.json b/config/oneapi.json
index c77d988..064e6b7 100644
--- a/config/oneapi.json
+++ b/config/oneapi.json
@@ -4,12 +4,13 @@
"title": "Ant Design Pro",
"version": "1.0.0"
},
+
"servers": [
{
- "url": "http://localhost:8000/"
+ "url": "http://localhost:3004/"
},
{
- "url": "https://localhost:8000/"
+ "url": "https://localhost:3004/"
}
],
"paths": {
diff --git a/config/proxy.ts b/config/proxy.ts
index c290c6a..a812e74 100644
--- a/config/proxy.ts
+++ b/config/proxy.ts
@@ -12,17 +12,17 @@
export default {
// 如果需要自定义本地开发服务器 请取消注释按需调整
dev: {
- // localhost:8000/api/** -> https://preview.pro.ant.design/api/**
+ // localhost:5004/api/** -> https://preview.pro.ant.design/api/**
'/api/': {
// 要代理的地址
- target: 'http://localhost:8080',
+ target: 'http://localhost:5004',
// 配置了这个可以从 http 代理到 https
// 依赖 origin 的功能可能需要这个,比如 cookie
changeOrigin: true,
pathRewrite: { '^/api': '' },
},
'/profile/avatar/': {
- target: 'http://localhost:8080',
+ target: 'http://localhost:5004',
changeOrigin: true,
}
},
@@ -32,7 +32,7 @@
* @doc https://github.com/chimurai/http-proxy-middleware
*/
test: {
- // localhost:8000/api/** -> https://preview.pro.ant.design/api/**
+ // localhost:5004/api/** -> https://preview.pro.ant.design/api/**
'/api/': {
target: 'https://proapi.azurewebsites.net',
changeOrigin: true,
diff --git a/nginx.conf b/nginx.conf
new file mode 100644
index 0000000..a2b5e67
--- /dev/null
+++ b/nginx.conf
@@ -0,0 +1,24 @@
+upstream backend {
+ server backend:5004; # 保持不变(Docker Compose中服务名即容器间通信域名)
+}
+
+server {
+ listen 3004; # 修改为Docker Compose中映射的前端端口(与docker-compose.yml保持一致)
+ server_name _; # 允许所有域名访问
+
+ # 静态资源处理
+ location / {
+ root /usr/share/nginx/html;
+ index index.html;
+ try_files $uri $uri/ /index.html; # 适配SPA路由(如React/Vue)
+ }
+
+ # 反向代理API请求到后端
+ location /api/ {
+ proxy_pass http://backend/; # 代理路径与后端API前缀匹配
+ 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;
+ }
+}
\ No newline at end of file
diff --git a/package.json b/package.json
index 044644a..4625852 100644
--- a/package.json
+++ b/package.json
@@ -7,9 +7,9 @@
"dev": "max dev",
"build": "max build",
"deploy": "npm run build && npm run gh-pages",
- "preview": "npm run build && max preview --port 8000",
+ "preview": "npm run build && max preview --port 5004",
"serve": "umi-serve",
- "start": "npm run dev",
+ "start": "npm run dev -- --port 5004",
"start:dev": "cross-env REACT_APP_ENV=dev MOCK=none UMI_ENV=dev max dev",
"start:no-mock": "cross-env MOCK=none UMI_ENV=dev max dev",
"start:pre": "cross-env REACT_APP_ENV=pre UMI_ENV=dev max dev",