我正在用 3 个 docker 构建一个项目。
所以我有一个 docker-compose 来处理所有事情。
我的问题是当我想为我的数据库做种时,出现错误:
Mongo::Error::NoServerAvailable: No server is available matching preference: #<Mongo::ServerSelector::Primary:0x47121755943460 tag_sets=[] server_selection_timeout=30 local_threshold=0.015>
我尝试了很多东西,我目前正在 OSX 上使用 Beta 版的 Docker native 。
version: '2'
services:
web:
build: web/
ports:
- "80:8080"
links:
- api
depends_on:
- api
volumes:
- ./web:/app
api:
build: api/
command: rails s -p 3000 -b '0.0.0.0'
volumes:
- ./api:/app
- ./api:/app/tmp/pids
links:
- db
#depends_on:
#- db
ports:
- "3000:3000"
environment:
RAILS_ENV: development
db:
image: mongo:3.2
ports:
- 27017:27017
我的种子.rb
require 'csv'
file = File.read("db/data.csv")
csv = CSV.parse(file, :headers => false, :col_sep => ";")
csv.each do |row|
Datum.create(
:country_code => row[0],
:country => row[1]
)
end
还有我的 mongoid.yml 我使用 localhost 是正常的,因为对于 beta 版本,它在 localhost 上。当我尝试使用 docker-machine 时,我将其更改为 docker ip。
development:
clients:
default:
database: api_development
hosts:
- localhost:27017
options:
options:
test:
clients:
default:
database: api_test
hosts:
- localhost:27017
options:
read:
mode: :primary
max_pool_size: 1
一旦我的 docker-compose up -d 我尝试使用 docker-compose run api rake db:seed
所有容器都在运行,我可以毫无问题地从我的导航器访问它。
当我卸载 docker native 并使用 boot2docker 时,它可以正常工作。
有没有人知道问题出在哪里??
最佳答案
你需要使用
- db:27017
在 mongoid.yml 的主机配置中,其中“db”是 docker-compose.yml 中的 mongo 服务的名称。
关于ruby-on-rails - Docker rails mongodb NoServerAvailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37486544/