title: Docker-compose : unable to connect to mysql from rails app first time , next run it works
body: <p>I am creating developer environment for rails app using docker, docker-compose and docker-sync. When I run docker-compose up first time it builds the image of my rails app and downloads mysql image, but not able to connect it, but if I quit this and run docker-compose up again everything works fine. Here are my docker configurations</p>
  
  <pre><code>Dockerfile:
  FROM ruby:2.5.1
  
  RUN apt-get update &amp;&amp; \
        apt-get -y install sudo
  
  RUN apt-get update &amp;&amp; apt-get install -y \ 
    build-essential \ 
    nodejs \
    vim \
    mysql-client \
    apt-transport-https \
    build-essential
  
  RUN curl -s -N https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -\
      &amp;&amp; echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list \
      &amp;&amp; apt-get update \
      &amp;&amp; apt-get install -y yarn 
  
  RUN mkdir /rails-app
  ENV RAILS_HOME /rails-app
  
  
  WORKDIR /tmp
  ADD Gemfile Gemfile
  ADD Gemfile.lock Gemfile.lock
  RUN bundle install
  
  WORKDIR /rails-app
  ADD . /rails-app
  
  EXPOSE 3000
  CMD ["bash"]
  </code></pre>
  
  <p>Below is my docker-compose</p>
  
  <pre><code>docker-compose.yml
  version: '3'
  services:
      web:
        image: "rails_app"
        build: .
        ports:
          - "3000:3000"
        env_file:
          - web_env
          - db_env
        depends_on:
          - db
        command: &gt;
            bash -c "bundle exec rails db:schema:load &amp;&amp;
                     bundle exec rails db:seed &amp;&amp;
                     rm -f tmp/pids/server.pid &amp;&amp;
                     bundle exec rails s -p 3000 -b '0.0.0.0'"
      db:
        image: library/mysql:5.6.42
        env_file:
          - db_env
  </code></pre>
  
  <p>docker-compose dev file for volume syncing</p>
  
  <pre><code>docker-compose-dev.yml
  version: '3'
  services:
        web:
           volumes:
              - rails_app_volume_sync:/rails-app:nocopy
  
  volumes:
     rails_app_volume_sync:
        external: true   
  </code></pre>
  
  <p>docker-sync file </p>
  
  <pre><code>docker-sync.yml
  version: "2"
  syncs:
      rails_app_volume_sync:
         src: '.'
         sync_host_ip: 'auto'
         sync_strategy: 'unison'
         notify_terminal: true
  </code></pre>
  
  <p>environment files:</p>
  
  <pre><code>web_env:
  DATABASE_HOST=db
  RAILS_ENV=development
  
  db_env:
  MYSQL_DATABASE=development_exp
  MYSQL_ROOT_PASSWORD=root
  MYSQL_USER=dev
  MYSQL_PASSWORD=dev
  </code></pre>
  
  <p>and my database config is as follows:</p>
  
  <pre><code>development:
    adapter: mysql2
    encoding: utf8
    database: &lt;%= ENV['MYSQL_DATABASE'] %&gt;
    username: &lt;%= ENV['MYSQL_USER'] %&gt;
    password: &lt;%= ENV['MYSQL_PASSWORD'] %&gt;
    host: &lt;%= ENV['DATABASE_HOST'] %&gt;
    reconnect: true
    pool: 5
  </code></pre>
  
  <p>when I run docker-sync-stack start I get the following error after the images are build.</p>
  
  <pre><code>web_1  | Mysql2::Error::ConnectionError: Can't connect to MySQL server on 'db' (111 "Connection refused")
  </code></pre>
  
  <p>if I stop the containers and run it again, its working, why is it failing first time ?</p>