I am trying to execute the next Makefile, but when I run the docker kill it fails because don't detect the "$(sudo docker ps -q)" or it does not execute this part.
I have the next Makefile:
.PHONY: kill all services farr-api farr-ingest farr-from-on-premise farr-real-time-processing
farr-api:
cd apis/1api && sudo docker-compose up -d
farr-ingest:
cd apis/2ingest && sudo docker-compose up -d
farr-from-on-premise:
cd apis/3onpremise && sudo docker-compose up -d
farr-real-time-processing:
cd apis/4realtimeprocessing && sudo docker-compose up -d
services:
cd services && sudo docker-compose up -d
all: services farr-from-on-premise farr-real-time-processing farr-ingest farr-api
kill:
sudo docker kill $(sudo docker ps -q)
When I run make kill it throws the next error:
sudo docker kill
"docker kill" requires at least 1 argument(s).
See 'docker kill --help'.
Usage: docker kill [OPTIONS] CONTAINER [CONTAINER...]
Kill one or more running containers
make: *** [kill] Error 1
It looks like "$" does not detect by Makefile.
But if I run manually sudo docker kill $(sudo docker ps -q) it works fine.
The target should look like this:
kill:
for c in $$(sudo docker ps -q); do sudo docker kill $$c; done
UPDATE:
turns out that docker kill works with multiple containers as arguments, so just escaping dollar is enough to kill all containers
kill:
sudo docker kill $$(sudo docker ps -q)
Related
I am running
docker run --rm -t -p 3000:3000 --name node-app foo/node
How do i quit ctrl + C does not seem to work.
Currently I have to open another terminal to stop the docker. Is this the only option?
If you use -t and -i then Control-C will terminate the container. When using -i with -t then you have to use Control-P Control-Q to detach without terminating
Test 1:
$ ID=$(sudo docker run -t -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps
The container is still listed.
Test 2:
$ ID=$(sudo docker run -t -i -d ubuntu /usr/bin/top -b)
$ sudo docker attach $ID
Control-C
$ sudo docker ps
The container is terminated
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.
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)'
I tested for 2 way follow bellow answer:
remove_all_containers:
docker container rm $$(docker ps -aq) -f
remove_all_containers:
docker container rm $(shell docker ps -aq) -f
I'm reading a book on docker. It is a couple of years old.
I'll cite:
If you want to get rid of all your stopped containers, you can use
the output of docker ps -aq -f status=exited , which gets the
IDs of all stopped containers. For example:
$ docker rm -v $(docker ps -aq -f status=exited)
When I run this, I get:
michael#michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D: dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Could you help me understand what should I do to gain what is intended.
In order to remove all our stopped containers, you can first run
$ docker ps -a
This gives you the list of running and stopped containers, from which you can select what are the containers that you wanted to get rid. But if you want to get rid of all stopped containers, then you need to use
$ docker container prune
This removes all stopped containers by giving you the following messages.
Warning! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
your container id list will be printed.
It could simply means that you have no container with a status 'exited'.
The commands becomes then:
sudo docker rm -v
The lack of any parameter would trigger the error message you see.
But today, this would be done with docker container prune anyway.
$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get
http://%2Fvar%2Frun%2Fdocker.sock/v1.30/containers/json?all=1&filters=%7B%22status%22%3A%7B%22exited%22%3Atrue%7D%7D:
dial unix /var/run/docker.sock: connect: permission denied
"docker rm" requires at least 1 argument(s).
See 'docker rm --help'.
The permission denied message comes from the embedded docker ps command. It is run by the shell outside of your parent sudo command, and the output is passed to sudo to run the docker rm as root. There are several fixes.
The easy option, run the docker ps with sudo:
$ sudo docker rm -v $(sudo docker ps -aq -f status=exited)
Option two is to run an entire shell as root:
$ sudo -s
# docker rm -v $(docker ps -aq -f status=exited)
# exit
Or you can give your user access to the docker socket so sudo is no longer needed:
$ sudo usermod -aG docker $USER
$ newgrp docker
The above is a one time change, and gives that user root access implicitly with docker. Then you can run:
$ docker rm -v $(docker ps -aq -f status=exited)
What seems to be happening is docker was started with different user. Hence, docker ps -aq -f status=exited could not be run due permission issue and as a result got blank result.
Running docker rm complains of missing argument due to blank result.
michael#michael-desktop:~$ sudo docker rm -v $(docker ps -aq -f status=exited)
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock:
Actually its an rights issue.... the error message:
Got permission denied while trying to connect to the Docker daemon socket at unix://
Tells you that you cant connect to you docker daemon which is running under root. You should decide if you want to use docker with # sudo or as root user.
Manage Docker as a non-root user
Further as said the docker rm complains about no images found for deletion therefore it wouldn't possible to delete images.
The command docker rm $(docker ps -aq -f status=exited) is just fine with newest docker version 18.09.0 but you could use docker container prune as well that is the more interactive way.
File name: dockerHandler.sh
#!/bin/bash
set -e
to=$1
shift
cont=$(docker run -d "$#")
code=$(timeout "$to" docker wait "$cont" || true)
docker kill $cont &> /dev/null
docker rm $cont
echo -n 'status: '
if [ -z "$code" ]; then
echo timeout
else
echo exited: $code
fi
echo output:
# pipe to sed simply for pretty nice indentation
docker logs $cont | sed 's/^/\t/'
docker rm $cont &> /dev/null
But whenever I check the docker container status after running the the docker image it is giving list of exited docker containers.
command: docker ps -as
Hence to delete those exited containers I am running manually below command
rm $(docker ps -a -f status=exited -q)
You should add the flag --rm to your docker command:
From Docker man:
➜ ~ docker run --help | grep rm
--rm Automatically remove the container when it exits
removed lines
docker kill $cont &> /dev/null
docker rm $cont
docker logs $cont | sed 's/^/\t/'
and used gtimeout instead timeout in Mac, it works fine.
To install gtimeout on Mac:
Installing CoreUtils
brew install coreutils
In line 8 of DockerTimeout.sh change timeout to gtimeout
Is there any command which can combine the docker stop and docker rm command together ? Each time I want to delete a running container, I need to execute 2 commands sequentially, I wonder if there is a combined command can simplify this process.
docker stop CONTAINER_ID
docker rm CONTATINER_ID
You can use :
docker rm -f CONTAINER_ID
It will remove the container even if it is still running.
https://docs.docker.com/engine/reference/commandline/rm/
You can also run your containers with --rm option (e.g. docker run --rm -it alpine), it will be automatically removed when stopped.
https://docs.docker.com/engine/reference/run/#clean-up---rm
Edit: The rm -f might be dangerous for your data and is best suited for test or development containers. #Bernard's comment on this subject is worth reading.
docker stop CONTAINER_ID | xargs docker rm
You can stop and remove the container with a single command the $_ gives you the last echo
docker stop CONTAINER && docker rm $_
In my case, to remove the running containers I used
docker rm -f $(docker ps -a -q)
In case you also need to remove the images, then run
docker rmi $(docker images -q) afterwards.
Only run docker rmi $(docker images -q) if you want to remove the images.
https://www.ctl.io/developers/blog/post/gracefully-stopping-docker-containers/
You can use kill, and also by using rm and the force flag it will also use kill.
Remove all containers: docker ps -aq | xargs docker rm -f
This will stop and remove all images including running containers as we are using -f
docker rmi -f $(docker images -a -q)
Use the docker ps command with the -a flag to locate the name or ID of the containers you want to remove
docker ps -a
To remove: $ docker rm ID_or_Name ID_or_Name
Remove a container upon exit:
If you know when you’re creating a container that you won’t want to keep it around once you’re done, you can run docker run --rm to automatically delete it when it exits.
Run and Remove : docker run --rm image_name
Remove all exited containers:
You can locate containers using docker ps -a and filter them by their status: created, restarting, running, paused, or exited. To review the list of exited containers, use the -f flag to filter based on status. When you've verified you want to remove those containers, using -q to pass the IDs to the docker rm command.
List:
docker ps -a -f status=exited
docker rm $(docker ps -a -f status=exited -q)
Remove containers using more than one filter:
Docker filters can be combined by repeating the filter flag with an additional value. This results in a list of containers that meet either condition. For example, if you want to delete all containers marked as either Created (a state which can result when you run a container with an invalid command) or Exited, you can use two filters:
docker ps -a -f status=exited -f status=created
Stop and Remove all the containers:
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
For removing a single container
docker rm -f CONTAINER_ID
For removing all containers
docker rm -f `docker container ps -qa`
To remove all stopped containers docker system prune
To stop live container, docker stop CONTAINER_ID waits 10 sec and it calls docker kill CONTAINER_ID
Or with docker kill CONTAINER_ID, you can immediately stop the container
remove all container with xargs
docker ps -a -q | xargs docker rm
for stop all
sudo docker ps -a -q |sudo xargs docker stop
remove single container
docker rm -f <container_id>