How to correct docker in makefile which requires at least 1 argument for remove all containers command
You need a second $
in your recipe:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
# ^
The single $
is expanded as a makefile variable when the makefile is parsed. It expands to blank. Make therefore passes docker container rm -f
to your shell. The second $
sign causes make to expand $$
to $
, and it will pass docker container rm $(docker ps -aq) -f
to bash, which I'm guessing is what you want.
Notice, if you put the shell
in there as @EricMd proposed, it will run a shell command, but that command will be run at Makefile read time, as opposed to the time that the recipe is executed. If the docker ps -aq
command is dependent on any other artifacts of your build it would not work.
Sounds like you don't have any containers in docker to remove. I sometimes use a different syntax for this scenario:
remove_all_containers:
docker container ls -aq | xargs --no-run-if-empty docker container rm -f
The xargs syntax will not run docker container rm
if there are no containers to delete.
According to the documentation, docker ps -a
should list all containers.
You obtained this message "docker container rm" requires at least 1 argument
certainly because you forgot to prepend the command at stake with Make's shell
builtin:
remove_all_containers:
docker container rm $(shell docker ps -aq) -f
Note also that the docker ps
admits a filtering feature: the online doc describes the various flavors of the corresponding -f
flag.
For example, below are three Bash alias examples that can be useful to (i) stop all containers, (ii) remove all stopped containers; and (iii) remove dangling images−that would be tagged as <none>
when doing docker images ls
:
alias docker-stop='docker stop $(docker ps -a -q)'
alias docker-clean='docker rm $(docker ps -a -q -f status=exited)'
alias docker-purge='docker rmi $(docker images -q -f dangling=true)'