Skip to content

Instantly share code, notes, and snippets.

@stephenc
stephenc / Makefile
Created October 9, 2018 10:44
How to have a makefile only rebuild a docker image when needed
DOCKER_TAG = ...
.PHONY: build-image touch.build-image
build-image: touch.build-image .build-image
touch.build-image:
$(eval timestamp=$(shell docker inspect -f '{{ range $$i, $$e := split .Metadata.LastTagTime "T" }}{{if eq $$i 0}}{{range $$j, $$v := split $$e "-"}}{{$$v}}{{end}}{{else}}{{$$f := printf "%.8s" $$e}}{{range $$j, $$g := split $$f ":"}}{{if lt $$j 2}}{{$$g}}{{else}}.{{$$g}}{{end}}{{end}}{{end}}{{end}}' $(DOCKER_TAG) 2>/dev/null ))
@if [ "A$(timestamp)A" = "AA" ] ; then rm -f .build-image ; else touch -t $(timestamp) .build-image ; fi
.build-image: Dockerfile rootfs/*