Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node_modules missing even after npm install in docker container

The Dockerfile being used:

FROM node:8-alpine

WORKDIR /usr/src/app
COPY . .
RUN npm install

CMD ["npm", "run", "serve"]

EXPOSE 8080

And the docker-compose.yml file:

version: '3'

services:
    app:
        container_name: app
        restart: always
        build:
            context: ./app
            dockerfile: Dockerfile
        ports:
            - "8080:8080"
        volumes:
            - ./app:/usr/src/app
            - ./logs:/logs

The folder structure is the following:

project/
|-- docker-compose.yml
|-- logs/
|-- app/
    |-- Dockerfile
    |-- package.json

When running docker-compose up --build from project/, the npm install step outputs the following after about one minute:

added 1684 packages from 1297 contributors and audited 36429 packages in 56.23s
found 0 vulnerabilities

However, at the npm run serve step, the output basically consists in saying that no npm module can be found, and among other things, this line:

npm WARN Local package.json exists, but node_modules missing, did you mean to install?

How comes npm install is actually and definitely executed, but npm complains that node_modules cannot be found?

like image 735
Jivan Avatar asked Apr 05 '19 17:04

Jivan


2 Answers

I had the same problem and I solved it just following this instruction. Add one line of code - /usr/src/app/node_modules to the docker-compose.yml file in the volumes:

volumes:
  - ${PWD-.}/name_of_your_app:/usr/src/app
  - /usr/src/app/node_modules
like image 80
Roman Malkevych Avatar answered Sep 18 '22 08:09

Roman Malkevych


Update: I just ended up using only ./app/src folder as a volume, instead of ./app.

This way, /app/node_modules is not overridden by the host's volume.

version: '3'

services:
    app:
        container_name: app
        restart: always
        build:
            context: ./app
            dockerfile: Dockerfile-dev
        ports:
            - "8080:8080"
        volumes:
            - ./app/src:/usr/src/app/src # <---- this
            - ./logs:/logs
like image 30
Jivan Avatar answered Sep 19 '22 08:09

Jivan