1

Basically I have a main directory and Books Directory (General file structure, there's more but these are the important pieces). So when I fire a request from main to booksServer, it doesn't work because the node modules are missing.

That's because the node modules are inside the docker container at a specific path: '/usr/src/app'

How can I have main.js see that books (service/container) does have the proper node packages inside this specific path?

I think I can use docker-compose, but I wanted to test it individually without docker-compose first.

**-Main Directory (Individual Service, has its own container)**
  -Initiator (Fires commands) 
  -DockerFile

**-Books Directory (Individual Service, has its own container)**
  -Stubs
     -BooksStub.js (NEED THIS!, but it won't work because needs npm modules which is located in its container @/usr/src/app. How can I access the nodemodules that it's using?)

  -booksServer.js
  -Package*.json (lock and package.json)
  -DockerFile

Inside the

Error:

internal/modules/cjs/loader.js:800
    throw err;
    ^

Error: Cannot find module 'grpc'

Books Dockerfile

FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
EXPOSE 30043
CMD ["node", "booksServer.js"]

Main DockerFile

FROM node:12.14.0
WORKDIR /usr/src/app
COPY package*.json ./
COPY . /usr/src/app
RUN npm install
EXPOSE 4555
CMD ["node", "main.js"]
TheRoadLessTaken
  • 531
  • 2
  • 7
  • 15
  • That looks like you should (correctly) have two separate images, with two separate `node_modules` trees. Are you missing the `grpc` dependency in `main/package.json`? – David Maze Jul 30 '20 at 16:45
  • No. This is because in main, it requires a file from the books directory. So that's why there needs to be a shared data volume of some sort. (still a bit unsure as to how to do this.) – TheRoadLessTaken Jul 30 '20 at 17:43
  • What's the specific file you need? Where is it in your filesystem layout? Can you `COPY` it into the `main` image, so that image is self-contained? – David Maze Jul 30 '20 at 18:53
  • -Books Directory -Stubs -BooksStub.js || Main Directory -Initiator -> The initiator needs access to the BooksStub.js file. But the BooksStub.js file won't work because the node_module is inside the Books Container. This is a microservice architecture so the idea was that each service (main service vs. book service) has their own dependencies (package*.json) – TheRoadLessTaken Jul 30 '20 at 19:52
  • Just edited the file directory on the post as well. – TheRoadLessTaken Jul 30 '20 at 19:57
  • I keep getting this error, probably something involving async gRPC calls. Error: undefined:1 undefined ^ SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse () at ClientUnaryCall. (/usr/src/app/main.js:119:8) at ClientUnaryCall.emit (events.js:210:5) at Object.onReceiveMetadata (/usr/src/app/node_modules/grpc/src/client_interceptors.js:1202:15) at InterceptingListener._callNext (/usr/src/app/node_modules/grpc/src/client_interceptors.js:568:42) at InterceptingListener.onReceiveMetadata – TheRoadLessTaken Jul 31 '20 at 15:48

1 Answers1

2

You can create one common datavolume and attached your containers with the datavolume

Here is the step to create a datavolume,

Step 1 : docker volume create --name storageOne You can give any name instead of storageOne

Step 2 : Now you need to attach that volume with the container using docker run -ti --name=myContainer -v storageOne:/storageOne ubuntu command

Step 3 : Copy or create your required file in that datavolume

Step 4 : Now Create an another Container using docker run -ti --name=myContainer2 --volumes-from MyContainer ubuntu command

Step 5 : Restart your myStorage container

So whatever files are available in myStorage will be shareable between attached container.

May be this will help you

Bhargav Tailor
  • 442
  • 4
  • 8
  • There are big problems using volumes for code like this. Of note, say the file in the `books` container changes in the underlying code base. Since volumes are supposed to contain user data, Docker won't update the volume, so the `main` container will have an out-of-date copy of the file; not only that, but the stale volume code will probably also hide what's in the image in the `books` containers. I would not recommend volumes here. – David Maze Jul 30 '20 at 18:52