How to docker remove all containers based on image name - docker

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>"

Related

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

What is the best practice to prune dangled images and its associated container(s) based on image label?

I have couple of dangled images (images that appear in docker images with none tag). To clean them, based on LABEL, the usual command is: docker image prune -a --force --filter="label=some-key=some-value".
However, this command doesn't prune dangled images that have container associated. Irrespective of whether the container is started or stopped.
I cannot run docker container prune -f since I don't want to prune all stopped containers. Is there a command to prune images and the containers associated with the image based on image label?
Given an image (or images) with a label, you want to delete any containers using that image.
Using xargs and the ancestor filter, you can do that:
docker image ls -q --filter "label=foo=bar" | \
xargs -I IMG docker ps -aq --filter "ancestor=IMG" | \
xargs docker rm -fv
It's not exactly "one command" but it should get you where you need to go.
Or Powershell:
#(docker image ls -q --filter "label=foo=bar") | %{&"docker" ps -qa --filter "ancestor=$_"} | %{&"docker" rm -fv $_}

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)

Stop docker containers with name matching a pattern

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

Stopping Docker containers by image name - Ubuntu

On Ubuntu 14.04 (Trusty Tahr) I'm looking for a way to stop a running container and the only information I have is the image name that was used in the Docker run command.
Is there a command to find all the matching running containers that match that image name and stop them?
If you know the image:tag exact container version
Following issue 8959, a good start would be:
docker ps -a -q --filter="name=<containerName>"
Since name refers to the container and not the image name, you would need to use the more recent Docker 1.9 filter ancestor, mentioned in koekiebox's answer.
docker ps -a -q --filter ancestor=<image-name>
As commented below by kiril, to remove those containers:
stop returns the containers as well.
So chaining stop and rm will do the job:
docker rm $(docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}"))
If you know only the image name (not image:tag)
As Alex Jansen points out in the comments:
The ancestor option does not support wildcard matching.
Alex proposes a solution, but the one I managed to run, when you have multiple containers running from the same image is (in your ~/.bashrc for instance):
dsi() { docker stop $(docker ps -a | awk -v i="^$1.*" '{if($2~i){print$1}}'); }
Then I just call in my bash session (after sourcing ~/.bashrc):
dsi alpine
And any container running from alpine.*:xxx would stop.
Meaning: any image whose name is starting with alpine.
You might need to tweak the awk -v i="^$1.*" if you want ^$1.* to be more precise.
From there, of course:
drmi() { docker rm $(dsi $1 | tr '\n' ' '); }
And a drmi alpine would stop and remove any alpine:xxx container.
The previous answers did not work for me, but this did:
docker stop $(docker ps -q --filter ancestor=<image-name> )
You could start the container setting a container name:
docker run -d --name <container-name> <image-name>
The same image could be used to spin up multiple containers, so this is a good way to start a container. Then you could use this container-name to stop, attach... the container:
docker exec -it <container-name> bash
docker stop <container-name>
docker rm <container-name>
This code will stop all containers with the image centos:6. I couldn't find an easier solution for that.
docker ps | grep centos:6 | awk '{print $1}' | xargs docker stop
Or even shorter:
docker stop $(docker ps -a | grep centos:6 | awk '{print $1}')
Two ways to stop running a container:
1. $docker stop container_ID
2. $docker kill container_ID
You can get running containers using the following command:
$docker ps
Following links for more information:
https://docs.docker.com/engine/reference/commandline/stop/
https://docs.docker.com/v1.8/reference/commandline/kill/
This will only stop all containers with image = "yourImgName" :
sudo docker stop $(sudo docker ps | grep "yourImgName" | cut -d " " -f 1)
This will stop and remove all containers with image = "yourImgName" :
sudo docker rm $(sudo docker stop $(sudo docker ps -a | grep "yourImgName" | cut -d " " -f 1))
I made a /usr/local/bin/docker.stop that takes in the image name (assumes you only have one running).
docker stop $(docker ps -q -f "name=$1")
Stop docker container by image name:
imagename='mydockerimage'
docker stop $(docker ps | awk '{split($2,image,":"); print $1, image[1]}' | awk -v image=$imagename '$2 == image {print $1}')
Stop docker container by image name and tag:
imagename='mydockerimage:latest'
docker stop $(docker ps | awk -v image=$imagename '$2 == image {print $1}')
If you created the image, you can add a label to it and filter running containers by label
docker ps -q --filter "label=image=$image"
Unreliable methods
docker ps -a -q --filter ancestor=<image-name>
does not always work
docker ps -a -q --filter="name=<containerName>"
filters by container name, not image name
docker ps | grep <image-name> | awk '{print $1}'
is problematic since the image name may appear in other columns for other images
list all containers with info and ID
docker ps
docker stop CONTAINER ID
For Docker version 18.09.0
I found that format flag won't be needed
docker rm $(docker stop $(docker ps -a -q -f ancestor=<image-name>))
I was trying to wrap my Docker commands in gulp tasks and realised that you can do the following:
docker stop container-name
docker rm container-name
This might not work for scenarios where you have multiple containers with the same name (if that's possible), but for my use case it was perfect.
In my case --filter ancestor=<image-name> was not working, so the following command cleaned up the Docker container for me:
docker rm $(docker stop $(docker ps -a -q --filter "name=container_name_here" --format="{{.ID}}"))
Adding on top of #VonC superb answer, here is a ZSH function that you can add into your .zshrc file:
dockstop() {
docker rm $(docker stop $(docker ps -a -q --filter ancestor="$1" --format="{{.ID}}"))
}
Then in your command line, simply do dockstop myImageName and it will stop and remove all containers that were started from an image called myImageName.
use: docker container stop $(docker container ls -q --filter ancestor=mongo)
(base) :~ user$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d394144acf3a mongo "docker-entrypoint.s…" 15 seconds ago Up 14 seconds 0.0.0.0:27017->27017/tcp magical_nobel
(base) :~ user$ docker container stop $(docker container ls -q --filter ancestor=mongo)
d394144acf3a
(base) :~ user$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
(base) :~ user$
This is my script to rebuild docker container, stop and start it again
docker pull [registry]/[image]:latest
docker build --no-cache -t [localregistry]/[localimagename]:latest -f compose.yaml context/
docker ps --no-trunc | grep [localimagename] | awk '{print $1}' | xargs docker stop
docker run -d -p 8111:80 [localregistry]/[localimagename]:latest
note --no-trunc argument which shows the image name or other info in full lenght in the output
Here's a concise command which doesn't require you to specify the image tag (as most of these answers do):
docker stop $(docker ps -a | awk -v i="^${image_name}.*" '{if($2~i){print$1}}')
docker stop $(docker ps -a | grep "zalenium")
docker rm $(docker ps -a | grep "zalenium")
This should be enough.
If you want to prefer a simple AWK approach, here Is my take:
docker rm -f $(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}')
$(docker ps | awk '{ if($2 == "<your image name>") { print $NF}}') - prints the docker container names based on input image
docker ps - list all containers
awk '{ if($2 == "<your-image-name>") { print $NF}}' - The second parsed column of docker ps gives the image name. Comparing it with your image name will execute print $NF which prints the container name.
docker rm -f removes the containers
For example, removing all running containers of ubuntu image, can be done simply as:
docker rm -f $(docker ps | awk '{ if($2 == "ubuntu:latest") { print $NF}}')
PS: Remember to include the image tag in AWK, since it's a equal comparator.
if you know a part of the container name you can use AWK with docker as following :
CONTAINER_IDS=$(docker ps -a | awk '($2 ~ /container.*/) {print $1}')
if [ -z "$CONTAINER_IDS" -o "$CONTAINER_IDS" == " " ]; then
echo "No containers available for deletion"
else
docker rm -f $CONTAINER_IDS
fi
image: docker
services:
- docker:dind
stages:
- deploy
step-deploy-prod:
stage: deploy
tags:
- docker
script:
- container_id=$(docker ps -q -a -f status=running --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker stop $container_id;
docker rm -f $container_id;
fi
- container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker rm -f $container_id;
fi
- docker build -t app/creative .
- docker run -d -p 8081:80 --name todoapp app/creative
First, check for a running container with the command docker ps -q -a -f status=running --filter name=todoapp , if it finds one it stops and deletes the running container then check for any containers that are stopped and have the name todoapp using the command docker ps -q -a -f status=exited --filter name=todoapp, then it will remove the container if it's found.
Then it will build a new image and start a new container with the new build image.
As I have found out, if you stop the container, it can't be found with docker rm just incase anyone stumbles across this if you are wanting to replace a newly deployed image via gitlab-ci
There is an option in docker ps command -f status=exited which shows all the containers which are in stopped state.
container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
This command would only return container ids that are stopped and has name todoapp
Also, a better way to remove the stopped container is by using the -f or --force option with the docker rm command. This option will remove the container even if it is in a stopped state.
You can use the ps command to take a look at the running containers:
docker ps -a
From there you should see the name of your container along with the container ID that you're looking for. Here's more information about docker ps.

Resources