Docker:如何从给定容器中的另一个容器访问文件? [英] Docker: How to access files from another container from a given container?
问题描述
基本上,我有一个主目录和图书目录(常规文件结构,还有更多,但这些都是重要的部分)。因此,当我从main到booksServer发出请求时,由于缺少节点模块而无法使用。
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.
这是因为节点模块位于特定路径的docker容器内:'/ usr / src / app'
That's because the node modules are inside the docker container at a specific path: '/usr/src/app'
如何让main.js看到书籍(服务/容器)在此特定路径中确实具有正确的节点包?
How can I have main.js see that books (service/container) does have the proper node packages inside this specific path?
我想我可以使用docker-compose,但我想先进行测试,而无需先进行docker-compose。
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
内部
错误:
internal/modules/cjs/loader.js:800
throw err;
^
Error: Cannot find module 'grpc'
书籍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"]
主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"]
推荐答案
您可以创建一个常见的 datavolume
并将容器附加到数据量
You can create one common datavolume
and attached your containers with the datavolume
这是创建数据量的步骤,
Here is the step to create a datavolume,
步骤1: docker volume create --name storageOne
您可以使用任何名称代替 storageOne
Step 1 : docker volume create --name storageOne
You can give any name instead of storageOne
第2步:现在,您需要使用 docker run -ti --name = myContainer -v storageOne:/ storageOne将卷与容器连接ubuntu
命令
Step 2 : Now you need to attach that volume with the container using docker run -ti --name=myContainer -v storageOne:/storageOne ubuntu
command
步骤3:在该数据量中复制或创建所需文件
Step 3 : Copy or create your required file in that datavolume
第4步:现在使用 docker run -ti --name = myContainer2 --volumes-来自MyContainer ubuntu
命令创建另一个容器
Step 4 : Now Create an another Container using docker run -ti --name=myContainer2 --volumes-from MyContainer ubuntu
command
第5步:重新启动 myStorage
容器
所以 myStorage
中可用的任何文件都可以在连接的容器之间共享。
So whatever files are available in myStorage
will be shareable between attached container.
可能是这个会帮助您
这篇关于Docker:如何从给定容器中的另一个容器访问文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!