chore: 优化 Dockerfile 缩小镜像体积

This commit is contained in:
bennett 2024-11-29 16:54:26 +08:00
parent f01465aa6c
commit 7aad416202
2 changed files with 31 additions and 13 deletions

4
.dockerignore Normal file
View File

@ -0,0 +1,4 @@
**/node_modules
**/dist
**/logs
**/package-lock.json

View File

@ -1,24 +1,38 @@
# 镜像集成 # 镜像集成
FROM node:18 FROM node:18-slim AS builder
WORKDIR /builder
# 复制文件到工作区间
COPY web/ /builder/web/
COPY server/ /builder/server/
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
npm config set registry https://registry.npmmirror.com && \
apt-get update && apt-get install -y build-essential && \
cd /builder/web && npm install && npm run build-only && \
cd /builder/server && npm install && npm run build
FROM node:18-slim
# 设置工作区间 # 设置工作区间
WORKDIR /xiaoju-survey WORKDIR /xiaoju-survey
# 复制文件到工作区间
COPY . /xiaoju-survey
# 安装nginx # 安装nginx
RUN apt-get update && apt-get install -y nginx RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list.d/debian.sources && \
npm config set registry https://registry.npmmirror.com && \
RUN npm config set registry https://registry.npmjs.org/ apt-get update && apt-get install -y nginx
# 安装项目依赖
RUN cd /xiaoju-survey/web && npm install && npm run build-only
# 仅复制运行需要的文件到工作区间
COPY --from=builder /builder/web/dist/ /xiaoju-survey/web/dist/
COPY --from=builder /builder/server/dist/ /xiaoju-survey/server/dist/
COPY --from=builder /builder/server/node_modules/ /xiaoju-survey/server/node_modules/
COPY --from=builder /builder/server/public/ /xiaoju-survey/server/public/
COPY --from=builder /builder/server/package*.json /builder/server/.env* /xiaoju-survey/server/
COPY docker-run.sh /xiaoju-survey/docker-run.sh
# 覆盖nginx配置文件 # 覆盖nginx配置文件
COPY ./nginx/nginx.conf /etc/nginx/nginx.conf COPY nginx/nginx.conf /etc/nginx/nginx.conf
RUN cd /xiaoju-survey/server && npm install && npm run build
# 暴露端口 需要跟nginx的port一致 # 暴露端口 需要跟nginx的port一致
EXPOSE 80 EXPOSE 80