55 lines
1.3 KiB
Docker
55 lines
1.3 KiB
Docker
# 构建阶段
|
|
FROM node:18-alpine as build-stage
|
|
|
|
# 切换 Alpine 镜像源为阿里云
|
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
|
|
|
# 设置环境变量
|
|
ARG NODE_ENV=production
|
|
ENV NODE_ENV=${NODE_ENV}
|
|
|
|
# 安装 yarn
|
|
RUN apk add --no-cache yarn
|
|
|
|
# 设置 yarn 和 npm 镜像源为淘宝源
|
|
RUN yarn config set registry https://registry.npmmirror.com && \
|
|
npm config set registry https://registry.npmmirror.com
|
|
|
|
# 设置工作目录
|
|
WORKDIR /app
|
|
|
|
# 复制 package.json 和 yarn.lock
|
|
COPY package.json yarn.lock* ./
|
|
|
|
# 安装依赖
|
|
RUN yarn install --frozen-lockfile
|
|
|
|
# 复制项目文件
|
|
COPY . .
|
|
|
|
# 根据环境变量选择构建命令
|
|
RUN if [ "$NODE_ENV" = "production" ]; then \
|
|
yarn build:prod; \
|
|
elif [ "$NODE_ENV" = "testing" ]; then \
|
|
yarn build:test; \
|
|
else \
|
|
yarn build:dev; \
|
|
fi
|
|
|
|
# 生产阶段
|
|
FROM nginx:stable-alpine as production-stage
|
|
|
|
# 切换 Alpine 镜像源为阿里云
|
|
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
|
|
|
# 复制构建结果到Nginx目录
|
|
COPY --from=build-stage /app/dist /usr/share/nginx/html
|
|
|
|
# 复制Nginx配置文件
|
|
COPY nginx.conf /etc/nginx/conf.d/default.conf
|
|
|
|
# 暴露80端口
|
|
EXPOSE 80
|
|
|
|
# 启动Nginx
|
|
CMD ["nginx", "-g", "daemon off;"] |