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

1

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 ?

Jackknife answered 2/9, 2020 at 15:54 Comment(4)
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.)Glabrous
@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-repoJackknife
Did the version number in package-lock.json change?Glabrous
@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 itselfJackknife
J
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.

Jackknife answered 4/9, 2020 at 8:36 Comment(2)
this does not work for me. Does this only work using docker-compose?Thebaid
@TorstenN. - I used this as part of docker-buildJackknife

© 2022 - 2024 — McMap. All rights reserved.