feat: Docker镜像构建

This commit is contained in:
CaasianVale
2025-03-07 04:08:11 +08:00
parent 4c115cf325
commit a42884b453
5 changed files with 262 additions and 10 deletions

39
frontend/Dockerfile Normal file
View File

@@ -0,0 +1,39 @@
# 构建阶段
FROM node:18-alpine as build-stage
# 设置工作目录
WORKDIR /app
# 安装 yarn
RUN apk add --no-cache yarn
# 复制 package.json 和 yarn.lock如果有
COPY package*.json yarn*.lock ./
# 安装依赖
RUN yarn install --frozen-lockfile
# 复制项目文件
COPY . .
# 构建应用
RUN yarn build
# 生产阶段
FROM nginx:stable-alpine as production-stage
# 复制自定义nginx配置如需要
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 从构建阶段复制构建结果到nginx服务目录
COPY --from=build-stage /app/dist /usr/share/nginx/html
# 暴露80端口
EXPOSE 80
# 健康检查
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD wget --quiet --tries=1 --spider http://localhost:80 || exit 1
# 启动nginx
CMD ["nginx", "-g", "daemon off;"]

73
frontend/nginx.conf Normal file
View File

@@ -0,0 +1,73 @@
server {
listen 80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
root /usr/share/nginx/html;
index index.html;
# 缓存静态资源
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, max-age=31536000, immutable";
}
# API请求代理到后端服务
location /api/ {
proxy_pass http://backend:8888/api/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_read_timeout 300s;
}
location /login {
proxy_pass http://backend:8888/login;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /check_auth {
proxy_pass http://backend:8888/check_auth;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /need_login {
proxy_pass http://backend:8888/need_login;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /config {
proxy_pass http://backend:8888/config;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /analyze {
proxy_pass http://backend:8888/analyze;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_read_timeout 300s;
}
# 所有其他路由返回index.htmlSPA应用需要
location / {
try_files $uri $uri/ /index.html;
}
# 错误页面
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}