5

This is strange. I'm currently using Rails 5.1.5 with Docker and Docker-Compose. I am connecting to a remote MySQL (which is firewalled, and has limited access to. No, the database is not inside a docker container; it runs in its own server). I was able to run rails db:migrate and the schema was successfully created.

But, when I try to navigate to the part of the site which has a database call, it displays:

We're sorry, but something went wrong.

I went ahead and enabled STOUT logs to check for everything that was happening. It seems that there is a part in which it says:

Mysql2::Error (Unknown MySQL server host 'db'.  (-2));

enter image description here

Note that 'db' is the host for my development environment. The production environment is another one.

I don't think this is a Docker problem (although I could be wrong)

This is the current database.yml:

default: &default
  adapter: mysql2
  pool: 5
  encoding: utf8
  database: <%= Rails.application.secrets.mysql_database %>
  username: <%= Rails.application.secrets.mysql_username %>
  password: <%= Rails.application.secrets.mysql_password %>
  host:  <%= Rails.application.secrets.mysql_host %>
  port: 3306
development: *default
test:
  <<: *default
  database: db/test.sqlite3
production: *default

The current secrets.yml is as follows:

development:
  secret_key_base: the_secret_key_base
  mysql_database: <%= ENV["SECRET_MYSQL_DATABASE"] %>
  mysql_username: <%= ENV["SECRET_MYSQL_USERNAME"] %>
  mysql_password: <%= ENV["SECRET_MYSQL_PASSWORD"] %>
  mysql_host: <%= ENV['SECRET_MYSQL_HOST'] %>

I am currently using

config.read_encrypted_secrets = true

And the encrypted secrets.yml.enc is: enter image description here

This is the Docker-Compose file I'm currently using:

version: '3.2'
services:
  app:
    image: jja3324/ntid:cprintservicehub_app
    restart: always
    environment:
      RAILS_ENV: production
      # What this is going to do is that all the logging is going to be printed into the console. 
      # Use this with caution as it can become very verbose and hard to read.
      # This can then be read by using docker-compose logs app.
      RAILS_LOG_TO_STDOUT: 'true'
    # The first command, the remove part, what it does is that it eliminates a file that 
    # tells rails and puma that an instance is running. This was causing issues, 
    # https://github.com/docker/compose/issues/1393
    command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -e production -p 5000 -b '0.0.0.0'"
    volumes:
      - /var/www/cprint
    ports:
      - "5000:5000"
    expose:
      - "5000"
  # Uses Nginx as a web server (Access everything through http://localhost)
  # https://stackoverflow.com/questions/30652299/having-docker-access-external-files
  web:
    image: jja3324/ntid:cprintservicehub_web
    restart: always
    links:
      - app
    volumes:
      - type: bind
        source: /path-to/ssl/certs
        target: /path-to/ssl/certs
      - type: bind
        source: /path-to-private-ssl/private/
        target: /path-to-private-ssl/private
    links:
      - app
    ports:
      - "80:80"
      - "443:443"

Reading this answer tells me that Rails couldn't resolve the name for the MySQL server. I think this translates to Rails defaulting back to its original config.

Any ideas? Thanks :)

1
  • 1
    Another interesting detail, if I run the console, and I call User.count, I'm able to see a result. Commented Mar 21, 2018 at 20:23

1 Answer 1

1

While I haven't solved entirely the problem (Can't connect to the Database yet) it seems this had something to do with Nginx, and config.force_sslin production.rb

Apparently, I had a bug in the Nginx configuration. I was missing setting the X-Forwarded-Proto https header in the configuration file. This was causing infinite redirects (which I honestly don't know why they weren't present the day before... I think it was because of the cookies in my browser).

After doing that, Rails is correctly using my configuration. I still have to figure out what's the problem (which seems to be a firewall issue).

Sign up to request clarification or add additional context in comments.

1 Comment

There are 2 things in programming that makes my brain hurt: #1) When you have this "bug" or "problem" that just goes on and on for days, and you can't solve it..... #2) When things work, and you don't know why it's working, but it's working..... #2 just happened :/ I don't know if I should be excited, scared, or whatever...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.