【问题标题】:Can docker compose build image from different Dockerfiles at the same folderdocker可以从同一文件夹中的不同Dockerfiles构建构建图像吗
【发布时间】:2015-06-13 01:31:52
【问题描述】:

我的容器应该共享同一个源代码文件夹。

但它们应该从不同的 Dockerfile 构建。

是否可以使用 docker-compose 构建它们并一次从不同的 Dockerfile 构建?

我希望有像

这样的语法

build: . -f <<Dockerfile_ABC>build: . -f <<Dockerfile_CDE>

abc:
  build: . -f <<Dockerfile_ABC>
  environment:
    - PROJECT=abc
  command: ruby abc/abc.rb
  volumes:
      - ./:/working_direcotry
  ports:
    - 5904:5904

cde:
  build: . -f <Dockerfile_CDE>
  environment:
    - PROJECT=cde
  command: ruby cde/cde.rb
  volumes:
      - ./:/working_direcotry
  ports:
    - 5902:5902

【问题讨论】:

  • 您应该可以直接向 docker github 提出拉取请求以获取您的想法
  • 这可能是stackoverflow.com/questions/29835905/… 的重复。起初我误解了这个问题(忽略了 docker-compose 部分)并写了一个答案。我现在删除了那个答案,而是发布了这个评论哈哈。

标签: docker dockerfile docker-compose


【解决方案1】:

以下对我有用

abc:
  build:
    context: .
    dockerfile: Dockerfile.one
def:
  build:
    context: .
    dockerfile: Dockerfile.two

当然,如果您有不同的上下文,您可以根据需要调整上下文。我还使用它来拥有单独的 Dockerfile.production 版本,以便为应用程序的生产版本进行不同的设置。

【讨论】:

  • 一个小技巧:在vsCode中,最好使用one.dockerfile而不是Dockerfile.one
【解决方案2】:

只需指定 Dockerfile 的名称:

abc:
    build: path/to/Dockerfile_for_abc

【讨论】:

  • 我猜作者希望所有服务都使用相同的构建上下文。一旦 Dockerfile 在不同的路径中,构建上下文也会更改为该路径。
  • Answer 对您指定的内容感到困惑,它不是“Dockerfile 的名称”,它是一个完全不同的文件夹,将其指定为另一个 docerfile 的完整路径将导致错误:failed to read dockerfile: error from sender: ..../Dockerfile_for_abc: not a directory
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2015-06-21
相关资源
最近更新 更多