Duckman
Duckman

Reputation: 85

Getting docker-compose to build container that uses a parent container

I have a setup that I am migrating to docker-compose from a set of shell scripts. The container I want to build uses a parent container that is created by a custom dockerfile. But I can't see how to get docker-compose to (re)build the requisite parent container.

There are three files as per:

/code/containers/parent/DockerFile

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

/code/containers/child-one/Dockerfile

FROM parent
RUN...

/code/docker-compose.yml

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

As expected it will fail with:
Service 'my-service' failed to build: repository parent not found: does not exist or no pull access

Can't find any solution to this other than manually running docker to build the parent image first.

Any ideas much appreciated.

edit: base on VonCs idea:

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: .

However I had to use depends_on, which was a hack, I am worried about effects of the parent starting (when child is run). This is not my intent.

Upvotes: 3

Views: 3129

Answers (2)

mcarson
mcarson

Reputation: 3352

As of January 2021, there is another way to do this by using the profiles key. Support for this key was added in 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 will be based directly on parent-notservice. Then you can add whatever ports and volumes are needed for that container in the docker-compose.yml file.

another-service will be built from the Dockerfile which could also be based on parent-notservice using the FROM command. Other packages can then be installed on that container that are not part of the parent.

FROM parent-notservice
RUN...

And the best part is that when you use docker-compose up the parent-notservice will not start.

Further documentation on the profiles key can be found here: https://docs.docker.com/compose/profiles/

Upvotes: 6

VonC
VonC

Reputation: 1324248

"parent" would exist in your local docker registry if your build was setting the name "parent" (docker build -t parent)

With a docker-compose file, you need to build parent/DockerFile first, with an image: parent under your build directive.

However, you should only build service you intent to run, which is not the case for "parent": parent should be built before docker-compose is involved.
The proper solution is to have a wrapper script which would:

  • docker build -t parent ...
  • then call docker-compose

No more depends_on hack between imaginary services (there is no "service" for parent: nothing runs there).

Upvotes: 3

Related Questions