There’s an issue with npm have been stuck for a few days. It’s you have to build the node_modules at build time, otherwise it’s not accessible at runtime. And finally found a solution on https://dev.to/alex_barashkov/using-docker-for-nodejs-in-development-and-production-3cgp.
Here’s another best practice adjustment: Copy your package.json and package-lock.json before you copy your code into the container. Docker will cache installed node_modules as a separate layer, then, if you change your app code and execute the build command, the node_modules will not be installed again if you did not change package.json. Generally speaking, even if you forget to add those line, you will not encounter a lot of problems. Usually, you will need to run a docker build only when your package.json was changed, which leads you to install from scratch anyway. In other cases, you don’t run docker build too often after your initial build in the development environment.
In another word, Having
COPY package*.json ./
RUN npm install
in your Dockerfile to do the npm install. Then in the docker-compose.yml
build: .
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
command: npm start