Stop dock 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 the 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 an image, you can add a label to it and filter the running containers by label
docker ps -q --filter "label=image=$image"
Untrusted methods
docker ps -a -q --filter ancestor=<image-name>
not always working
docker ps -a -q --filter="name=<containerName>"
filters by container name, not image name
docker ps | grep <image-name> | awk '{print $1}'
problematic as the image name may appear in other columns for other images
Jay May 10 '19 at 17:49 2019-05-10 17:49
source share