Categories
JavaScript Answers

How to cache the RUN npm install instruction when docker build a Dockerfile with JavaScript?

Spread the love

To cache the RUN npm install instruction when docker build a Dockerfile with JavaScript, we copy the package.json file to the Docker volume.

For instance, in our Dockerfile, we write

# install node modules
WORKDIR  /usr/app
COPY     package.json /usr/app/package.json
RUN      npm install

# install application
COPY     . /usr/app

to add

COPY     package.json /usr/app/package.json

to copy package.json from the project to /usr/app/package.json in the Docker volumne.

Then npm install the packages that isn’t already installed in the volumne.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *