Can’t delete docker image with dependent child images

前端 未结 19 2177
挽巷
挽巷 2020-12-02 06:11

I am trying

docker rmi c565603bc87f

Error:

Error response from daemon: conflict: unable to delete c565603bc87f (ca

19条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 06:59

    Expanding on the answer provided by @Nguyen - this function can be added to your .bashrc etc and then called from the commandline to help clean up any image has dependent child images errors...

    You can run the function as yourself, and if a docker ps fails, then it will run the docker command with sudo and prompt you for your password.

    Does NOT delete images for any running containers!

    docker_rmi_dependants ()                                                                                                                                                         
    { 
      DOCKER=docker
      [ docker ps >/dev/null 2>&1 ] || DOCKER="sudo docker"
    
      echo "Docker: ${DOCKER}"
    
      for n in $(${DOCKER} images | awk '$2 == "" {print $3}');
      do  
        echo "ImageID: $n";
        ${DOCKER} inspect --format='{{.Id}} {{.Parent}}' $(${DOCKER} images --filter since=$n -q);
      done;
    
      ${DOCKER} rmi $(${DOCKER} images | awk '$2 == "" {print $3}')
    }
    

    I also have this in my .bashrc file...

    docker_rm_dangling ()  
    { 
      DOCKER=docker
      [ docker ps >/dev/null 2>&1 ] || DOCKER="sudo docker"
    
      echo "Docker: ${DOCKER}"
    
      ${DOCKER} images -f dangling=true 2>&1 > /dev/null && YES=$?;                                                                                                                  
      if [ $YES -eq 1 ]; then
        read -t 30 -p "Press ENTER to remove, or CTRL-C to quit.";
        ${DOCKER} rmi $(${DOCKER} images -f dangling=true -q);
      else
        echo "Nothing to do... all groovy!";
      fi  
    }
    

    Works with:

    $ docker --version 
    Docker version 17.05.0-ce, build 89658be
    

提交回复
热议问题