Docker Community Forums

Share and learn in the Docker community.

docker shared volume


(Vivekjava) #1

i am facing issue while re-using global node_modules inside my container ,
my requirement is , i want to use my older npm’s inside my container by sharing the volumes where my global node_modules are present… i have tried most of the approaches but still i cant able to re-use my global npms inside the containers…

db:
image: mongo
ports:

  • "27017:27017"
    restart: always
    web:
    build: .
    volumes:
  • ./:/app
  • ./node_modules:/usr/local/lib/node_modules
    ports:
  • "6000:6000"
    links:
  • db
    command: node /app/index.js

This is my docker-compose file.


(David Maze) #2

Having your application depending on code and especially shared libraries injected by the host is not a best practice.

That’s very easy to accomplish. Put an appropriate version constraint in your package.json file. Both npm and yarn have a “lock” file that names very specific versions of the packages that will be installed. Then a routine Dockerfile will work for you, something like

FROM node:8 
COPY package.json yarn.lock .
RUN yarn install
COPY main.js src .

Each image will have its own node_modules tree, just like each source directory in your developer workspace will have its own node_modules tree.