title: How to know if a service container is ready for connections?
body: <p>I frequently run into this issue creating stacks in Docker. Say I have a LAMP stack in docker-compose.yml:</p>
  
  <pre><code>db:
      image: mysql
      environment:
          MYSQL_VARIOUS: things
  web:
      build: .
      links:
          - db
      ports:
          - "80"
      entrypoint:
          - setup-and-start
  </code></pre>
  
  <p>And the <code>setup-and-start</code> script looks like this:</p>
  
  <pre><code>wait_for_mysql &amp;&amp; initialize
  </code></pre>
  
  <p>If the web container has mysql-client installed on it, then I can easily define <code>wait_for_mysql</code>:</p>
  
  <pre><code>wait_for_mysql() {
      local timeout=10
      while sleep 1; do
          mysql [credentials] -e 'select 1;' &amp;&amp; return 0
          (( --timeout &lt;= 0 )) &amp;&amp; break
      done
      echo 'Failed to initialize mysql within time limit.' &gt;&amp;2
      return 1
  }
  </code></pre>
  
  <p>But many containers won't have the client. Rather than install it into every lightweight container, is there a reasonable way I can check if mysql is up and running before initializing my application? It should work on most Docker containers, but doesn't have to be a perfect validation of mysql; even checking for an expected telnet response would probably be acceptable (except that telnet is also not commonly on docker images).</p>