If statement inside of Makefile to detect docker image exist and if so delete it

Viewed 797

I have a simple if statement inside of Makefile to say that delete docker image if it exists but it doesn't work. Notice in the log file image exists but when I do make clean if statement fails.

NAME=program1

all: .docker-build
    docker run ${NAME} make all

.docker-build:
    docker build . -t ${NAME}
    @echo "" > .docker-build

run-%: .docker-build
    docker run ${NAME} make $*

clean:
ifeq ($(docker images -q ${NAME} 2> /dev/null), "")
    docker image rm -f ${NAME}
endif
    @rm -f .docker-build

Log (notice image exist but if statement inside of make clean fails):

āžœ  dockerfile-test docker images -q program1 2> /dev/null
5ee4797b91ad
āžœ  dockerfile-test make clean                            
rm -f .docker-build
1 Answers

This:

ifeq ($(docker images -q ${NAME} 2> /dev/null), "")

expands the make variable named docker images -q program1 2> /dev/null, which doesn't exist and so expands to the empty string, then it compares it to the two-character string "" and it never matches.

You probably meant:

ifeq ($(shell docker images -q ${NAME} 2> /dev/null),)
Related