这很奇怪。我目前正在使用带有 Docker 和 Docker-Compose 的 Rails 5.1.5。我正在连接到远程 MySQL(它有防火墙,访问权限有限。不,数据库不在 docker 容器内;它在自己的服务器上运行)。我能够运行 rails db:migrate
并且架构已成功创建。
但是,当我尝试导航到具有数据库调用的站点部分时,它显示:
We're sorry, but something went wrong.
我继续并启用 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 默认回到其原始配置。
有什么想法吗?谢谢:)
最佳答案
虽然我还没有完全解决问题(还不能连接到数据库)这似乎与 Nginx 和 config.force_ssl
in production.rb
显然,我在 Nginx 配置中有一个错误。 I was missing在配置文件中设置 X-Forwarded-Proto https
header 。这导致了无限重定向(老实说,我不知道为什么它们在前一天不存在……我认为这是因为我浏览器中的 cookie)。
这样做之后,Rails 就可以正确使用我的配置了。我仍然需要找出问题所在(这似乎是防火墙问题)。
关于mysql - 未知的 MySQL 服务器主机 'db' Rails 和 Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49412098/