Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Chore]: 优化 Dockerfile 缩小镜像体积 #452

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/node_modules
**/dist
**/logs
**/package-lock.json
38 changes: 26 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down