Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 552
f5e7805f Avatar asked Jun 20 '16 23:06

f5e7805f


People also ask

What does depends on do docker-compose?

depends_on is a Docker Compose keyword to set the order in which services must start and stop. For example, suppose we want our web application, which we'll build as a web-app image, to start after our Postgres container.

Do containers share common dependencies?

Yes, answer is "true" to both questions. If you start 2 (or more) containers on the same host, all using the same base image, the whole content of the base image will be shared.

Why you shouldn't use docker-compose in production?

It's a cool tool to make handling container configuration or multiple interconnected containers a bit easier. The “don't use docker-compose in production” statement is motivated by hidden assumptions which are not necessarily valid for everybody, and propagated by unclear communication.

How do I handle multiple files in docker-compose?

To use multiple override files, or an override file with a different name, you can pass the -f option to the docker-compose up command. The base Compose file has to be passed on the first position of the command.


2 Answers

It's possible. There's a kind of workaround. You're close, but you were missing explicit image tags (so you had little ability on child images to declare which image you inherited from).

version: '3.2'
services:
  base:
    image: mybaseimage
    build: ./path-to-base-dockerfile
  child1:
    build: ./path-to-child1-dockerfile
    depends_on:
      - base
  child2:
    build: ./path-to-child2-dockerfile
    depends_on:
      - base

Let's say you have no images built. You run docker-compose up. The following things will happen:

  • docker-compose sees that child1 and child2 services depend on base. So it will deploy base first.
  • docker-compose sees that you have not yet tagged any image as mybaseimage. It knows how to build mybaseimage (you gave it a build path), so it will build it now, and tag it as mybaseimage.
  • docker-compose deploys the base service.
    • ideally you should design base so that it quits immediately, or has no entrypoint. since we don't actually want it to run this service.
  • docker-compose considers deploying child1 and child2
  • docker-compose sees that you have not yet tagged any image as child1. It knows how to build child1 (you gave it a build path), so it will build it now, and tag it as child1.
  • docker-compose deploys the child1 service
  • same sequence of steps for child2

The next docker-compose up will be simpler (we have tagged images available, so we skip all build steps).

If you already have tagged images, and want to rebuild: use docker-compose build to tell it to build all images (yes, base and children will both be rebuilt).

like image 134
Birchlabs Avatar answered Oct 19 '22 20:10

Birchlabs


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

like image 29
dnephin Avatar answered Oct 19 '22 18:10

dnephin