Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stopping Docker containers by image name - Ubuntu

People also ask

How do you stop a container with name?

We can feed the output of the docker container ls command into docker container stop by running docker container stop $(docker container ls -q --filter name=myapp*) .

How do I stop docker from using images?

To stop a container you use the docker stop command and pass the name of the container and the number of seconds before a container is killed. The default number of seconds the command will wait before the killing is 10 seconds.

How do I stop a running docker container?

Note that pressing `Ctrl+C` when the terminal is attached to a container output causes the container to shut down. Use `Ctrl+PQ` in order to detach the terminal from container output.


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/

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