这很奇怪。我目前正在使用Rails 5.1.5与Docker和Docker-Compose。我正在连接到一个远程MySQL(这是一个防火墙,并且访问权限有限。不,数据库在docker容器中不;它在自己的服务器中运行)。我能够运行rails db:migrate
并且架构已成功创建。
但是,当我尝试导航到具有数据库调用的站点部分时,它会显示:
我们很抱歉,但出了点问题。
我继续并启用STOUT日志来检查发生的所有事情。它似乎有一个部分:
Mysql2::Error (Unknown MySQL server host 'db'. (-2));
请注意' db'是我的开发环境的主机。生产环境是另一个。
我不认为这是Docker问题(尽管我可能错了)
这是当前的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
目前的secrets.yml如下:
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'] %>
我目前正在使用
config.read_encrypted_secrets = true
这是我目前正在使用的Docker-Compose文件:
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"
阅读this answer告诉我Rails无法解析MySQL服务器的名称。我认为这转换为Rails默认返回其原始配置。
有什么想法吗?谢谢:))
答案 0 :(得分:1)
虽然我还没有完全解决问题(无法连接到数据库),但这似乎与Nginx和生产中的config.force_ssl
有关.rb
显然,我在Nginx配置中遇到了一个错误。 I was missing在配置文件中设置X-Forwarded-Proto https
标头。这导致无限重定向(我真的不知道为什么他们前一天没有出现......我认为这是因为我浏览器中的cookie)。
执行此操作后,Rails正确使用我的配置。我仍然需要弄清楚问题是什么(这似乎是一个防火墙问题)。