Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails config/database.yml is read from Docker env vars but mysql2 defaults to root and ignores host and other opts #211

Open
uchouhan opened this issue Mar 5, 2018 · 4 comments

Comments

@uchouhan
Copy link

uchouhan commented Mar 5, 2018

I'm running into an issue with the following passenger-docker setup (see below), the rails config/application.rb file does pick up the right env vars passed to the docker run command. However, in the stack trace, mysql2 still tries to connect using root and local socket, ignoring the host, username and password env vars that are passed in

Dockerfile

FROM phusion/passenger-ruby21:0.9.15

ENV HOME /root
CMD ["/sbin/my_init"]

EXPOSE 80

RUN rm -f /etc/service/nginx/down
RUN rm /etc/nginx/sites-enabled/default
ADD nginx.conf /etc/nginx/sites-enabled/appname.conf
ADD rails-env.conf /etc/nginx/main.d/rails-env.conf

WORKDIR /tmp
ADD Gemfile /tmp/
ADD Gemfile.lock /tmp/
RUN bundle install

ADD . /home/app/appname
RUN chown -R app:app /home/app/appname

RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

nginx.conf

server {
  listen 80;
  server_name appname.optimispt.local;
  root /home/app/appname/public;
  passenger_enabled on;
  passenger_user app;
  passenger_ruby /usr/bin/ruby2.1;
}

rails-env.conf

env PASSENGER_APP_ENV;
env RACK_ENV;
env RAILS_ENV;
env SECRET_KEY_BASE;
env DB;
env DB_USER;
env DB_PASS;
env DB_HOST;

The docker command is docker run -d -e PASSENGER_APP_ENV=development -e DB=appname_development -e DB_USER=user -e DB_PASS=pass -e DB_HOST=10.0.2.2 -p 80:80

@uchouhan
Copy link
Author

uchouhan commented Mar 5, 2018

Also, rails console using docker exec works just fine, its just the server that fails trying to connect to the mysql database even though the ENV seems to have the environment variables passed in as docker env vars.

@autotelik
Copy link

Facing the same thing (no connection from browser when using env vars in database.yml) I just came across : https://github.com/phusion/passenger-docker#nginx_env_vars

Seems nginx clears all ENV vars from child processes. So they are preserved when running a local bash/rails console but are missing when going via passenger

@emj-io
Copy link

emj-io commented Nov 7, 2018

I've been trying to find a way around this problem too.

I think you want to use passenger_env_var. You can set env vars in passenger using passenger_env_var VALUE; However, you can't use an env var here. passenger_env_var DB_MASTER_USER FOO $FOO will not work... it will set the value of FOO to literally $FOO.

The combination that seems to work (and feels super hacky to me) is to make a template nginx config file and then use envsubst on it at container start. See docker-library/docs#496 (comment) for an example.

If someone can come up with a better way to do this, I would be very interested.

@Juksefantomet
Copy link

Hello,

This is a tad old, but still a problem to this day. just adding information that Rails in config/database.yml expects a host entry for each environment.

if a host entry is blank or with a <%= ENV['DB_HOSTNAME'] %> it actually defaults to development regardless. You can test this by removing all environment entries and leaving e.g. staging only, then running it with the following config:

staging:
adapter: postgresql
database: database_stage
host: <%= ENV['DB_HOSTNAME'] %>
user: <%= ENV['DB_USERNAME'] %>
password: <%= ENV['DB_PASSWORD'] %>
pool: 5
timeout: 5000

This is at least the case for postgres, with pg gem ~> 1.2.3

and will yield a message stating:

ActiveRecord::AdapterNotSpecified: 'development' database is not configured. Available: ["staging"]

just wanted to add this in case someone was hitting the same issue.

I've been troubleshooting an ENV error on aws + docker + rails which was a direct result of this. Took me 8 hours to properly troubleshoot as i was starting in the AWS pipeline then proceeded to docker and eventually discovered it was Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants