title: Docker + Rails + MySQL = Ignoring environment variables in database.yml
body: <p>I'm newbie using docker and my issue is to load a Rails 4 application using MySQL and Docker with environment variables. </p>
  
  <p>Basically, I build the app and run it. In another terminal I create mysql database and user/password to be used by the database.yml. Here are the steps that I run:</p>
  
  <pre><code>$ docker-compose build
  $ docker-compose up -d
  $ docker exec -it poseidon_db_1 bash -l
  #root@dockerimg mysql -uroot -proot
  mysql&gt; create database poseidon_development;
  mysql&gt; CREATE USER 'poseidon_user'@'localhost' IDENTIFIED BY 'poseidon_password';
  mysql&gt; CREATE USER 'poseidon_user'@'%' IDENTIFIED BY 'poseidon_password';
  mysql&gt; GRANT ALL ON poseidon_development.* TO 'poseidon_user'@'localhost';
  mysql&gt; GRANT ALL ON poseidon_development.* TO 'poseidon_user'@'%';
  mysql&gt; FLUSH PRIVILEGES;
  mysql&gt; exit
  #root@dockerimg exit
  $ docker-compose run web bundle exec rake db:migrate
  </code></pre>
  
  <p>database.yml</p>
  
  <pre><code>default: &amp;default
    adapter: mysql2
    encoding: utf8
    pool: 5
    username: &lt;%= ENV["DATABASE_USERNAME"] %&gt;
    password: &lt;%= ENV["DATABASE_PASSWORD"] %&gt;
    database: &lt;%= ENV["DATABASE_NAME"] %&gt;
    host: db
  
  development:
    &lt;&lt;: *default
  
  test:
    &lt;&lt;: *default
  </code></pre>
  
  <p>docker-compose.yml</p>
  
  <pre><code>version: '3.6'
  
  services:
    db:
      restart: always
      image: mysql:5.6.34
      ports:
        - "3306:3306"
      volumes:
        - dbdata
      env_file:
        - .env
  
    dbdata:
      image: tianon/true
      volumes:
        - /var/lib/mysql
  
    web:
      build: .
      ports:
        - '3000:3000'
      volumes:
        - webdata
      links:
        - "db"
      env_file:
        - .env
  
    webdata:
      image: tianon/true
      volumes:
        - ".:/var/www/poseidon"
  </code></pre>
  
  <p>Dockerfile</p>
  
  <pre><code>FROM ruby:2.3.7
  
  ENV LANG C.UTF-8
  
  RUN apt-get update -qq
  RUN apt-get install -y build-essential mysql-client vim
  
  WORKDIR /tmp
  ADD Gemfile Gemfile
  ADD Gemfile.lock Gemfile.lock
  RUN bundle install
  
  ENV APP_ROOT /var/www/poseidon
  
  RUN mkdir -p $APP_ROOT
  WORKDIR $APP_ROOT
  COPY . $APP_ROOT
  RUN chmod a+x .env
  
  EXPOSE 3000
  
  ENTRYPOINT ["bundle", "exec"]
  
  CMD ["rails", "server", "-p", "3000", "-b", "0.0.0.0"]
  </code></pre>
  
  <p>.env</p>
  
  <pre><code>SECRET_KEY_BASE=xxxxxxxx
  RAILS_ENV=development
  RAILS_SERVE_STATIC_FILES=true
  URL_OPTIONS=domain.com
  RECAPTCHA_SECRET_KEY=xxxxxxx
  RECAPTCHA_SITE_KEY=xxxxxxx
  MAILGUN_API=xxxxxx
  MAILGUN_DOMAIN=xxxxxxx
  MYSQL_ROOT_PASSWORD=root
  DATABASE_USERNAME=poseidon_user
  DATABASE_PASSWORD=poseidon_password
  DATABASE_NAME=poseidon_development
  </code></pre>
  
  <p>The problem is, when accessing <code>http://127.0.0.1</code>, I receive <code>Access denied for user '&lt;%= ENV["DATABASE_USERNAME"] %&gt;'@'IP' (using password: YES)</code></p>
  
  <p>I don't know what to do anymore, it seems to not load the env vars correctly. </p>
  
  <p>Any help will be really appreciated!</p>
  
  <p>Regards</p>