简体   繁体   English

Ruby on Rails 无法连接到 Docker 容器中的数据库主机

[英]Ruby on Rails can't connect to database host in Docker container

My Docker setup is listed in this ticket for reference: Ruby on Rails 4.1.8 Gem::LoadError for mysql2 gem此票证中列出了我的 Docker 设置以供参考: Ruby on Rails 4.1.8 Gem::LoadError for mysql2 gem

Building a Ruby Docker image FROM ruby:2.1.3 works fine until I run bundle exec rails server .构建 Ruby Docker 图像FROM ruby:2.1.3工作正常,直到我运行bundle exec rails server At this point an error is thrown:此时会抛出错误:

/usr/local/bundle/gems/mysql2-0.3.20/lib/mysql2/client.rb:70:in `connect': Unknown MySQL server host 'dokku-mariadb-runrite-production' (25) (Mysql2::Error)

The MariaDB instance is a Docker container created using the MariaDB Dokku plugin. MariaDB 实例是使用 MariaDB Dokku 插件创建的 Docker 容器。 The MariaDB instance's image does not have OpenSSL installed but I've deployed more recent stacks using Dokku before and never had to worry about that. MariaDB 实例的映像没有安装 OpenSSL,但我之前使用 Dokku 部署了更新的堆栈,因此不必担心这一点。

What could be going on?会发生什么?

It looks like the hostname cannot be resolved.看起来主机名无法解析。 The likely causes are one of the following:可能的原因是以下之一:

  1. The instance name isn't what you think it is.实例名称不是您想象的那样。 You might need to make sure the --name flag when you started the MariaDB docker instance is correct, or override the --hostname of the MariaDB container to match the name you're referring to in the Ruby container. You might need to make sure the --name flag when you started the MariaDB docker instance is correct, or override the --hostname of the MariaDB container to match the name you're referring to in the Ruby container.
  2. The two containers aren't attached to the same network.这两个容器没有连接到同一个网络。 You can make sure they're both on the same network by specifying it in the --network flag when using docker run.使用 docker 运行时,您可以通过在 --network 标志中指定它们来确保它们都在同一个网络上。

You may also have something unusual related to your DNS resolver setup, but I'm guessing based on this question you haven't done anything that would have altered the defaults, so I'm guessing it's not a factor.您可能还有一些与您的 DNS 解析器设置相关的异常情况,但我猜测基于这个问题您没有做任何会改变默认设置的事情,所以我猜这不是一个因素。

If you're using docker compose or another method of starting the containers, the same issues apply, but docker-compose has configuration options in the YAML for network and hostname, and docker-compose will quietly create a few hostname aliases for each instance that you bring up (usually something like project_containername, containername, and containername-replicanumber). If you're using docker compose or another method of starting the containers, the same issues apply, but docker-compose has configuration options in the YAML for network and hostname, and docker-compose will quietly create a few hostname aliases for each instance that you bring up (通常类似于 project_containername、containername 和 containername-replicanumber)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM