staff_data/Dockerfile

103 lines
2.7 KiB
Docker
Raw Normal View History

2024-07-11 11:03:11 +08:00
# 基础镜像
FROM node:20-alpine as base
2024-12-30 09:22:38 +08:00
# 更改 apk 镜像源为阿里云
2025-01-03 09:24:46 +08:00
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
# 设置 npm 镜像源
RUN yarn config set registry https://registry.npmmirror.com
# 全局安装 pnpm 并设置其镜像源
RUN yarn global add pnpm && pnpm config set registry https://registry.npmmirror.com
2024-07-11 11:03:11 +08:00
# 设置工作目录
WORKDIR /app
# 复制 pnpm workspace 配置文件
COPY pnpm-workspace.yaml ./
# 首先复制 package.json, package-lock.json 和 pnpm-lock.yaml 文件
COPY package*.json pnpm-lock.yaml* ./
2025-01-03 09:24:46 +08:00
COPY tsconfig.json .
# 利用 Docker 缓存机制,如果依赖没有改变则不会重新执行 pnpm install
#100-500 5-40
2024-07-11 11:03:11 +08:00
FROM base As server-build
WORKDIR /app
COPY packages/common /app/packages/common
2024-12-30 09:22:38 +08:00
COPY apps/server /app/apps/server
2025-01-03 09:24:46 +08:00
RUN pnpm install --filter server
RUN pnpm install --filter common
RUN pnpm --filter common generate && pnpm --filter common build:cjs
RUN pnpm --filter server build
2024-07-11 11:03:11 +08:00
FROM base As server-prod-dep
WORKDIR /app
COPY packages/common /app/packages/common
2024-12-30 09:22:38 +08:00
COPY apps/server /app/apps/server
2025-01-03 09:24:46 +08:00
RUN pnpm install --filter common --prod
RUN pnpm install --filter server --prod
2024-12-30 09:22:38 +08:00
2024-07-11 11:03:11 +08:00
FROM server-prod-dep as server
WORKDIR /app
ENV NODE_ENV production
COPY --from=server-build /app/packages/common/dist ./packages/common/dist
2024-12-30 09:22:38 +08:00
COPY --from=server-build /app/apps/server/dist ./apps/server/dist
COPY apps/server/entrypoint.sh ./apps/server/entrypoint.sh
2025-01-03 09:24:46 +08:00
2024-12-30 09:22:38 +08:00
RUN chmod +x ./apps/server/entrypoint.sh
2025-01-03 09:24:46 +08:00
RUN apk add --no-cache postgresql-client
2024-07-11 11:03:11 +08:00
2024-12-30 09:22:38 +08:00
EXPOSE 3000
2024-07-11 11:03:11 +08:00
2024-12-30 09:22:38 +08:00
ENTRYPOINT [ "/app/apps/server/entrypoint.sh" ]
2024-07-11 11:03:11 +08:00
2024-12-30 09:22:38 +08:00
FROM base AS web-build
2024-07-11 11:03:11 +08:00
# 复制其余文件到工作目录
COPY . .
2025-01-03 09:24:46 +08:00
RUN pnpm install
RUN pnpm --filter web build
2024-07-11 11:03:11 +08:00
# 第二阶段,使用 nginx 提供服务
2024-12-30 09:22:38 +08:00
FROM nginx:stable-alpine as web
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
2024-07-11 11:03:11 +08:00
# 设置工作目录
WORKDIR /usr/share/nginx/html
# 设置环境变量
ENV NODE_ENV production
# 将构建的文件从上一阶段复制到当前镜像中
2024-12-30 09:22:38 +08:00
COPY --from=web-build /app/apps/web/dist .
2024-07-11 11:03:11 +08:00
# 删除默认的nginx配置文件并添加自定义配置
RUN rm /etc/nginx/conf.d/default.conf
2024-12-30 09:22:38 +08:00
COPY apps/web/nginx.conf /etc/nginx/conf.d
2024-07-11 11:03:11 +08:00
# 添加 entrypoint 脚本,并确保其可执行
2024-12-30 09:22:38 +08:00
COPY apps/web/entrypoint.sh /usr/bin/
2024-07-11 11:03:11 +08:00
RUN chmod +x /usr/bin/entrypoint.sh
# 安装 envsubst 以支持环境变量替换
2024-12-30 09:22:38 +08:00
RUN apk add --no-cache gettext
2024-07-11 11:03:11 +08:00
# 暴露 80 端口
EXPOSE 80
CMD ["/usr/bin/entrypoint.sh"]
2025-01-03 09:24:46 +08:00
FROM nginx:stable-alpine as nginx
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
2024-07-11 11:03:11 +08:00
# 设置工作目录
2025-01-03 09:24:46 +08:00
WORKDIR /usr/share/nginx/html
2024-07-11 11:03:11 +08:00
2025-01-03 09:24:46 +08:00
# 设置环境变量
ENV NODE_ENV production
2024-07-11 11:03:11 +08:00
2025-01-03 09:24:46 +08:00
# 安装 envsubst 以支持环境变量替换
RUN apk add --no-cache gettext
2024-07-11 11:03:11 +08:00
2025-01-03 09:24:46 +08:00
# 暴露 80 端口
EXPOSE 80
2024-07-11 11:03:11 +08:00