'Update cached monorepo node_modules in docker volume

I have a problem with updating node_modules that are shared as a docker volume in nx monorepo.

services:
  service-a:
    container_name: service-a
    volumes:
      - ./:/app
      - node-modules:/app/node_modules
  service-b:
    container_name: service-b
    volumes:
      - ./:/app
      - node-modules:/app/node_modules
volumes:
  node-modules:
FROM node:14-alpine
WORKDIR /app
COPY package*.json ./

RUN npm install

# start app
CMD npm run nx serve ${APP_NAME} --host 0.0.0.0

Service-a and service-b run two nest.js applications inside docker containers, node_modules are shared. Frontend run on the host machine outside docker.

Since the volume during each build step is discarded after that step completes I cannot update packages when container is built. When I update packages I run update process on my host, then run npm install inside one of docker containers. It works pretty well most of the time but major updates are painful. For example when nx has some migrations then it will modify the code on host but node_modules inside container are untouched. When I try to run docker container it might fail (new code run with old packages) so I will be unable to run npm install inside that container.

I might create special docker container that will run and hang (like tail -f /dev/null) and then execute docker exec update-service npm install.

Do you have better idea to solve this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source