Run npm update in docker without using the cache on that specific update

Viewed 6889

Background:

  1. I'm writing code in node.js, using npm and docker. I'm trying to get my docker file to use cache when I build it so it doesn't take too long.
  2. We have a "common" repo that we use to keep logic that is used in a variety of repositories and this gets propagated is npm packages.

The problem:

I want the docker file NOT use the cache on my "common" package.

Docker file:

FROM node:12-alpine as X

RUN npm i npm@latest -g
RUN mkdir /app && chown node:node /app
WORKDIR /app

RUN apk add --no-cache python3 make g++ tini \
    && apk add --update tzdata

USER node
COPY package*.json ./
COPY .npmrc .npmrc
RUN npm install --no-optional && npm cache clean --force
ENV PATH /app/node_modules/.bin:$PATH
COPY . .

package.json has this line:

"dependencies": {
  "@myorg/myorg-common-repo": "~1.0.13",

I have tried adding these lines in a variety of places and nothing seems to work:

  1. RUN npm uninstall @myorg/myorg-common-repo && npm install @myorg/myorg-common-repo
  2. RUN npm update @myorg/myorg-common-repo --force

Any ideas on how I can get docker to build and not use the cache on @myorg/myorg-common-repo ?

1 Answers

So I finally managed to solve this using this answer:

What we want to do is invalidate the cache for a specific block in the Docker file and then run our update command. This is done by adding a build argument to the command (CLI or Makefile) like so:

docker-compose -f docker-compose-dev.yml build --build-arg CACHEBUST=0

And then Adding this additional block to the Docker file:

ARG CACHEBUST=1 
USER node
RUN npm update @myorg/myorg-common-repo

This does what we want.
The ARG CACHEBUST=1 invalidates the cache and the npm update command runs without it.

Related