Stopping Docker containers by image name - Ubuntu

后端 未结 16 1462
故里飘歌
故里飘歌 2020-12-12 10:04

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 comma

16条回答
  •  甜味超标
    2020-12-12 10:39

    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=
    

    does not always work

    docker ps -a -q --filter="name="
    

    filters by container name, not image name

    docker ps | grep  | awk '{print $1}'
    

    is problematic since the image name may appear in other columns for other images

提交回复
热议问题