Stopping docker containers by image name, and don't error if no containers are running
<p>This <a href="https://stackoverflow.com/questions/32073971/stopping-docker-containers-by-image-name-ubuntu">question</a> explains how to stop Docker containers started from an image.</p>

<p>But if there are no running containers I get the error <strong>docker stop requires a minimum of one argument</strong>. Which means I can't run this command in a long .sh script without it breaking.</p>

<p>How do I change these commands to work even if no results are found?</p>

<pre><code>docker stop $(docker ps -q --filter ancestor="imagname")
docker rm `docker ps -aq` &amp;&amp;
</code></pre>

<p><em>(I'm looking for a pure Docker answer if possible, not a bash test, as I'm running my script over ssh so I don't think I have access to normal script tests)</em></p>