Stopping Docker containers by image name - Ubuntu

2019-01-29 22:24发布

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?

标签: ubuntu docker
10条回答
\"骚年 ilove
2楼-- · 2019-01-29 22:54

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}')
查看更多
萌系小妹纸
3楼-- · 2019-01-29 22:55

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")
查看更多
狗以群分
4楼-- · 2019-01-29 22:58

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.

查看更多
我想做一个坏孩纸
5楼-- · 2019-01-29 23:04

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

docker stop $(docker ps -q --filter ancestor=<image-name> )
查看更多
虎瘦雄心在
6楼-- · 2019-01-29 23:05

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:

查看更多
相关推荐>>
7楼-- · 2019-01-29 23:11

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>
查看更多
登录 后发表回答