2

My current docker image build of an Angular 8 application takes about 30 minutes. Is there a way to skip the npm install step by using a folder with node modules prebuilt on disk on the server?

Here is my current Dockerfile:

FROM node:8-alpine as builder

COPY package.json package-lock.json ./

RUN npm set progress=false && npm config set depth 0 && npm cache clean --force

## Storing node modules on a separate layer will prevent unnecessary npm installs at each build
RUN npm i && mkdir /ng-app && cp -R ./node_modules ./ng-app

WORKDIR /ng-app

COPY . .

## Build the angular app in production mode and store the artifacts in dist folder
RUN $(npm bin)/ng build --prod

FROM nginx:1.13.3-alpine

## Copy our default nginx config
COPY nginx/default.conf /etc/nginx/conf.d/

## Remove default nginx website
RUN rm -rf /usr/share/nginx/html/*

## From 'builder' stage copy over the artifacts in dist folder to default nginx public folder
COPY --from=builder /ng-app/dist /usr/share/nginx/html

CMD ["nginx", "-g", "daemon off;"]
Jakub Bares
  • 159
  • 1
  • 11
  • 1
    Docker will cache the results of a previous `docker build` (splitting the `COPY` into two separate parts as you've done helps this) so it shouldn't repeat the `npm install` step once you've done it once. 30 minutes is a _long_ time; what part specifically is having problems, and do you have the same trouble from your host outside of Docker? – David Maze Jan 13 '20 at 00:04
  • 2
    You can reduce the number of layers and make image more compact and building faster by uniting some of your commands and getting rid of **RUN** related to **npm** as there's no longer need to build, it's pre-built. Each command line is a layer that takes more memory and time. – dmitryro Jan 13 '20 at 00:10
  • 2
    Yes, you can combine COPY and RUN commands together and reduce number of layers in the image. Also, the first run will only taking longer time and consecutive builds will completer in lesser times due to caching of underlying layers. Are you doing the build as a step in the CI pipeline or just doing it manually? – Anuradha Fernando Jan 13 '20 at 04:06
  • @dmitryro Can you give an example? I dont understand how to remove the run and yet still make the npm build itself at first run – Jakub Bares Jan 16 '20 at 14:19
  • @JakubBares See the *Dockerfile.2* example in the question showing how lines belong to a single **RUN** (same applies to **COPY**) with lines separated with `&&\ ` - https://stackoverflow.com/questions/39223249/multiple-run-vs-single-chained-run-in-dockerfile-what-is-better – dmitryro Jan 16 '20 at 14:28

0 Answers0