javascript - How to cache node_modules for unchanged package.json during yarn / npm install during docker build? -


i have dockerfile node application looking this:

from node:8.3 env term=xterm-color npm_config_loglevel=warn path="$path:/usr/src/app/node_modules/.bin/" workdir /usr/src/app add . /usr/src/app run yarn install --frozen-lockfile --ignore-platform --ignore-engines --quiet && tsc && webpack cmd ["node", "/usr/src/app/server"] 

yet means on every change sourcetree, layer run yarn install ... invalidated , run again.

as yarn install takes 80s, built unnecessarily slowed down if readme.md changed.

i want use run yarn install once either package.json or yarn.lock changes.

this question apply npm , packlage

you can cache package.json , appropriate lock file , run intallation in temp folder. after adding source, can mv node_modules , files main app folder.

here's example if using yarn install.

from node:8.3 env term=xterm-color npm_config_loglevel=warn path="$path:/usr/src/app/node_modules/.bin/" volume ["/logs"] workdir /tmp/node add package.json yarn.lock ./ run yarn install --frozen-lockfile --ignore-platform --ignore-engines --quiet workdir /usr/src/app add . /usr/src/app run mv /tmp/node/* ./ && tsc && webpack cmd ["node", "/usr/src/app/server"] 

a quick metric using time return these output tweaked dockerfile on docker build .:

  • on source file change:
    • 0m34.084s
  • on yarn.lock change:
    • 2m22.774s

for npm, need, depending on version, need add either

  • npm-shrinkwrap.json
  • package-lock.json

as insted of yarn.lock file.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -