title: Rails on docker: Mysql2::Error::ConnectionError: Unknown MySQL server host 'db'
body: <h1>Rails and Docker</h1>
  
  <p><strong>TL;DR</strong>: I try to orchestrate my rails app using mysql with docker-compose. I have one container running a mysql server and another one using passenger to serve my application. When building I get an unknown Host error.</p>
  
  <hr>
  
  <p>Hello,</p>
  
  <p>I want to deploy my Ruby on Rails app with docker-compose. Since I wanted to use <a href="https://www.phusionpassenger.com/" rel="nofollow noreferrer">Passenger</a> as the application server I thought it would also be a good idea to use their <a href="https://github.com/phusion/baseimage-docker" rel="nofollow noreferrer">baseimage</a>. The application is using mysql as its database. The application works fine in development. When trying to build the image using <code>docker-compose build</code> I get the following error:</p>
  
  <pre><code>** Invoke db:migrate (first_time)
  ** Invoke db:load_config (first_time)
  ** Invoke environment (first_time)
  ** Execute environment
  ** Execute db:load_config
  ** Execute db:migrate
  rake aborted!
  Mysql2::Error::ConnectionError: Unknown MySQL server host 'db' (11)
  /var/www/school/code/vendor/bundle/ruby/2.6.0/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `
  ...
  </code></pre>
  
  <p>I don't quite understand why it's throwing this error since I thought when I use depends_on it will wait for the other container to be started.</p>
  
  <p>I am still learning. Both rails and docker. Here are my compose and database.yml files. If there is anything more to add please let me know. Thanks in advance!</p>
  
  <pre><code># docker-compose.yml
  
  # Using version 2 because of portainer where its supposed to run later on
  version: "2.4"
  
  services:
    db:
      image: mysql:5.7
      volumes:
        - db_data:/var/lib/mysql
      environment:
        MYSQL_ROOT_PASSWORD: password
        MYSQL_DATABASE: school
      ports:
        - "3306:3306"
  
    app:
      build: 
        context: .
        dockerfile: ./docker/passenger.Dockerfile
      volumes:
        - .:/var/www/school
      ports:
        - "80:80"
      depends_on:
        - db
  
  volumes:
    db_data: {}
  </code></pre>
  
  <p>And here also my database.yml</p>
  
  <pre><code># database.yml
  
  default: &amp;default
    adapter: mysql2
    encoding: utf8
    reconnect: false
    pool: &lt;%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %&gt;
    timeout: 5000
  
  production:
    &lt;&lt;: *default
    username: root
    password: "password"
    database: school
    port: 3306
    host: db
  </code></pre>