Home > Back-end >  Why call from makefile returns empty result while same call from console does not?
Why call from makefile returns empty result while same call from console does not?

Time:11-03

I want to do shortcut with Makefile to delete docker image after i rebuild one of the services with docker-compose up --build, because dangling images are created with every rebuild.

My solution get image_id of stoped container and docker rmi it. As part if this a have to obtain image id, which i do like this:

export CONTAINER=name_1
docker container inspect --format='{{.Image}}' $CONTAINER
# > sha256:xxxxxxxxxxxxxxxxxxxxxx

But i get empty ouput from it if i call with makefile:

./Makefile

CONTAINER=name_1
cmd:
    echo $(docker container inspect --format='{{.Image}}' $CONTAINER)
cmd
# > echo 

I do not understard what is the problem here.

CodePudding user response:

This:

echo $(docker container inspect --format='{{.Image}}' $CONTAINER)

does not run docker. $ is special to make and it introduces a make variable, so you are expanding a very oddly-named (and empty) variable. You have to escape all $ as $$ in your recipe to hide it from make.

Also, makefile variable references that are >1 character long must be enclosed in either () or {} (they are equivalent):

echo $$(docker container inspect --format='{{.Image}}' ${CONTAINER})
  • Related