Skip to content

Instantly share code, notes, and snippets.

@stephenc
Created October 9, 2018 10:44
Show Gist options
  • Save stephenc/550de8d71c2742504764ae94515f035a to your computer and use it in GitHub Desktop.
Save stephenc/550de8d71c2742504764ae94515f035a to your computer and use it in GitHub Desktop.
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/*
docker build --tag $(DOCKER_TAG):latest .
@touch .build-image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment