Search code examples
dockerdocker-composedockerfile

Building common dependencies with docker-compose


Assuming I have a set of images which depend on a common base image:

  • base (this is only a set of common dependencies)

    FROM ubuntu:16.04
    
    ENV FOO 1
    
  • child1

    FROM mybaseimage  # where mybaseimage corresponds to base
    
    CMD ["bar1_command"]
    
  • child2

    FROM mybaseimage  # where mybaseimage corresponds to base
    
    CMD ["bar2_command"]
    

Is it possible to create docker-compose file which would build base without running it? Lets say I have following dependencies:

version: '2'
services:
    child1:
        build: ./path-to-child1-dockerfile
services:
    child2:
        build: ./path-to-child2-dockerfile
    depends_on:
        - child1

I would like base to be build even if it is not explicitly started. Is something like this even possible? Or should I simply use external Makefile to build dependencies?

build_base:
    docker build -t mybaseimage mybaseimage  

build_all: build_base
    docker-compose build

Solution

  • Use a Makefile. docker-compose is not designed to build chains of images, it's designed for running containers.

    You might also be interested in dobi which is a build-automation tool (like make) designed to work with docker images and containers.

    Disclaimer: I'm the author of dobi