Files

52 lines
1.7 KiB
Docker
Raw Permalink Normal View History

2026-10-21 12:21:45 +08:00
# 多阶段构建:先在 node 里编译,最终镜像只保留 nginx + dist
FROM node:24 AS build
# 由 magic-docker 传入:--build-arg VERSION=<tag>
ARG VERSION=0.0.0
WORKDIR /app
# 构建上下文就是 context 目录
COPY . .
RUN npm ci
RUN npm pkg set version="$VERSION"
# 有 build-only 脚本就用它(兼容老项目),否则用 build
RUN if npm run 2>/dev/null | grep -q "build-only"; then \
npm run build-only; \
else \
npm run build; \
fi
# history 路由回退 + 跨域:context 里有 nginx.conf 就用项目的,否则用内置默认配置
RUN set -e; \
if [ -f nginx.conf ]; then \
cp nginx.conf /app/nginx.conf; \
echo "使用项目自带的 nginx.conf"; \
else \
printf '%s\n' \
'server {' \
' listen 80;' \
' root /usr/share/nginx/html;' \
' index index.html;' \
'' \
' add_header Access-Control-Allow-Origin * always;' \
' add_header Access-Control-Allow-Methods "GET, POST, OPTIONS, PUT, DELETE" always;' \
' add_header Access-Control-Allow-Headers "*, Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Token" always;' \
' add_header Access-Control-Allow-Credentials "true" always;' \
'' \
' if ($request_method = "OPTIONS") {' \
' return 204;' \
' }' \
'' \
' location / {' \
' try_files $uri $uri/ /index.html;' \
' }' \
'}' \
> /app/nginx.conf; \
echo "使用内置的 nginx.conf"; \
fi
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
COPY --from=build /app/nginx.conf /etc/nginx/conf.d/default.conf