Stop docker containers with name matching a pattern - docker

I'm using puckel/docker-airflow with CeleryExecutor. It launches a total of 5 containers named like this
docker-airflow_flower_1_de2035f778e6
docker-airflow_redis_1_49d2e710e82b
..
While development, I often have to stop all above containers. However, I can't do a docker stop $(docker ps -aq) since I have other containers running on my machine too.
Is there a way to stop all containers who's names match a given pattern (for instance all containers who's names start with docker-airflow in above)?

From this article by #james-coyle, following command works for me
docker ps --filter name=docker-airflow* --filter status=running -aq | xargs docker stop
I believe docker CLI natively does not provide such a functionality, so we have to rely on filtering and good-old bash PIPE and xargs
UPDATE-1
Note that depending on your environment, you might have to do these
run docker commands with sudo (just prefix both docker .. commands above with sudo)
enclose name pattern in double-quotes --filter name="docker-airflow*" (particularly on zsh)

Better late than never ;). From this article. The following works for me:
Stop containers with names matching a given pattern:
$ docker container stop $(docker container ls -q --filter name=<pattern>)
On the other hand, if we want to start containers with names matching a given pattern:
$ docker container start $(docker container ls --all -q --filter name=<pattern>)
NOTE: For different environments related tips, #y2k-shubham's update is a good starting point.

Another approach using grep and docker ps:
To stop docker container matching the given pattern/list of pattern":
docker ps | grep -E "name_1|name_2|name_3" | awk '{print $1}' | xargs docker stop
To stop docker container excluding the given pattern/list of pattern:
docker ps | grep -Ev "name_1|name_2|name_3" | awk '{print $1}' | xargs docker stop
Reference: Grep

Related

Docker: Filter containers by image, ignoring the tag, then the images

I need to remove a series of open containers which are derived from an image which I have tagged many times so I have plenty of versions around. I also have plenty of containers running (which remained hanging from various tests I did with the various versions of the container).
What are the commands to quickly erase all of them? (I.E based on image name or whatever)
I found that you can do
docker container stop $(docker container ls -aq --filter "ancestor=imagename")
But this doesn't work with the various tagged versions (like imagename:1.0, imagename:2.0)
so to get all of them I had to use
docker container stop $(docker container ls -aq --limit 30)
Which gets the container created up to 1 month ago.
Prune just cleaned a few of them.
I won't list every and all the commands I tried.
Please tell me what is the quickest and cleanest way to clean an image and all the successive tagged versions, and the relative containers which are running or exited related to them.
Thanks
Can't you just use some linux magic? For example
docker ps | grep <image-name> | awk '{ print $1 }' | xargs docker stop
Explanation:
docker ps show running containers
grep <image-name> search for image-name in the previous command output
awk '{ print $1 }' take the first 'column' (in this case the container id)
xargs docker stop feed the ids to docker stop
Similarly, you could just use other bash commands to delete the containers and remove the images:
docker ps -a | grep <image-name> | awk '{ print $1 }' | xargs docker container rm
docker image ls | grep <image-name> | awk '{ print $3 }' | xargs docker rmi
A little warning: grep is not specific to docker and will filter just based on the contents of the string so if your image name appears in the container names or anywhere else, then this method will stop/remove those containers as well!

Remove docker containers according to days filter

I have containers with a specific tag. I want to delete all containers which are running more than 3 days.
docker ps -a | grep -i "ngnix" | xargs docker container prune --force --filter "until=3days"
But its not working. Can anyone please help me?
If it's not clear:
I have list of containers with same tag name but different container_name and now I want to first grep all the containers with same tag name and stop all containers running since last 3 days
Your requirement is not clear but here is something I found related to it.
docker container prune provides a filter to remove containers until a specific date:
docker container prune -a --filter "until=$(date +'%Y-%m-%dT%H:%M:%S' --date='-3 days')"
OR
Using the command below, it will stop all containers running with a specific image, since the time of container id you provide (which is basically for time reference).
There are the following filters used :
ancestor - image_name
status - One of created, restarting, running, removing, paused, exited, or dead
since - The since filter shows only containers created since the container with given id or name.
( In your case give conaitner id which is just older than 3 days.)
docker container ls -a --filter ancestor=image_name --filter status=running --filter since=container_id | xargs docker stop
Using the above command will stop all containers running. Now in order to remove them, you just have to change filter values and change command to docker rm.
This command worked for me
Cleanup:
stage: cleanup
script:
- docker ps -a --filter "name=mapping" | grep 'days ago' | awk '{print $1}' | xargs docker stop
- docker image prune -f --filter "until=48h"
- docker system prune -f

How to docker remove all containers based on image name

My docker sometimes create randomw container name based on my docker image e.g. yeeyi
How to docker rm all off the containers where the image is yeeyi?
is there something like? docker rm all --image yeeyi in a single command line?
You can do this using this command:
docker rm $(docker ps -a -q --filter "ancestor=ubuntu")
replace ubuntu with your image name.
This basically gets all the container ids (running or otherwise) that use the image ubuntu and then removes them.
Try the below workaround(update the grep string according to your need);
docker ps --filter "status=exited" | grep yeeyi
Check the output of above command, if you have the correct list, then use the below command;
docker rm (docker ps --filter "status=exited" | grep yeeyi | awk '{print $1}')
Another option is to check the exit code of such randomly created container. If that code is different then rest you can use the below command to get list of such containers;
docker ps -a --filter "exited=<status code>"

How can stop group of containers with regex in docker-compose

Question:
How can I stop containers that their names start with server-?
Containers
> sudo docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------------------------
server-myservername1_1 some commands Up
server-myservername2_1 some commands Up
server-myservername3_1 some commands Up
server-myservername4_1 some commands Up
server-myservername5_1 some commands Up
server-myservername6_1 some commands Up
console-myconsolename1_1 some commands Up
console-myconsolename2_1 some commands Up
First check the output of below command if it's gives the names of only those containers that you have to stop
docker-compose ps | grep server | awk '{print $1}'
If the list is right, then run
docker stop $(docker-compose ps | grep server | awk '{print $1}')
P.S. I haven't tested the above command. Let me know if it doesn't
You can simply use --filter option of ps command
Suppose you wanna look for 3 containers, which their names start with site
docker ps --filter name=site*
will show you stat of those three containers.
so use one of this to stop the containers
- docker ps --filter name=site* -aq | xargs docker stop
- docker stop $(docker ps --filter name=site* -aq)

Shell command to Get container id from "docker ps"

I am basically looking to achieve this 2 steps:
1. Run the docker image:
docker run -p 80:80 some-image-name:25
2. Now "docker ps" returns whole data about the container but I was looking for just the container ID to
3. run some test on it.. ex.
docker exec -it /usr/bin/npm run test
So my question is how I can get just the container id from step 2.
Note: I need this flow for my pipeline script in Jenkins.
docker ps -a -q
This will give you only container's id
You could use awk to get the container ID's as follows:
docker ps | awk 'NR > 1 {print $1}'
This one-liner outputs all the container ID's printed by docker ps. To get only the first one you would use:
docker ps | awk 'NR > 1 {print $1; exit}'
Even though that answers your question I recommend that you use container names instead of relying on container ID's.
P.S.: This solution is on average 1 millisecond slower than docker ps -q, but it is significantly more flexible.
docker ps --format {{.ID}}
Will return only the ids of running containers.
you can use docker functionality to get this done:
docker ps --filter volume=remote-volume --format "table {{.ID}}\t{{.Mounts}}"
with --format "{{.ID}}" you'd get the ids only. You can also filter. Read the documentation of docker ps for more details
All the below command give you container id's
docker ps -aqf "name=containername"
docker ps --no-trunc -aqf name=containername
docker container ls | grep 'container-name' | awk '{print $1}'```
You can get container ID using following command:
docker ps -q

Resources