Stop Docker containers by image name - Ubuntu

In Ubuntu 14.04 (Trusty Tahr), I'm looking for a way to stop a running container, and the only information I have is the name of the image that was used in the Docker launch command.

Is there a command to find all relevant running containers that match this image name and stop them?

+121
docker ubuntu docker-image
Aug 18 '15 at 13:35
source share
13 answers

With the release of 8959, a good start would be:

docker ps -a -q --filter="name=<containerName>" 

Since name refers to the container, not the image name, you will need to use the later ancestor of the Docker 1.9 filter mentioned in koekiebox's answer .

 docker ps -a -q --filter ancestor=<image-name> 



As Cyril commented below to remove these containers:

stop returns containers.

So the stop chain and rm will do their job:

 docker rm $(docker stop $(docker ps -a -q --filter ancestor=<image-name> --format="{{.ID}}")) 
+158
Aug 18 '15 at 13:40
source share

The previous answers didn’t work for me, but it worked:

 docker stop $(docker ps -q --filter ancestor=<image-name> ) 
+52
Jan 20 '16 at 12:05
source share

You can start the container by specifying the name of the container:

 docker run -d --name <container-name> <image-name> 

You can use the same image to promote multiple containers, so this is a good way to start a container. Then you can use this container name to stop, attach ... the container:

 docker exec -it <container-name> bash docker stop <container-name> docker rm <container-name> 
+36
Apr 14 '16 at 16:21
source share

This code will stop all containers with centos: 6 image. I could not find a simpler solution for this.

 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}') 
+13
Jun 14 '16 at 4:47
source share

Two ways to stop container launch:

 1. $docker stop container_ID 2. $docker kill container_ID 

You can start running containers using the following command:

 $docker ps 

The following links for more information:

+9
Jun 28 '16 at 8:43
source share

Stop dock 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 the 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 an image, you can add a label to it and filter the running containers by label

 docker ps -q --filter "label=image=$image" 



Untrusted methods

 docker ps -a -q --filter ancestor=<image-name> 

not always working

 docker ps -a -q --filter="name=<containerName>" 

filters by container name, not image name

 docker ps | grep <image-name> | awk '{print $1}' 

problematic as the image name may appear in other columns for other images

+2
May 10 '19 at 17:49
source share

I made /usr/local/bin/docker.stop , which accepts the name of the image (it is assumed that you have only one run).

 docker stop $(docker ps -q -f "name=$1") 
+1
Sep 08 '15 at 15:46
source share

In my case --filter ancestor=<image-name> did not work, so the following command cleared the Docker container for me:

 docker rm $(docker stop $(docker ps -a -q --filter "name=container_name_here" --format="{{.ID}}")) 
+1
Feb 24 '17 at 16:20
source share

For Docker version 18.09.0, I found that the format flag is not needed

 docker rm $(docker stop $(docker ps -a -q -f ancestor=<image-name>)) 
+1
Dec 19 '18 at 16:29
source share
 docker stop $(docker ps -a | grep "zalenium") docker rm $(docker ps -a | grep "zalenium") 

That should be enough.

+1
May 2 '19 at 13:30
source share

I tried to wrap my Docker commands in gulp tasks and realized that you can do the following:

 docker stop container-name docker rm container-name 

This may not work for scenarios in which you have multiple containers with the same name (if possible), but for my use case this was ideal.

0
Mar 23 '16 at 23:14
source share

list of all containers with information and identifier

 docker ps docker stop CONTAINER ID 
0
Jul 14 '19 at 9:19
source share

You can use the ps command to look at working containers:

 docker ps -a 

From there, you should see the name of your container along with the identifier of the container you are looking for. Here is more information about docker ps .

-5
Aug 18 '15 at 23:50
source share



All Articles