Stopping Docker containers by image name - Ubuntu

百般思念 提交于 2019-11-29 18:45:45
VonC

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

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

docker stop $(docker ps -q --filter ancestor=<image-name> )
Javier Sánchez

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

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

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:

list all containers with info and ID

docker ps

docker stop CONTAINER ID

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

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

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

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>))
docker stop $(docker ps -a | grep "zalenium")
docker rm $(docker ps -a | grep "zalenium")

This should be enough.

Matt Gaunt

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.

Aaron Tracy

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!