DEV Community

Discussion on: How to Reduce Node Docker Image Size by 10X

Collapse
 
patarapolw profile image
Pacharapol Withayasakpunt • Edited

Currently I use astefanutti/scratch-node. The concept in general is called distroless.

FROM node:12-alpine AS frontend
WORKDIR /app
COPY packages/web-frontend/package.json packages/web-frontend/yarn.lock ./
RUN yarn --frozen-lockfile
COPY packages/web-frontend .
RUN yarn build

FROM node:12-alpine AS server
WORKDIR /app
COPY packages/web-server/package.json packages/web-server/yarn.lock ./
RUN yarn --frozen-lockfile
COPY packages/web-server .
RUN yarn build
RUN yarn --production --frozen-lockfile

FROM astefanutti/scratch-node:12
WORKDIR /app
COPY --from=server /app/node_modules /app/dist ./
COPY --from=frontend /app/dist public
EXPOSE 8080
ENTRYPOINT ["node", "dist/index.js"]

I think it is 97 MB.

I used yarn --frozen-lockfile, but for npm, it would be npm ci.

But scratch-node (and also perhaps alpine in general) is a little problematic if you use native modules, though. Not that it cannot be managed.