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.
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.
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.
One Solution collect form web for “Incrementally build Docker image hierarchy with Makefile”
- your docker images are named
- their respective docker files are named
- their corresponding build directories are
diadepends on files
dibdepends on file
./dib.dir/and the docker image
dicdepends on file
./dic.dir/and the docker image
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)) endef $(foreach d,$(DOCKERIMAGES),$(eval $(call DEPS_rule,$(d)))) .dib.tag: .dia.tag .dic.tag: .dib.tag clean: 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.