'Angular docker file takes too much time to build

I am using dockerfile to build and run. Docker file build takes 10-15 minutes to build. But npm run build takes 2-3 minutes. Any ideas how I can improve this docker file for faster build time.

FROM node:14.18-alpine as build
WORKDIR /usr/local/frontend
RUN npm cache clean --force
COPY ./ /usr/local/frontend/
RUN npm install
RUN npm run build --prod

# Nginx
FROM nginx:1.21-alpine
# COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/local/frontend/dist /usr/share/nginx/html
# CMD ["nginx", "-g", "daemon off;"]
EXPOSE 9020


Solution 1:[1]

If you copy the package.json file first and then do npm install, you create a cache layer with all the npm packages that only needs to be refreshed if you change the package.json file.

I don't think the cache clean does anything since the image is a 'blank' node image at the point where you run it.

FROM node:14.18-alpine as build
WORKDIR /usr/local/frontend
RUN npm cache clean --force
COPY package.json .
RUN npm install
COPY ./ /usr/local/frontend/
RUN npm run build --prod

# Nginx
FROM nginx:1.21-alpine
# COPY nginx.conf /etc/nginx/nginx.conf
COPY --from=build /usr/local/frontend/dist /usr/share/nginx/html
# CMD ["nginx", "-g", "daemon off;"]
EXPOSE 9020

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hans Kilian