一樣我們今天來看怎麼可以例用multi-staging來幫同事的node js docker image瘦身
首先先看第一版的
FROM node:14.17-alpine
RUN apk add --no-cache \
vim \
git \
curl \
bash \
python2 \
make \
gcc \
g++ \
openssh-client
WORKDIR /bidAgent
# nodejs dependencies
COPY . /bidAgent
RUN yarn global add pm2@4.4.0 && pm2 install pm2-logrotate && yarn global add lerna --ignore-engines
RUN yarn install --only=production && yarn cache clean
RUN lerna link
RUN echo `mktemp -d` > $HOME/TMPNAME
ENV NODE_ENV=production
CMD ["pm2-runtime", "pm2.config.js"]
Build 完size 大約1.31 GB
ccc latest c229a62610be 3 minutes ago 1.31GB
使用multi-staging
FROM node:14.17-slim as base
RUN apt update -y && \
apt install -y \
vim \
curl \
bash \
openssh-client && \
rm -rf /var/lib/apt/lists/*
WORKDIR /bidAgent
# nodejs dependencies
FROM base as builder
RUN apt update -y && \
apt install -y \
git \
build-essential \
python3
COPY package.json /bidAgent
COPY lerna.json /bidAgent
COPY yarn.lock /bidAgent
COPY . /bidAgent
RUN yarn install --only=production && yarn cache clean
RUN rm -rf /bidAgent/node_modules/node-rdkafka/deps/librdkafka/examples
FROM base as production
COPY --from=builder /bidAgent/node_modules /bidAgent/node_modules
COPY --from=builder /bidAgent/packages /bidAgent/packages
COPY . /bidAgent
RUN yarn global add pm2@4.4.0 && pm2 install pm2-logrotate && yarn global add lerna --ignore-engines
RUN lerna link
RUN echo `mktemp -d` > $HOME/TMPNAME
ENV NODE_ENV=production
CMD ["node", "/bidAgent/index.js"]
這邊我有把alpine換成debian-slim,雖然image會大一些,但可以縮短一些build time
然後因為我有用dfimage去檢查node-rdkafka這個套件,儼然發現放了一堆檔案在example所以手動的把他刪除了也省去不少空間
所以大家真的要多利用hive和dfimage檢查自己的image,多花一點時間,可以省下很多麻煩。
Build 完 Size從 1.31 GB 縮減到876 MB
ddd latest 5e22f26244ae 41 seconds ago 876MB
因此大家可以回頭檢視看看自家的image做些調整。
這樣在HPA部署或是Node provision起來要部署pod的時候會更縮短時間喔。