Issue in removing docker images remotely on CentOS

1/3/2019

I've installed a Kubernetes cluster using Rancher on 5 different CentOS nodes (let's say node1, node2, ..., node5). For our CI run, we need to clean up stale docker images before each run. I created a script that runs on node1, and password-less ssh is enabled from node1 to rest of the nodes. The relevant section of the script looks something like below:

#!/bin/bash
helm ls --short --all | xargs -L1 helm delete --purge

echo "Deleting old data and docker images from Rancher host node."
rm -rf /var/lib/hadoop/* /opt/ci/*
docker images | grep localhost | awk '{print $3}' | xargs docker rmi -f

hosts=(node2 node3 node4 node5)
for host in ${hosts[*]}
do
  echo "Deleting old data and docker images from ${host}"
  ssh root@${host} docker images | grep localhost | awk '{print $3}' | xargs docker rmi -f
  ssh root@${host} rm -rf /var/lib/hadoop/* /opt/ci/*
done
echo "All deletions are complete! Proceeding with installation."

sleep 2m

Problem is, while the docker rmi command inside the for loop runs for all other 4 nodes, I get the error Error: No such image: <image-id> for each of the images. But if I execute same command on that node, it succeeds. I'm not sure what's the issue here. Any help is appreciated.

-- Bitswazsky
bash
docker
kubernetes
linux

1 Answer

1/3/2019

The problem is that the only the first command in the ssh pipe is executed remotely:

ssh root@${host} docker images | grep localhost | awk '{print $3}' | xargs docker rmi -f

Shell understand that it is

ssh ssh-arguments | grep grep-arguments | awk awk-arguments | xarg xarg-arguments

And the result is that the only docker images is executed remotely. Then the output from the remote docker images is transferred to the local machine where it is filtered by grep and awk and then docker rmi is executed on local machine.

It is necessary to add quotes to inform shell that everything at command line is a ssh argument:

ssh root@${host} "docker images | grep localhost | awk '{print $3}' | xargs docker rmi -f"
-- Zaboj Campula
Source: StackOverflow