Reputation: 121
I need to run more than 70 docker containers at once. Later, these containers need to be stopped.
At the moment I can docker stop
all of them with the shell command docker stop $(docker ps -f since=<last docker before>)
. It works OK, but if there are any containers started after mine, I have a problem as the above code will stop them too.
Is there any way I can close all of running containers with some kind of specific search?
I know there is an docker ps -f label=<some label>
, but I just haven't figured out on how to use it yet.
Upvotes: 0
Views: 635
Reputation: 2076
If you're launching many containers at the same time, launch them all with
docker run --label=anyname other-docker-args-of-yours image:tag
And when you want to delete all your containers just do
docker stop $(docker ps -f label=anyname | awk 'NR>1 {print$1}')
where anyname
is the label name you provide during the docker run
command and
awk 'NR>1 {print$1}'
ignores the column header CONTAINER_ID
and just prints the values alone.
Edit-1:
I later realized that you can achieve the list of Container_ID without awk
as well. I'd consider using the below line.
docker stop `docker ps -qaf label=anyname`
If you want to remove all stoppped containers also, then include a
within the options, like instead of -qf
use -qaf
.
-q
to print container IDs alone.
-a
for all containers including stopped.
Upvotes: 4