Docker tip: Set build-time arguments for cache busting



We have recently launched a practical workshop that gives you an introduction to practice in Docker and DevOps. Do you want to get a taste of what the workshop contains?


Take a look at the Docker tip below from Marc Klefter. Do you want to know more? Attend the workshop on the next occasion at the end of March, Read more.

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


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