Docker tip: Ställ in byggtidsargument för cache-busting



Vi har nyligen lanserat en praktisk workshop som ger dig en introduktion till praxis inom Docker och DevOps. Vill du få ett smakprov kring vad workshopen innehåller?


Ta del av nedan Docker tip som Marc Klefter tagit fram. Vill du veta mer?

Delta vid workshopen på nästa tillfälle i slutet av mars, Läs mer.

-----------------------------------------------------------------------------


Docker tip: Set build-time arguments for cache busting


Docker uses caching when building images; steps in a Dockerfile that have been built will only be rebuilt if the instructions have changed.


As an example, the following instruction for an image might be present in a Dockerfile:


# Install dependencies for a NodeJS project

RUN npm install


The first time the image is built and this is run, Docker will cache the downloaded dependencies and reuse them the next time the image is built.


What if you explicitly want to force the reinstallation of the dependencies (perhaps to make sure you’re up to date)? This is where Docker build-time arguments prove handy!


In the Dockerfile, add the following:


ARG NPM_CLEAN_CACHE=0

RUN npm install

If the NPM_CLEAN_CACHE argument is set to a new, unique value, the image cache will be rebuilt (“busted”) from the point following the ARG line:


docker build –build-arg NPM_CLEAN_CACHE=${RANDOM}


This technique is thus useful for occasions when you want to explicitly rebuild selected steps in the image.


0 kommentarer

Senaste inlägg

Visa alla