一个容器可以使用另一个容器的文件吗?

时间:2020-07-30 23:33:55

标签: docker docker-compose dockerfile docker-registry docker-volume

基本上,我有一个主目录和“书籍目录”(常规文件结构,还有更多,但这些都是重要的部分)。因此,当我从main向booksServer发出请求时,由于缺少节点模块,所以该请求不起作用。

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

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

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

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

1 个答案:

答案 0 :(得分:0)

使用卷可以将文件从一个容器共享到另一个容器。

  • 创建一个卷以在两个Docker容器之间共享:

    docker volume create --name volume_name

  • 首先创建带有容器的容器

    docker run -it --name = container_first -v volume_name:/ volume image_name

  • 创建container_second,它将与container_first共享文件

    docker run -it --name = container_second --volumes-from container_first image_name