Stopping Docker containers by image name - Ubuntu

后端 未结 16 1458
故里飘歌
故里飘歌 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:45

    If you want to prefer a simple AWK approach, here Is my take:

    docker rm -f $(docker ps | awk '{ if($2 == "") { print $NF}}')
    

    $(docker ps | awk '{ if($2 == "") { print $NF}}') - prints the docker container names based on input image

    docker ps - list all containers

    awk '{ if($2 == "") { 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.

提交回复
热议问题