I am new to docker. I tried to dockerize my existing node application. During docker-compose the app throws error stating it cannot find the node_modules.
Here's the error
dockerised-yelpcamp | > dockerised-yelpcamp@1.0.1 start /usr/src/app
dockerised-yelpcamp | > node app.js
dockerised-yelpcamp |
dockerised-yelpcamp | internal/modules/cjs/loader.js:584
dockerised-yelpcamp | throw err;
dockerised-yelpcamp | ^
dockerised-yelpcamp |
dockerised-yelpcamp | Error: Cannot find module 'express'
dockerised-yelpcamp | at Function.Module._resolveFilename (internal/modules/cjs/loader.js:582:15)
dockerised-yelpcamp | at Function.Module._load (internal/modules/cjs/loader.js:508:25)
dockerised-yelpcamp | at Module.require (internal/modules/cjs/loader.js:637:17)
dockerised-yelpcamp | at require (internal/modules/cjs/helpers.js:22:18)
dockerised-yelpcamp | at Object.<anonymous> (/usr/src/app/app.js:1:77)
.
.
.
dockerised-yelpcamp | at Function.Module._load (internal/modules/cjs/loader.js:531:3)
dockerised-yelpcamp | npm ERR! code ELIFECYCLE
dockerised-yelpcamp | npm ERR! errno 1
dockerised-yelpcamp | npm ERR! dockerised-yelpcamp@1.0.1 start: `node app.js`
dockerised-yelpcamp | npm ERR! Exit status 1
dockerised-yelpcamp | npm ERR!
dockerised-yelpcamp | npm ERR! Failed at the dockerised-yelpcamp@1.0.1 start script.
dockerised-yelpcamp | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
dockerised-yelpcamp | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
dockerised-yelpcamp |
dockerised-yelpcamp | npm ERR! A complete log of this run can be found in:
dockerised-yelpcamp | npm ERR! /root/.npm/_logs/2019-04-17T11_14_17_109Z-debug.log
dockerised-yelpcamp exited with code 1
docker-compose.yml
version: "3"
services:
main_app:
container_name: dockerised-yelpcamp
restart: always
build: .
ports:
- 80:3000
links:
- mongo
volumes: ["./:/usr/src/app"]
mongo:
container_name: mongo
image: mongo
ports:
- 27017:27017
Dockerfile
FROM node:10
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
My docker file contains npm install
which is executed successfully while building image.
You copy only package.json, so you need node_modules folder created:
dockerised-yelpcamp | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
if my memory is good, docker container can't respond yes by default. So, he skip creation folder and node_modules stay missing.
Your volumes:
directive is hiding everything the Dockerfile
does with content from your local system. If you don't have a node_modules
directory there or its architecture or library stack is incompatible with what's in the container, this setup simply won't work.
The common workaround seems to be to declare a second anonymous volume to hold node_modules
, which will work until you change your package.json
file.
My recommendation would be to remove the volumes:
directive entirely. Develop your application locally without Docker. Once it works and you've tested it, use this Dockerfile
to rebuild the image. This both gives you a simpler non-Docker development setup, and replicates the environment you have in production (where you're not going to copy the application code in addition to the Docker image that contains it).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.