From 469c5a01705b6baf3efa122fd4a0fe60660b89ab Mon Sep 17 00:00:00 2001 From: coolxitech Date: Sat, 13 Dec 2025 10:19:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(docker):=20=E4=BC=98=E5=8C=96Docker?= =?UTF-8?q?=E6=9E=84=E5=BB=BA=E6=B5=81=E7=A8=8B=E5=B9=B6=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E7=94=9F=E4=BA=A7=E7=8E=AF=E5=A2=83=E9=83=A8=E7=BD=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 引入多阶段构建,分离构建与运行时环境 - 使用node:20-alpine作为构建基础镜像,减小体积 - 添加pnpm包管理器并优化依赖安装流程 - 利用缓存机制提升构建效率 - 新增nginx.conf配置文件解决Vue Router历史模式404问题 - 设置静态资源缓存策略提升性能 - 更改默认启动命令为Nginx服务方式运行应用 - 移除开发服务器相关指令,适配生产部署需求 --- Dockerfile | 36 ++++++++++++++++++++++++++---------- nginx.conf | 17 +++++++++++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 nginx.conf diff --git a/Dockerfile b/Dockerfile index 6ec9e2a..51fc2bf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,33 @@ -FROM node:latest - -RUN mkdir -p /workspace - -WORKDIR /workspace +# ========= 阶段1:构建 ========= +FROM node:20-alpine AS builder +# 使用国内镜像加速(可选) RUN npm config set registry https://registry.npmmirror.com -RUN cd /workspace +WORKDIR /app -RUN git clone https://github.com/setube/ogame-vue-ts.git +# 先复制依赖文件,利用缓存 +COPY package.json pnpm-lock.yaml* ./ -RUN mv ./ogame-vue-ts/* . ; rm -rf ./ogame-vue-ts/ +# 安装 pnpm 并安装依赖 +RUN corepack enable && corepack prepare pnpm@latest --activate \ + && pnpm install --frozen-lockfile -RUN npm install -g pnpm ; pnpm install ; npx vite build +# 复制源码 +COPY . . -CMD ["npx", "vite", "preview", "--host", "0.0.0.0", "--port", "25121"] \ No newline at end of file +# 生产构建 +RUN pnpm run build + +# ========= 阶段2:运行时 ========= +FROM nginx:alpine + +# 复制构建产物 +COPY --from=builder /app/dist /usr/share/nginx/html + +# 解决 Vue Router history 模式 404 问题 +COPY nginx.conf /etc/nginx/conf.d/default.conf + +EXPOSE 80 + +CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e3a5f04 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,17 @@ +server { + listen 80; + server_name localhost; + + root /usr/share/nginx/html; + index index.html index.htm; + + location / { + try_files $uri $uri/ /index.html; + } + + # 可选:缓存静态资源 + location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff2?|ttf|eot) { + expires 1y; + add_header Cache-Control "public, immutable"; + } +} \ No newline at end of file