Stopping Docker containers by image name - Ubuntu

DockerUbuntuDocker Image

Docker Problem Overview


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?

Docker Solutions


Solution 1 - Docker

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.

Solution 2 - Docker

The previous answers did not work for me, but this did:

docker stop $(docker ps -q --filter ancestor=<image-name> )

Solution 3 - Docker

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>

Solution 4 - Docker

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}')

Solution 5 - Docker

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:

Solution 6 - Docker

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

Solution 7 - Docker

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

Solution 8 - Docker

list all containers with info and ID

docker ps

docker stop CONTAINER ID

Solution 9 - Docker

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

Solution 10 - Docker

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.

Solution 11 - Docker

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

Solution 12 - Docker

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.

Solution 13 - Docker

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$

Solution 14 - Docker

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

Solution 15 - Docker

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}}')

Solution 16 - Docker

docker stop $(docker ps -a | grep "zalenium")
docker rm $(docker ps -a | grep "zalenium")

This should be enough.

Solution 17 - Docker

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.

Solution 18 - Docker

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

Solution 19 - Docker

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

Solution 20 - Docker

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCollin EstesView Question on Stackoverflow
Solution 1 - DockerVonCView Answer on Stackoverflow
Solution 2 - DockerKoekieboxView Answer on Stackoverflow
Solution 3 - DockerJavier SánchezView Answer on Stackoverflow
Solution 4 - DockerArgonQQView Answer on Stackoverflow
Solution 5 - Dockerkalyani chaudhariView Answer on Stackoverflow
Solution 6 - DockerAmanicAView Answer on Stackoverflow
Solution 7 - DockerJayView Answer on Stackoverflow
Solution 8 - DockerMuhammed MoussaView Answer on Stackoverflow
Solution 9 - DockerankitjView Answer on Stackoverflow
Solution 10 - DockerMatt GauntView Answer on Stackoverflow
Solution 11 - DockerAlexeyView Answer on Stackoverflow
Solution 12 - DockerAntoine JaussoinView Answer on Stackoverflow
Solution 13 - DockerASHView Answer on Stackoverflow
Solution 14 - DockerScholtzView Answer on Stackoverflow
Solution 15 - DockerAlex JansenView Answer on Stackoverflow
Solution 16 - DockerBerkay KirmiziogluView Answer on Stackoverflow
Solution 17 - DockerashutoshView Answer on Stackoverflow
Solution 18 - DockerarwaView Answer on Stackoverflow
Solution 19 - DockerMoha DarknessView Answer on Stackoverflow
Solution 20 - DockerAaron TracyView Answer on Stackoverflow