Alexander Mills
Alexander Mills

Reputation: 100010

Speed up NPM install in Docker container

We use the standard practices of not including node_modules in version control. However, when moving through the CI/CD pipeline, we have to reinstall NPM dependencies in several places and it makes everything very slow.

Is there a way to somehow cache NPM dependencies with Docker? I searched Google "docker cache npm dependencies" and the first hit from 2014 yielded a dead link.

Nothing else of much value came up.

One solution is to include node_modules in version control, but I think that would be a huge mistake. I think caching the dependencies somehow would be the best option.

Here is the Dockerfile as is:

FROM node:6

COPY . .  # copy all files, but node_modules does not exist ( => gitignored)

RUN npm install --no-optional --only=production > /dev/null 2>&1
RUN npm install -g bower  > /dev/null 2>&1
RUN bower install --config.interactive=false --allow-root > /dev/null 2>&1

ENTRYPOINT ["/bin/bash", "/root/cdt/run.sh"]

Here is one possible solution, but I can't quite figure out how it works:

=> http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/

Upvotes: 42

Views: 52445

Answers (4)

kain
kain

Reputation: 264

The accepted solution doesn't work for me, my code had been already like that.

Update the command from npm install to npm ci. ci here means clean and install. When I updated it, my installing in Docker finished in 6 minutes which can not be finished before even spent 30 more minutes.

Upvotes: 0

AlexanderB
AlexanderB

Reputation: 959

Additional tricks for collection:
In square brackets is the relative time saved in my case

RUN mkdir node_modules
  • Skipping security audit on docker. [-30sec]
RUN npm ci --no-audit
  • Using other package managers like pnpm, even with additional install it's still faster.
RUN npm install -g pnpm
RUN pnpm install

Upvotes: 6

Alexander Mills
Alexander Mills

Reputation: 100010

This method works like magic:

https://blog.playmoweb.com/speed-up-your-builds-with-docker-cache-bfed14c051bf

Docker has a special way of caching things for you, and apparently it's best to use the inborn caching ability.

Cannot say I completely understand how it works, but it does work.

If you follow this pattern, it will work for you:

FROM mhart/alpine-node:5.6.0
WORKDIR /src

# Expose the port 3000
EXPOSE 3000

# Set the default command to run when a container starts
CMD ["node", "server.js"]

# Install app dependencies
COPY package.json /src
RUN npm install

# Copy your code in the docker image
COPY . /src

Upvotes: 27

Jamesed
Jamesed

Reputation: 302

Have you tried using yarn instead of npm which is way faster? Yarn does parallel package installations

https://yarnpkg.com/lang/en/compare/

Upvotes: 6

Related Questions