From 7aad41620271ec2eeead646eb0893e3fe23904ac Mon Sep 17 00:00:00 2001 From: bennett Date: Fri, 29 Nov 2024 16:54:26 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=BC=98=E5=8C=96=20Dockerfile=20?= =?UTF-8?q?=E7=BC=A9=E5=B0=8F=E9=95=9C=E5=83=8F=E4=BD=93=E7=A7=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 4 ++++ Dockerfile | 38 ++++++++++++++++++++++++++------------ 2 files changed, 30 insertions(+), 12 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..6bd61043 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +**/node_modules +**/dist +**/logs +**/package-lock.json diff --git a/Dockerfile b/Dockerfile index e61b04cf..fb07d764 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,24 +1,38 @@ # 镜像集成 -FROM node:18 +FROM node:18-slim AS builder -# 设置工作区间 -WORKDIR /xiaoju-survey +WORKDIR /builder # 复制文件到工作区间 -COPY . /xiaoju-survey +COPY web/ /builder/web/ +COPY server/ /builder/server/ -# 安装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 && \ + 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 -RUN npm config set registry https://registry.npmjs.org/ -# 安装项目依赖 -RUN cd /xiaoju-survey/web && npm install && npm run build-only +FROM node:18-slim -# 覆盖nginx配置文件 -COPY ./nginx/nginx.conf /etc/nginx/nginx.conf +# 设置工作区间 +WORKDIR /xiaoju-survey -RUN cd /xiaoju-survey/server && npm install && npm run build +# 安装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 && \ + apt-get update && apt-get install -y nginx + +# 仅复制运行需要的文件到工作区间 +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配置文件 +COPY nginx/nginx.conf /etc/nginx/nginx.conf # 暴露端口 需要跟nginx的port一致 EXPOSE 80