0

I am using docker-compose to install and start the app. But here node_modules are not available to application to run it. I have checked this Docker-compose: node_modules not present in a volume after npm install succeeds but it did not help me.

Here is my docker-compose.yml

version: "2"

services:
    neo4j:
        container_name: ms_neo4j
        image: neo4j:latest
        environment:
            - NEO4J_AUTH=none
        ports:
            - "7474:7474"
            - "7687:7687"

    mongo:
        container_name: ms_mongo
        image: mongo:latest
        ports:
            - "27017:27017"
        volumes:
            - "./mongo/db:/data/db"

    nginx:
        build: ./nginx
        container_name: ms_nginx
        links:
            - petstore
            - users
        ports:
            - "80:80"

    petstore:
        build: ./petstore
        container_name: ms_petstore
        environment:
            - loglevel=none
        links:
            - "mongo:mongo"
        volumes:
            - "./petstore:/src/app"
            - node_modules:/petstore/node_modules
        working_dir: "/src/app"
        ports:
            - "8080:8080"
            - "5858:5858"
        # command: npm run start
        command: npm run start:dev

    users:
        build: ./users
        container_name: ms_users
        links:
            - neo4j
        volumes:
            - "./users:/src/app"
            - node_modules:/users/node_modules
        working_dir: "/src/app"
        command: npm start

volumes:
    node_modules:

users/Dockerfile

FROM node:latest

RUN npm i -g rimraf typescript

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/package.json
RUN npm install

COPY . /usr/src/app

petstore/Dockerfile

FROM node:latest

RUN npm i -g nodemon node-inspector@0.7.5 npm-run-all rimraf typescript

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY package.json /usr/src/app/package.json
RUN npm install

COPY . /usr/src/app

Getting these errors

src/config/Application.ts(4,25): error TS2307: Cannot find module 'config' or its corresponding type declarations.

src/config/Mongo.ts(1,33): error TS2307: Cannot find module 'mongodb' or its corresponding type declarations.

src/config/Mongo.ts(2,25): error TS2307: Cannot find module 'config' or its corresponding type declarations.

src/config/Mongo.ts(3,25): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/services/PetSwagger.ts(2,25): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/services/PetSwagger.ts(3,25): error TS2307: Cannot find module 'config' or its corresponding type declarations.

src/services/MongoService.ts(1,33): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/services/PetService.ts(1,33): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/controllers/PetStoreController.ts(1,40): error TS2307: Cannot find module 'routing-controllers' or its corresponding type declarations.

src/controllers/PetStoreController.ts(2,24): error TS2307: Cannot find module 'typedi' or its corresponding type declarations.

src/controllers/PetStoreController.ts(3,40): error TS2307: Cannot find module 'mongodb' or its corresponding type declarations.

src/subscribers/PetSubscriber.ts(1,37): error TS2307: Cannot find module 'event-dispatch' or its corresponding type declarations.

src/tasks/swagger.ts(2,21): error TS2307: Cannot find module 'fs' or its corresponding type declarations.

src/tasks/swagger.ts(3,23): error TS2307: Cannot find module 'glob' or its corresponding type declarations.

src/tasks/swagger.ts(7,38): error TS2580: Cannot find name 'require'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`.

enter image description here

Does anyone know how to resolve it ?

Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • The second part of `node_modules:/users/node_modules` is the target. so it means that u are actually mounting a volume to `/users/node_modules` in the docker compose. But u want to mount to `/src/app/node_modules`. – Aritra Chakraborty Jul 07 '21 at 07:00
  • Can you pls suggest what changes I need to make ? I am getting this error in both apps. petstore and users – Ajay S Jul 07 '21 at 07:04
  • can you provide the file structure? – Dory Nguyen Jul 07 '21 at 07:56
  • @DoryNguyen I added file structure snapshot in the question. Pls check – Ajay S Jul 07 '21 at 08:29
  • That's strange, you node modules are outside of user and petstores, but you're running npm install inside, so node modules are currently inside these folders. – Dory Nguyen Jul 07 '21 at 08:48
  • In addition, you're mounting ./petstore to /src/app but the working directory is /usr/src/app – Dory Nguyen Jul 07 '21 at 08:50
  • Can you delete all of the `volumes:` blocks being discussed here? That would use the code and `node_modules` trees that are built into the image. – David Maze Jul 07 '21 at 10:49
  • I updated the path but still is not working. I removed volumes: block also. Here is the updated docker-compose.yml https://pastebin.com/Xqsm9jdG – Ajay S Jul 07 '21 at 14:19

1 Answers1

0

I've noticed a few things related to paths in your files that look fishy:

In your docker-compose file you are using /src/app as your workdir, but in your Dockerfiles the workdir where the files are copied and the npm install is run, you are using /usr/src/app

Fix: use the same workdir in both files by omitting the working_dir entirely from you docker-compose file and fixing the paths in your docker-compose file. By using the WORKDIR command, your Dockerfiles can be simplified to

FROM node:latest

RUN npm i -g rimraf typescript

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# notice how I am omitting the /usr/src/app since you already have declared it as the workdir and just copying into the current directory `.`
COPY package.json .
RUN npm install
COPY . .

Your docker-compose file should reflect these path changes like this:

    petstore:
        build: ./petstore
        container_name: ms_petstore
        environment:
            - loglevel=none
        links:
            - "mongo:mongo"
        volumes:
            - "./petstore:/usr/src/app"
            - ./node_modules:/usr/src/app/node_modules
        ports:
            - "8080:8080"
            - "5858:5858"
        # command: npm run start
        command: npm run start:dev
Taxel
  • 3,859
  • 1
  • 18
  • 40
  • Do you think the docker-compose file is putting `note_modules` and `app` into two separate volumes? the docker file structure shows node_modules is outside 2 apps, so for node_modules to be accessible for both of them, 2 destinations should be mounted to one volume. – Dory Nguyen Jul 07 '21 at 09:33
  • I have not tried it but nested bind mounts seem to be possible (although having them indicates bad container design practice) [source](https://stackoverflow.com/a/65664470/3083470) and I think bind mounting the same directory twice should also work. But yes, I would not create a shared `node_modules` dir but instead remove the volume for it entirely and have the `node_modules` in the respective directories with their `package.json` – Taxel Jul 07 '21 at 10:58
  • I am getting same error still. ms_users | src/common/logging.ts:1:26 - error TS2307: Cannot find module 'winston' or its corresponding type declarations. – Ajay S Jul 07 '21 at 14:17