Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown MySQL server host 'db' Rails and Docker

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 :)

like image 927
Jose A Avatar asked Nov 07 '22 09:11

Jose A


1 Answers

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).

like image 82
Jose A Avatar answered Nov 10 '22 00:11

Jose A