title: Docker Compose + Rails: best practice to migrate?
body: <p>I just followed this article on <a href="https://blog.codeship.com/running-rails-development-environment-docker/" rel="noreferrer">Running a Rails Development Environment in Docker</a>. Good article, works great. After setting everything up, I decided to go on and set up a production environment.</p>
  
  <p><strong>GOAL:</strong></p>
  
  <p>I want to <code>rake db:create &amp;&amp; rake db:migrate</code> every time my docker image is run.</p>
  
  <p><strong>PROBLEM:</strong></p>
  
  <p>If I move the database creation and migrations steps...</p>
  
  <pre><code>docker-compose run app rake db:create
  docker-compose run app rake db:migrate
  </code></pre>
  
  <p>...into the Dockerfile...</p>
  
  <pre><code>RUN rake db:create &amp;&amp; rake db:migrate
  </code></pre>
  
  <p>...that will throw an error...</p>
  
  <pre><code>could not translate host name "postgres" to address: Name or service not known
  </code></pre>
  
  <p>...because the <code>host</code> in my <code>database.yml</code>...</p>
  
  <pre><code>default: &amp;default
    adapter: postgresql
    encoding: unicode
    pool: 5
    timeout: 5000
    username: postgres
    host: postgres
    port: 5432
  
  development:
    &lt;&lt;: *default
    database: rails_five_development
  </code></pre>
  
  <p>...is set to the <code>postgres</code> service name specified in my <code>docker-compose.yml</code>...</p>
  
  <pre><code>version: "2"
  services:
    postgres:
      image: postgres:9.5
      ports:
        - "5432"
    app:
      build: .
      command: rails server -p 3000 -b '0.0.0.0'
      volumes:
        - .:/app
      ports:
        - "3000:3000"
      links:
        - postgres
  </code></pre>
  
  <p>...since that is the recommended practice as pointed by the article.</p>
  
  <p><strong>QUESTION:</strong></p>
  
  <p>How can I automatically <code>rake db:create &amp;&amp; rake db:migrate</code> every time my docker image is run?</p>
  
  <p><a href="https://stackoverflow.com/questions/35419949/deploying-rails-app-with-docker-compose">I am trying to achieve the same thing as in this question</a></p>