2

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 ?

shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39
  • 1
    When you change the version number of the dependent library in the `package-lock.json` file, that will invalidate the Docker cache and re-run the `npm install` sequence. (Even if it's "shared code" that you wrote, treat it as a library and use proper semantic versioning for it.) – David Maze Sep 02 '20 at 16:10
  • @DavidMaze - unfortunately, this did not help. I already had my `package.json` set as described above, and the `docker-compose` command used the cache without checking if there is a newer version for `myorg-common-repo` – shapiro yaacov Sep 03 '20 at 18:54
  • 1
    Did the version number in `package-lock.json` change? – David Maze Sep 03 '20 at 20:09
  • @DavidMaze - I'm not sure if you're asking whether I changed it or it got changed during the process, but in any case, it was not changed. If you suggestion is that I change it manually - that is not what I'm looking for since I could also just change the `package.json` file itself – shapiro yaacov Sep 04 '20 at 07:37

1 Answers1

5

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.

shapiro yaacov
  • 2,308
  • 2
  • 26
  • 39