emre01 emre01 - 3 months ago 43
Node.js Question

Cannot install package using docker compose

My web app uses Node.js and MongoDB. I am pulling mongo image from docker image hub. When I build an image of my node app it runs fine alone. However when I start up containers using compose it gives the following error. Isn't it supposed to install the packages when image is built from Dockerfile?


web_1 | module.js:457
web_1 | throw err;

web_1 | ^

web_1 |

web_1 | Error: Cannot find module 'mongoose'

web_1 | at Function.Module._resolveFilename (module.js:455:15)

web_1 | at Function.Module._load (module.js:403:25)

web_1 | at Module.require (module.js:483:17)

web_1 | at require (internal/module.js:20:19)

web_1 | at Object. (/worklog/worklog.js:7:14)

web_1 | at Module._compile (module.js:556:32)

web_1 | at Object.Module._extensions..js (module.js:565:10)

web_1 | at Module.load (module.js:473:32)

web_1 | at tryModuleLoad (module.js:432:12)

web_1 | at Function.Module._load (module.js:424:3)


Dockerfile

FROM node

RUN mkdir -p /worklog

WORKDIR /worklog

RUN npm install mongoose \
express \
body-parser \
express-session \
method-override \
connect-mongo \
mongodb

COPY login.html worklog.html workloglist.html worklog.js /worklog/

ENV NODE_VERSION 6.3.1

EXPOSE 4000

CMD npm, start


docker-compose.yml

db:
image: mongo
ports:
- "27017:27017"
command: "--smallfiles --logpath=/dev/null"
web:
build: .
command: node worklog.js
volumes:
- .:/worklog
ports:
- "4000:4000"
links:
- db
environment:
PORT: 4000

Answer

The volumes

volumes:
  - .:/worklog

is basically masking everything you installed to /worklog in the build with the files on the host, including /worklog/node_modules if any. You could try

volumes:
  - .:/worklog
  - /worklog/node_modules

which I think overlays the /worklog/node_modules from the container on top of the directory mounted from the host again making the node_modules visible to the app.