oa_front/Dockerfile

37 lines
1.2 KiB
Docker
Raw Normal View History

2024-02-27 17:22:27 +08:00
2024-03-04 09:59:48 +08:00
FROM node:20-slim as base
ENV PROJECT_DIR=/huaxin-front \
PNPM_HOME="/pnpm" \
PATH="$PNPM_HOME:$PATH"
2024-02-27 17:22:27 +08:00
WORKDIR $PROJECT_DIR
2024-03-04 09:59:48 +08:00
COPY ./ $PROJECT_DIR
2024-02-27 17:22:27 +08:00
# 若网络不通,可以使用淘宝源
# RUN pnpm config set registry https://registry.npmmirror.com
2024-03-04 09:59:48 +08:00
# 若不存在安装pnpm
RUN npm install -g pnpm
# 构建项目
# 安装生成依赖
FROM base AS prod-deps
2024-03-04 13:17:32 +08:00
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
2024-03-01 17:23:37 +08:00
2024-03-04 09:59:48 +08:00
# 基于prod-deps生成的依赖执行构建
FROM base AS builder
2024-03-04 13:17:32 +08:00
## 不适用rimraf,因为rimraf是开发环境依赖
2024-03-01 17:23:37 +08:00
COPY --from=prod-deps $PROJECT_DIR/node_modules $PROJECT_DIR/node_modules
2024-03-04 13:17:32 +08:00
# 构建项目 VITE_BASE_URL=/ 为了静态资源assets中文件的路径正确 去掉/api
ENV VITE_BASE_URL=/
RUN pnpm run build
# # 将prod-deps生成的依赖拷贝到base/$PROJECT_DIR中,开始在builder构建阶段build,最后拷贝build生成的dist到base/$PROJECT_DIR中
# FROM base AS result
2024-03-04 10:20:16 +08:00
2024-03-04 11:29:26 +08:00
# 构建nginx,并且在result之后拷贝dist到nginx中其中包含了自定义nginx.conf,
FROM nginx:alpine as production
ENV PROJECT_DIR=/huaxin-front
2024-03-04 13:17:32 +08:00
COPY --from=builder $PROJECT_DIR/dist/ /usr/share/nginx/html
COPY --from=builder $PROJECT_DIR/nginx.conf /etc/nginx/nginx.conf
2024-03-04 11:29:26 +08:00
EXPOSE 80