How I Reduced Docker Image Size from 1.43 GB to 22.4 MB | by Mohammad Faisal | JavaScript In Plain English | Medium
Step 4. Multistage Build
In our previous configurations, we were copying all of our source codes into the working directory.
But it’s unnecessary as we only need the build folder to serve our website. So now we will use the concept of multi-stage build to cut down the unnecessary code and dependencies from our final image.
The configuration will look something like this.
# STAGE 1
FROM node:12-alpine AS build
WORKDIR /app
COPY package.json ./
RUN yarn install
COPY . /app
RUN yarn build
# STAGE 2
FROM node:12-alpine
WORKDIR /app
RUN npm install -g webserver.local
COPY --from=build /app/build ./build
EXPOSE 3000
CMD webserver.local -d ./build
January 13, 2021 at 3:18:16 PM EST
*
FILLER