Skip to content

Instantly share code, notes, and snippets.

@dweinstein
Last active June 29, 2022 16:59
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save dweinstein/9468644 to your computer and use it in GitHub Desktop.
Save dweinstein/9468644 to your computer and use it in GitHub Desktop.
Install node modules before copying over your working code so that node_modules are built (and cached) before you change your service code!
# ...
ADD package.json /tmp/package.json
RUN cd /tmp && npm install && \
mkdir -p /opt/app && cp -a /tmp/node_modules /opt/app/
# ...
WORKDIR /opt/app
ADD . /opt/app
@dweinstein
Copy link
Author

If the package.json file changes then Docker will re-run the npm install sequence... otherwise our modules are cached so we aren't rebuilding them every time we change our apps source code!

@NoumanSaleem
Copy link

👍

@rationull
Copy link

For those like myself who were initially bewildered by this, the "Build cache" section of the Dockerfile best practices doc may help fill in the gaps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment