Docker: How to access files from another container from a given container?

冷暖自知 提交于 2020-08-20 11:29:26

问题


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"]

回答1:


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



来源:https://stackoverflow.com/questions/63177219/docker-how-to-access-files-from-another-container-from-a-given-container

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!