Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Makefile variable value from command

I have a Docker container running and I would like to kill it using make kill.

Here’s my Makefile:

kill:
    CONTAINER=$(docker ps -a -q  --filter ancestor=container-name); \
    docker kill $$CONTAINER

It gives error:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

CONTAINER=; \
docker kill $CONTAINER
"docker kill" requires at least 1 argument.
See 'docker kill --help'.

It seems that the variable CONTAINER is empty.
However running in the shell:

$(docker ps -a -q  --filter ancestor=container-name)

Returns the container id, in fact it prints:

c1cddc4d19a0: command not found

>Solution :

I assume you have not defined a make variable named docker ps -a -q --filter ancestor=container-name, and you instead want to run that as a program and obtain its output.

If so, you need to escape the $ here like you did for the variable:

kill:
        CONTAINER=$$(docker ps -a -q  --filter ancestor=container-name); \
        docker kill $$CONTAINER

Otherwise make thinks that $(docker ps -a -q --filter ancestor=container-name) is a reference to a non-existent variable and will substitute the empty string.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading