Docker:如何从给定容器访问另一个容器中的文件? [英] Docker: How to access files from another container from a given container?

查看:165
本文介绍了Docker:如何从给定容器访问另一个容器中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我有一个主目录和书籍目录(一般文件结构,还有更多,但这些是重要的部分).因此,当我从 main 向 booksServer 发出请求时,它不起作用,因为缺少节点模块.

这是因为节点模块位于 docker 容器中的特定路径:'/usr/src/app'

我如何让 main.js 看到书籍(服务/容器)在此特定路径中确实有正确的节点包?

我认为我可以使用 docker-compose,但我想先单独测试它而不先使用 docker-compose.

**-主目录(个别服务,有自己的容器)**-Initiator(触发命令)-Docker文件**-图书目录(个人服务,有自己的容器)**-存根-BooksStub.js(需要这个!,但它不起作用,因为需要位于其容器 @/usr/src/app 中的 npm 模块.如何访问它正在使用的节点模块?)-booksServer.js-Package*.json(锁和 package.json)-Docker文件

里面

错误:

内部/模块/cjs/loader.js:800抛出错误;^错误:找不到模块grpc"

书籍 Dockerfile

FROM 节点:12.14.0工作目录/usr/src/app复制包*.json ./复制 ./usr/src/app运行 npm 安装暴露 30043CMD [节点",booksServer.js"]

主 DockerFile

FROM 节点:12.14.0工作目录/usr/src/app复制包*.json ./复制 ./usr/src/app运行 npm 安装暴露 4555CMD [节点",main.js"]

解决方案

您可以创建一个通用的 datavolume 并使用 datavolume

附加您的容器

这是创建数据卷的步骤,

第 1 步: docker volume create --name storageOne 您可以使用任何名称代替 storageOne

第 2 步:现在您需要使用 docker run -ti --name=myContainer -v storageOne:/storageOne ubuntu 命令将该卷附加到容器中p>

第 3 步:在该数据卷中复制或创建您需要的文件

第 4 步: 现在使用 docker run -ti --name=myContainer2 --volumes-from MyContainer ubuntu 命令创建另一个容器

第 5 步:重新启动您的 myStorage 容器

因此 myStorage 中可用的任何文件都可以在附加容器之间共享.

也许这会对你有所帮助

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

解决方案

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

这篇关于Docker:如何从给定容器访问另一个容器中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆