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