Incrementally build Docker image hierarchy with Makefile

In a project of mine I have several Docker images that base on each others, because I don’t need to rebuild basic parts of it every time and just want to incrementally build the upper layers of the hierarchy.

I build the images by means of a Makefile. I do this because I’m admittedly lazy and don’t want to enter the whole docker build commands manually every time. And also because I want to avoid making mistakes in the process.

  • Nginx load balancer upstream SSL and Docker
  • How to divert traffic from a docker container's ethernet interface to a bridge?
  • Connecting to a mysql running on a Docker container ubuntu 16.04
  • Firewall rules for Docker Hub access
  • Docker Network Nginx Resolver
  • run python-ldap with official python docker image as base
  • What I would like to do now is make make able to detect whether a Docker image has to be rebuild instead of Docker itself because for the latter the Docker build context has to be sent to the Docker daemon every time. The image is quite large because it is based on ubuntu:xenial and so the “Sending build context to Docker daemon” always takes a lot of time for each individual Docker image, independently of whether they need to be rebuilt or not.

    If make was able to detect whether the docker build command needs to be executed that would help a lot to speed up the process.

    So I would like to know if there’s a way for make to detect if a specific Docker image needs to be rebuilt.

  • Prometheus with multi-container pod on kubernetes
  • ceph docker starting OSD Failed to start Ceph disk activation: /dev/sdb1
  • Building a generic Camel/Docker image and apply different Camel routes when container starts
  • Why does docker not run with kernels older than 3.10?
  • docker-compose build leads to client and server don't have same version (client : 1.18, server: 1.17)
  • Dockerfile expose port for node.js image
  • One Solution collect form web for “Incrementally build Docker image hierarchy with Makefile”


    • your docker images are named dia, dib, dic
    • their respective docker files are named dockerfile.dia, dockerfile.dib, dockerfile.dic
    • their corresponding build directories are ./dia.dir/, ./dib.dir/, ./dic.dir/
    • dia depends on files foo, bar in ./dia.dir/
    • dib depends on file cuz in ./dib.dir/ and the docker image dia
    • dic depends on file baz in ./dic.dir/ and the docker image dib

    you can try something like (not tested):

    DOCKERIMAGES := dia dib dic
    TAGS := $(patsubst %,.%.tag,$(DOCKERIMAGES))
    diaDEPS := foo bar
    dibDEPS := cuz
    dicDEPS := baz
    all: $(TAGS)
    $(TAGS): .%.tag: dockerfile.%
        docker build -f $< -t $* $*.dir && \
        echo '$* image built' && \
        touch $@
    define DEPS_rule
    .$(1).tag: $$(addprefix $(1).dir/,$$($(1)DEPS))
    $(foreach d,$(DOCKERIMAGES),$(eval $(call DEPS_rule,$(d))))
    .dib.tag: .dia.tag
    .dic.tag: .dib.tag
        rm -f $(TAGS)

    It should rebuild the docker images only if their docker file changed, or the files they depend on in their directories changed, or the docker image they depend on, changed.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.