<p>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:</p>

<pre><code>#!/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
</code></pre>

<p>Problem is, while the <code>docker rmi</code> command inside the for loop runs for all other 4 nodes, I get the error <code>Error: No such image: &lt;image-id&gt;</code> 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.</p>

<p>The problem is that the only the first command in the ssh pipe is executed remotely:</p>

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

<p>Shell understand that it is</p>

<pre><code>ssh ssh-arguments | grep grep-arguments | awk awk-arguments | xarg xarg-arguments
</code></pre>

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

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

<pre><code>ssh root@${host} "docker images | grep localhost | awk '{print $3}' | xargs docker rmi -f"
</code></pre>
