partner-admin/Dockerfile
2025-03-12 09:21:09 +08:00

70 lines
1.7 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 构建阶段
FROM node:18-alpine as build-stage
# 切换 Alpine 镜像源为阿里云
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
WORKDIR /app
# 设置环境变量
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
# 添加构建时间戳,用于破除缓存
ARG BUILD_TIMESTAMP=$(date +%s)
ENV BUILD_TIMESTAMP=${BUILD_TIMESTAMP}
# 输出构建环境信息
RUN echo "Building for environment: ${NODE_ENV}"
RUN echo "Build timestamp: ${BUILD_TIMESTAMP}"
# 复制 package.json 和 package-lock.json
COPY package*.json ./
RUN npm config set registry http://mirrors.cloud.tencent.com/npm/
# 安装依赖
RUN npm install --production=false
# 复制源代码
COPY . .
# 构建应用
RUN npm run build
# 生产阶段
FROM nginx:stable-alpine as production-stage
# 安装基础工具
RUN apk add --no-cache bash curl
# 创建必要的目录并设置权限
RUN mkdir -p /var/cache/nginx/client_temp \
/var/cache/nginx/proxy_temp \
/var/cache/nginx/fastcgi_temp \
/var/cache/nginx/uwsgi_temp \
/var/cache/nginx/scgi_temp \
&& chmod 700 /var/cache/nginx/* \
&& chown -R nginx:nginx /var/cache/nginx
# 复制 nginx 配置
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 从构建阶段复制构建结果
COPY --from=build-stage /app/dist /usr/share/nginx/html
# 修改目录权限
RUN chown -R nginx:nginx /usr/share/nginx/html \
&& chmod -R 755 /usr/share/nginx/html \
&& chown -R nginx:nginx /var/log/nginx \
&& chmod -R 755 /var/log/nginx \
&& touch /var/run/nginx.pid \
&& chown -R nginx:nginx /var/run/nginx.pid \
&& chmod -R 755 /etc/nginx/conf.d
# 使用root用户运行nginx标准做法
# USER nginx
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]