oa_front/Dockerfile

34 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-01 17:23:37 +08:00
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile
2024-03-04 09:59:48 +08:00
# 基于prod-deps生成的依赖执行构建
FROM base AS builder
2024-03-04 10:09:00 +08:00
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
2024-03-01 17:23:37 +08:00
RUN pnpm run build
2024-03-04 09:59:48 +08:00
# 将prod-deps生成的依赖拷贝到base/$PROJECT_DIR中,开始在builder构建阶段build,最后拷贝build生成的dist到base/$PROJECT_DIR中
FROM base AS result
2024-03-01 17:23:37 +08:00
COPY --from=prod-deps $PROJECT_DIR/node_modules $PROJECT_DIR/node_modules
2024-03-04 09:59:48 +08:00
COPY --from=builder $PROJECT_DIR/dist $PROJECT_DIR/dist
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
COPY --from=result $PROJECT_DIR/dist/ /usr/share/nginx/html
COPY --from=result $PROJECT_DIR/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80