title: How to wait for a Docker container to be removed
body: <p>I have a task that I run in a docker container. I only want one instance of it running, so I name the container. Occasionally I need to modify the code or configuration so I rebuild the image, and then stop and re-start the container with the new image. I'd been doing a <code>docker stop</code> followed by a <code>docker run</code>.</p>
  
  <p>Recently, I tried to combine these into a single step:</p>
  
  <pre><code>docker stop myTask &amp;&amp;
      docker run --rm -d myTask myImage:$VERSION
  </code></pre>
  
  <p>However, this fails with:</p>
  
  <blockquote>
    <p>docker: Error response from daemon: Conflict. The container name "/myTask" is already in use by container "49d806e25655a5e6f5309cc5509b54a10b4f0c84e60fad39856777881c99e9ce". You have to remove (or rename) that container to be able to reuse that name.</p>
  </blockquote>
  
  <p>I then tried adding a <code>docker wait</code> in there:</p>
  
  <pre><code>docker stop myTask &amp;&amp;
      (docker wait myTask || true) &amp;&amp;
      docker run --rm -d myTask myImage:$VERSION
  </code></pre>
  
  <p>I still get this behavior. It seems <code>docker wait</code> only waits for the container to stop, but does not wait for it to be removed. I then tried using <code>docker rm</code>, thinking that it must surely wait for the container to be removed before returning:</p>
  
  <pre><code>docker stop myTask &amp;&amp;
      (docker rm myTask || true) &amp;&amp;
      docker run --rm -d myTask myImage:$VERSION
  </code></pre>
  
  <p>This <em>still</em> fails with a "The container name "/myTask" is already in use" error.</p>
  
  <p>How can I block until there is no container with a given name?</p>