Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correct docker in makefile which requires at least 1 argument for remove all containers command

The docker command "docker container rm $(docker ps -aq) -f" works fine from the command line. However, when I try to run it from a makefile using the following target ("remove_all_containers")...

remove_all_containers:
       docker container rm $(docker ps -aq) -f

I get the error message:

host_name$ make remove_all_containers
docker container rm  -f
"docker container rm" requires at least 1 argument.
See 'docker container rm --help'.

Usage:  docker container rm [OPTIONS] CONTAINER [CONTAINER...]

Remove one or more containers
make: *** [remove_all_containers] Error 1

Clearly, when executed from within the makefile, the "docker ps" command is not being properly being properly executed in a way where its results can be collected and passed into the "container rm" command.

My Question: How do I get the "docker ps" command to run correctly from within the makefile and pass its results correctly into the "docker rm" command, also within the makefile?

Thanks, in advance, for any assistance you can offer.

like image 263
Information Technology Avatar asked Jan 21 '19 23:01

Information Technology


People also ask

How do I fix the docker build requires exactly one argument?

The most common reason for “Docker build Requires 1 Argument” error is when we try to build the image without providing sufficient arguments. Here, in the arguments, we need to provide the directory with the command. In order to solve this issue, we need to provide the correct file name with -f option.

How do I remove all running containers in docker?

Use the docker container prune command to remove all stopped containers, or refer to the docker system prune command to remove unused containers in addition to other Docker resources, such as (unused) images and networks.

How do I force remove all containers?

Stop and remove all containers The command docker container ls -aq generates a list of all containers. Once all containers are stopped, remove them using the docker container rm command, followed by the containers ID list.


1 Answers

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.

like image 158
HardcoreHenry Avatar answered Sep 17 '22 12:09

HardcoreHenry