.PHONY: all clean images .DEFAULT_GOAL := all # Boiler plate for bulding Docker containers. # All this must go at top of file I'm afraid. IMAGE_PREFIX := quay.io/weaveworks/build- IMAGE_TAG := $(shell ../image-tag) GIT_REVISION := $(shell git rev-parse HEAD) UPTODATE := .uptodate # Every directory with a Dockerfile in it builds an image called # $(IMAGE_PREFIX). Dependencies (i.e. things that go in the image) # still need to be explicitly declared. %/$(UPTODATE): %/Dockerfile %/* $(SUDO) docker build --build-arg=revision=$(GIT_REVISION) -t $(IMAGE_PREFIX)$(shell basename $(@D)) $(@D)/ $(SUDO) docker tag $(IMAGE_PREFIX)$(shell basename $(@D)) $(IMAGE_PREFIX)$(shell basename $(@D)):$(IMAGE_TAG) touch $@ # Get a list of directories containing Dockerfiles DOCKERFILES := $(shell find . -name tools -prune -o -name vendor -prune -o -type f -name 'Dockerfile' -print) UPTODATE_FILES := $(patsubst %/Dockerfile,%/$(UPTODATE),$(DOCKERFILES)) DOCKER_IMAGE_DIRS := $(patsubst %/Dockerfile,%,$(DOCKERFILES)) IMAGE_NAMES := $(foreach dir,$(DOCKER_IMAGE_DIRS),$(patsubst %,$(IMAGE_PREFIX)%,$(shell basename $(dir)))) images: $(info $(IMAGE_NAMES)) @echo > /dev/null # Define imagetag-golang, etc, for each image, which parses the dockerfile and # prints an image tag. For example: # FROM golang:1.8.1-stretch # in the "foo/Dockerfile" becomes: # $ make imagetag-foo # 1.8.1-stretch define imagetag_dep .PHONY: imagetag-$(1) $(patsubst $(IMAGE_PREFIX)%,imagetag-%,$(1)): $(patsubst $(IMAGE_PREFIX)%,%,$(1))/Dockerfile @cat $$< | grep "^FROM " | head -n1 | sed 's/FROM \(.*\):\(.*\)/\2/' endef $(foreach image, $(IMAGE_NAMES), $(eval $(call imagetag_dep, $(image)))) all: $(UPTODATE_FILES) clean: $(SUDO) docker rmi $(IMAGE_NAMES) >/dev/null 2>&1 || true rm -rf $(UPTODATE_FILES)