获取docker-compose以构建使用父容器的容器

时间:2018-01-19 05:36:43

标签: docker docker-compose dockerfile

我有一个设置,我正在从一组shell脚本迁移到docker-compose。我想要构建的容器使用由自定义dockerfile创建的父容器。但是我看不出如何让docker-compose(重新)构建必需的父容器。

按照以下三个文件:

/ code / containers / parent / DockerFile

FROM centos:7  
RUN... (rest of file to create common stuff used by multiple child images)

/代码/容器/儿童酮/ Dockerfile

FROM parent
RUN...

/code/docker-compose.yml

version: '3.3'
services:
  my-service:
    image: child-one
    build:
      dockerfile: containers/child-one/Dockerfile
      context: .

正如预期的那样,它会失败:
服务'my-service'无法构建:找不到存储库父级:不存在或没有拉取访问权限

除了手动运行docker以首先构建父图像之外,找不到任何解决方案。

任何想法都非常感激。

编辑:基于VonCs的想法:

version: '3.3'
services:
  parent-notservice;
    image: parent
    build:
      dockerfile: containers/parent/Dockerfile
      context: .

  my-service:
    image: child-one
    depends_on:
      parent
    build:
      dockerfile: containers/child-one/Dockerfile
      context: .

但是我不得不使用depends_on,这是一个黑客,我担心父启动的影响(当孩子运行时)。这不是我的意图。

2 个答案:

答案 0 :(得分:4)

自 2021 年 1 月起,还有另一种方法可以使用 profiles 键执行此操作。 Docker Compose 1.28.0 中添加了对此键的支持。

version: '3.9'
services:
  parent-notservice:
    build:
      dockerfile: containers/parent/Dockerfile
      context: .
    profiles:
      - donotstart

  my-service:
    image: parent-notservice

  another-service:
    build:
      context: .
      dockerfile: ./containers/service/Dockerfile

my-service 将直接基于 parent-notservice. 然后您可以在 ports 文件中添加该容器所需的任何 volumesdocker-compose.yml

another-service 将从 Dockerfile 构建,Dockerfile 也可以使用 parent-notservice 命令基于 FROM。然后可以在该容器上安装不属于父容器的其他包。

FROM parent-notservice
RUN...

最好的部分是,当您使用 docker-compose up 时,parent-notservice 不会启动。

可以在此处找到有关 profiles 键的更多文档:https://docs.docker.com/compose/profiles/

答案 1 :(得分:1)

如果您的构建名称为“parent”(private static BigInteger function_f(int n) { // if n = 0 => f(n) = 0 if(n == 0) return new BigInteger("0"); // Initialization of variables // if n = 1 => f(n) = 1 (case included) BigInteger result = new BigInteger("1"); BigInteger last_fn = new BigInteger("0"); BigInteger before_last_fn = new BigInteger("0"); // Do the loop for n > 1 for (int i = 2; i <= n; i++) { // f(n - 2) before_last_fn = last_fn; // f(n - 1) last_fn = result; // f(n - 1) + f(n - 2) result = last_fn.add(before_last_fn); } // Return the result return result; }

,那么

“parent”将存在于您的本地docker注册表中

使用docker-compose文件,您需要首先构建docker build -t parentbuild directive下有parent/DockerFile

但是,您应该只构建意图运行的服务,而不是“父”的情况:在涉及docker-compose之前,应该构建父
正确的解决方案是使用包装脚本:

  • image: parent
  • 然后致电docker build -t parent ...

虚构服务之间不再有docker-compose黑客攻击(depends_on没有“服务”:没有任何东西在那里运行。)