user1315621
user1315621

Reputation: 3372

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:

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

Upvotes: 0

Views: 187

Answers (1)

MadScientist
MadScientist

Reputation: 100816

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.

Upvotes: 1

Related Questions