3

I have docker setup for a node.js web service which creates a data volume for node modules like this:

  volumes:
       - ./service:/usr/src/service
       - /usr/src/service/node_modules

The solution with data volume works fine. But using a data volume, the node_modules are not accessible or visible in my host (not container) which creates other restrictions. For example: I can not run tests in web-storm as they expect node_modules to be present.

Is there any alternative solution other than using data volume to mount node modules which also allows to access them on host?

The docker file looks like:

FROM node:6.3.1-slim

ARG NPM_TOKEN
ARG service
ENV NODE_ENV development

RUN apt-get update && apt-get -y install build-essential git-core python-dev vim

# use nodemon for development
RUN npm install --global nodemon

COPY .npmrc /root/.npmrc

RUN mkdir -p /usr/src/$service
RUN mkdir -p /modules/$service/
COPY sources/$service/package.json /modules/$service/package.json
RUN cd /modules/$service/ && npm install && rm -f /root/.npmrc && mv /modules/$service/node_modules /usr/src/$service/node_modules

WORKDIR /usr/src/$service
COPY sources/$service /usr/src/$service

ENV service=${service}

COPY start_services.sh /root/start_services.sh
COPY .env /root/.env
COPY services.sh /root/services.sh

RUN ["chmod", "+x", "/root/start_services.sh"]
RUN ["chmod", "+x", "/root/services.sh"]
CMD /root/start_services.sh
Rajat Arora
  • 532
  • 5
  • 17
  • Possible duplicate of [Docker-compose: node\_modules not present in a volume after npm install succeeds](https://stackoverflow.com/questions/30043872/docker-compose-node-modules-not-present-in-a-volume-after-npm-install-succeeds) – codelitt Jul 06 '17 at 13:02
  • its not. Because i am using the solution from the post you mentioned. But as I wrote, the real problem at the end I have is that i do not have access to node modules on my host machine because of data volume. – Rajat Arora Jul 06 '17 at 14:47
  • Apologies. That wasn't immediately clear to me. I see what is going on now. – codelitt Jul 06 '17 at 15:08

1 Answers1

0

Specify node_modules as follows:

volumes:
  - .:/usr/src/service/
  - /usr/src/service/node_modules
codelitt
  • 343
  • 2
  • 13
  • I do that already. Sorry forgot to mention that. Updated that part in the question. – Rajat Arora Jul 07 '17 at 12:09
  • @RajatArora Mmm okay. What does your Dockerfile look like? – codelitt Jul 07 '17 at 13:54
  • I updated the question with my docker file. Thank you for taking the time to help. – Rajat Arora Jul 07 '17 at 14:12
  • @RajatArora sure. It seems in the Dockerfile you're copying over the `sources/service` directory and then syncing the volume `./service`. Should one of those be changed to match the other? Not sure from where things are being ran, but that's something to check (check simple things first). – codelitt Jul 07 '17 at 14:39