Makefile No Such File or Directory Error – How to Fix

make

I am trying to create a Makefile target which first checks to see if my Docker container exists. If it exists then I want to issue the Docker Container Restart command otherwise I want to issue the Docker Run command to create and start the container.

I have coded the below but am getting the error shown below. The result = 1 is correct as I do have a container running. I have removed the container and tested that the result then becomes 0 which is also correct. The problem seems to be when I try to use result in the ifeq statement. Can someone please advise me what I am doing wrong? (I have temporarily commented out the docker commands and replaced them with echo true / false just while I am debugging).

start_docker_myapp:
    result = $(shell (docker ps -a | grep myapp ) | wc -l )
    ifeq (${result}, 1)
        @echo 'true'
# docker restart ${IMAGE}
    else
        @echo 'false'
# docker run -v ${DIR}/var/log/docker:/var/log/myapp -p 1812:1812/udp  -p 1813:1813/udp --detach  --name ${IMAGE} $(REGISTRY)/$(IMAGE):$(TAG)
    endif

Output from Terminal

$ make start_docker_myapp
result =        1
make: result: No such file or directory
make: *** [start_docker_myapp] Error 1
$

Best Answer

There are a number of issues with your Makefile (beyond the question of whether a Makefile is the appropriate solution):

  • conditional directives aren’t part of a recipe, so they mustn’t start with a tab;
  • conditional directives are evaluated as the Makefile is read, so variables must be assigned previously and can’t be target-specific;
  • docker ps -a returns information on all known containers, including non-running containers;
  • phony targets should be declared as such.

The following works:

result = $(shell docker ps -f name=myapp -q | wc -l)
start_docker_myapp:
ifeq ($(strip $(result)),1)
    @echo true
else
    @echo false
endif

.PHONY: start_docker_myapp
Related Question