Stopping Docker containers by image name - Ubuntu - docker

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?

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/

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

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

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

list all containers with info and ID
docker ps
docker stop CONTAINER 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>))

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.

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

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.

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$

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

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

docker stop $(docker ps -a | grep "zalenium")
docker rm $(docker ps -a | grep "zalenium")
This should be enough.

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.

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

image: docker
services:
- docker:dind
stages:
- deploy
step-deploy-prod:
stage: deploy
tags:
- docker
script:
- container_id=$(docker ps -q -a -f status=running --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker stop $container_id;
docker rm -f $container_id;
fi
- container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
- if [ -n "$container_id" ]; then
docker rm -f $container_id;
fi
- docker build -t app/creative .
- docker run -d -p 8081:80 --name todoapp app/creative
First, check for a running container with the command docker ps -q -a -f status=running --filter name=todoapp , if it finds one it stops and deletes the running container then check for any containers that are stopped and have the name todoapp using the command docker ps -q -a -f status=exited --filter name=todoapp, then it will remove the container if it's found.
Then it will build a new image and start a new container with the new build image.
As I have found out, if you stop the container, it can't be found with docker rm just incase anyone stumbles across this if you are wanting to replace a newly deployed image via gitlab-ci
There is an option in docker ps command -f status=exited which shows all the containers which are in stopped state.
container_id=$(docker ps -q -a -f status=exited --filter name=todoapp)
This command would only return container ids that are stopped and has name todoapp
Also, a better way to remove the stopped container is by using the -f or --force option with the docker rm command. This option will remove the container even if it is in a stopped state.

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.

Related

How to avoid getting an error message when there is no docker container/image/volume to remove?

So currently in my pipeline I need to remove all previous docker containers, images and volumes, and for that I run:
docker stop $(docker ps -q)
docker rm -f $(docker ps -a -q)
docker volume rm $(docker volume ls -q)
docker rmi -f $(docker images -q)
But if for some reason, there was already any volume (or container or image), the command line returns the error: docker volume rm requires at least 1 argument.
And of course the pipeline breaks:
See the error in my pipeline
I've tried adding a line in the pipeline before, so I will always have at least one docker container/volume/image to remove, but I know this is not a good practice.
I need a command so if there is no container/volume/image, returns a 'nothing to remove' message and continues without breaking the pipeline.
Thanks!
Edit: Thanks to larsks suggestion, I ended up using:
docker ps -q | xargs --no-run-if-empty docker container stop
docker ps -a -q | xargs --no-run-if-empty docker container rm
docker volume ls -q | xargs --no-run-if-empty docker volume rm
docker images -q | xargs --no-run-if-empty docker image rm
This completely fixed my Pipeline.
You can use xargs, for example:
docker volume ls -q | xargs --no-run-if-empty docker volume rm
This will not run the docker volume rm command if docker volume ls -q produces no output.

How to docker remove all containers based on image name

My docker sometimes create randomw container name based on my docker image e.g. yeeyi
How to docker rm all off the containers where the image is yeeyi?
is there something like? docker rm all --image yeeyi in a single command line?
You can do this using this command:
docker rm $(docker ps -a -q --filter "ancestor=ubuntu")
replace ubuntu with your image name.
This basically gets all the container ids (running or otherwise) that use the image ubuntu and then removes them.
Try the below workaround(update the grep string according to your need);
docker ps --filter "status=exited" | grep yeeyi
Check the output of above command, if you have the correct list, then use the below command;
docker rm (docker ps --filter "status=exited" | grep yeeyi | awk '{print $1}')
Another option is to check the exit code of such randomly created container. If that code is different then rest you can use the below command to get list of such containers;
docker ps -a --filter "exited=<status code>"

How can stop group of containers with regex in docker-compose

Question:
How can I stop containers that their names start with server-?
Containers
> sudo docker-compose ps
Name Command State Ports
-----------------------------------------------------------------------------------------------------------------------------------------------
server-myservername1_1 some commands Up
server-myservername2_1 some commands Up
server-myservername3_1 some commands Up
server-myservername4_1 some commands Up
server-myservername5_1 some commands Up
server-myservername6_1 some commands Up
console-myconsolename1_1 some commands Up
console-myconsolename2_1 some commands Up
First check the output of below command if it's gives the names of only those containers that you have to stop
docker-compose ps | grep server | awk '{print $1}'
If the list is right, then run
docker stop $(docker-compose ps | grep server | awk '{print $1}')
P.S. I haven't tested the above command. Let me know if it doesn't
You can simply use --filter option of ps command
Suppose you wanna look for 3 containers, which their names start with site
docker ps --filter name=site*
will show you stat of those three containers.
so use one of this to stop the containers
- docker ps --filter name=site* -aq | xargs docker stop
- docker stop $(docker ps --filter name=site* -aq)

Remove all Docker containers except one

I have script that stops containers and then removes them
docker stop $(docker ps -q)
docker rm $(docker ps -a -q)
But I don't want to remove the docker container with name "my_docker".
How can I remove all containers except this one?
You can try this, which will
Filter out the unwanted item (grep -v), and then
returns the first column, which contains the container id
Run this command:
docker rm $(docker ps -a | grep -v "my_docker" | awk 'NR>1 {print $1}')
To use cut instead of awk, try this:
docker rm $(docker ps -a | grep -v "my_docker" | cut -d ' ' -f1)
Examples for awk/cut usage here: bash: shortest way to get n-th column of output
The title of the question asks for images, not containers. For those stumbling across this question looking to remove all images except one, you can use docker prune along with filter flags:
docker image prune -a --force --filter "label!=image_name"
replacing image_name with the name of your image.
You can also use the "until=" flag to prune your images by date.
This is what's actually happening docker rm $(List of container Ids). So it's just a matter of how you can filter the List of Container Ids.
For example: If you are looking to delete all the container but one with a specific container Id, then this docker rm $(docker ps -a -q | grep -v "my_container_id") will do the trick.
I achieved this by the following command:
docker image rm -f $(docker images -a | grep -v "image_repository_name" | awk 'NR>1 {print $1}')
For those, who want to exclude more than 1 container just add
grep -v "container_name2" |
after the grep -v "container_name1" command.
The final command might look like
docker rm $(docker ps -a | grep -v "my_docker1" | grep -v "my_docker2" | cut -d ' ' -f1)
I would prefer to test the container name using something along the lines of (untested)
docker inspect --format '{{ .Name }}' $(docker ps -aq)
this will give the names of the (running or not) containers, and you can filter and
docker rm
using this information
Using docker ps with --filter name=<my_docker>
docker rm $(docker ps -a -q | grep -v `docker ps -a -q --filter "name=my_docker"`)
Old question, but I like reviving posts.
For such case you could use Spotify's Docker GC: https://github.com/spotify/docker-gc#excluding-containers-from-garbage-collection
You could do:
echo "my_docker" >> /tmp/docker-gc-exclude-containers
echo '*' > /tmp/docker-gc-exclude
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock -v /etc:/etc:ro -v /tmp/docker-gc-exclude-containers:/etc/docker-gc-exclude-containers:ro -v /tmp/docker-gc-exclude:/etc/docker-gc-exclude:ro spotify/docker-gc
(if you would like to get your images cleaned off too, you can avoid mounting the docker-gc-exclude file)
To stop
docker stop $(docker ps -a -q | grep -v "my_container_id")
To remove
docker rm $(docker ps -a -q | grep -v "my_container_id")
Deletes all the images except node image
sudo docker rmi -f $(sudo docker image ls -a | grep -v "node" | awk 'NR>1 {print $3}')
I know its little late response but it can help any windows user. Here is the command I prepared:
docker rmi $(
docker image list -a --no-trunc --format "table {{.ID}},{{.Repository}}" |
Where-Object {
$_ -NOTMATCH "mcr.microsoft.com/dotnet/core/aspnet" -and
$_ -NOTMATCH "ubuntu" -and
$_ -NOTMATCH "busybox" -and
$_ -NOTMATCH "alpine"
} | Select-Object -Skip 1 | %{ $_.Split(',')[0];}
)
Maybe you can start all the containers you don't want to prune. Then, run:
docker container prune -a

How do I delete all running Docker containers?

I remember using
docker rm -f `docker ps -aq`
to chain the commands without an issue a few months ago, but now this isn't working, and I'm getting the following output:
unknown shorthand flag: 'a' in -aq`
See 'docker rm --help'.
What changed? How can I delete all Docker running containers in one line? In case it helps, I'm using Docker for Windows (native with Hyper-V, not with VirtualBox) on Windows 10, and the command I used has worked fine with my previous Windows 8 Docker toolbox installation.
Till now (Docker version 1.12) we are using the following command to delete all the running containers (also, if we want to delete the volumes, we can do that manually using its respective tag -v in the following command),
Delete all Exited Containers
docker rm $(docker ps -q -f status=exited)
Delete all Stopped Containers
docker rm $(docker ps -a -q)
Delete All Running and Stopped Containers
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
Remove all containers, without any criteria
docker container rm $(docker container ps -aq)
But, in version 1.13 and above, for complete system and cleanup, we can directly user the following command,
docker system prune
All unused containers, images, networks and volumes will get deleted. Also, individually i.e. separately, we can do that using the following commands, that clean up the components,
docker container prune
docker image prune
docker network prune
docker volume prune
I had this issue when running in cmd. Switched to PowerShell and it worked!
Use:
docker rm -f $(docker ps -aq)
If anybody needs the Windows Shell Command (stop and remove container), here it is:
for /F %c in ('docker ps -a -q') do (docker stop %c)
for /F %c in ('docker ps -a -q') do (docker rm %c)
If you put it in a batch file, just add % to %c:
for /F %%c in ('docker ps -a -q') do (docker stop %%c)
I've had the same problem: I was on a Windows machine and used Docker within VirtualBox and the command docker rm -f ${docker ps -aq} worked well. Then I switched to Docker for Windows and the command didn't work on the Windows command line.
But using Cygwin or Git Bash solved the problem for me.
Try using this command.
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
$ docker rm $(docker ps --filter status=created -q)
Tested on Docker version 19.03.5, build 633a0ea on Mac OS Mojave.
Run docker commands in Windows PowerShell will execute and run most of the commands
Hope you also remember to stop running containers first before running the delete command
docker stop $(docker ps -aq)
Use this:
docker rm -f $(docker ps | grep -v CONTAINER | awk '{print $1}')
If you want to include previously stopped containers:
docker rm -f $(docker ps -a | grep -v CONTAINER | awk '{print $1}')
we can delete all running containers in docker ENV by the following the command -
docker container rm -f $(docker container ls -aq)
It should to the magic
if we have run our docker container using docker-compose.yaml then
docker-compose -f /path/to/compose/file down
should work
Single command to delete all stop and running containers (first stop and then just prune/remove them. Works for me all the time.
docker stop $(docker ps -a -q) && docker container prune -a
If the container is running, you cannot delete the image. First stop all the containers using the following command.
docker stop $(docker ps -aq)
you are saying running stop against the output of docker ps -aq.
'a' - get me all the containers
'q' - return only the container id.
Then run the following command to remove all the containers.
docker rm $(docker ps -aq)

Resources