title: See if a Docker Container Exists With a Specific Name
body: <p>I am trying to find if a Docker Container with a specific name exists, but I seem to be unsuccessful. I have found multiple answers from other posts, the most notable one being <a href="https://stackoverflow.com/questions/38576337/how-to-execute-a-bash-command-only-if-a-docker-container-with-a-given-name-does">this</a> one, but I cannot seem to reproduce their success. Their answer returns nothing when it is tried with the container in multiple states (sometimes stopped, sometimes running), but I can't seem to get an output. It is not supposed to do anything if the container is running for right now. The block of code that I've been using is:</p>
  <pre><code>if [ ! &quot;$(docker ps -q -f name=mysql80)&quot; ]; then
      if [ &quot;$(docker ps -aq -f status=exited -f name=mysql80)&quot; ]; then
          # cleanup
          docker rm mysql80
      fi
      # run your container
      docker run -d -p 3306:3306 -p 33060:33060 --volume &quot;$MYSQLFOLDERLOCATION&quot;:/var/lib/mysql --name mysql80 frostedflakez/php-mysql-webserver:0.9-beta.3-mysql-latest-8.0
  fi
  </code></pre>
  <p>I expect this block of code to find the container, and remove it if it is exited. I know this can sometimes be hazardous. I am running macOS 10.15.6 Catalina (latest update), with <code>Docker Desktop 2.3.0.4 (46911)</code>.</p>
  <pre><code>% docker ps
  CONTAINER ID        IMAGE                                                           COMMAND                  CREATED             STATUS              PORTS                                              NAMES
  1300b4021189        frostedflakez/php-mysql-webserver:0.9-beta.3-mysql-latest-8.0   &quot;docker-entrypoint.s…&quot;   6 seconds ago       Up 5 seconds        0.0.0.0:3306-&gt;3306/tcp, 0.0.0.0:33060-&gt;33060/tcp   mysql80
  4e83c877642b        frostedflakez/php-mysql-webserver:0.9-beta.3-php-latest-7.4     &quot;docker-php-entrypoi…&quot;   8 seconds ago       Up 6 seconds        0.0.0.0:80-&gt;80/tcp, 0.0.0.0:443-&gt;443/tcp           php74
  </code></pre>
  <p>^^^ This is my <code>docker ps</code> output</p>