Docker rails mongodb NoServerAvailable

Docker rails mongodb NoServerAvailable

我正在构建一个包含 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.

上使用 Docker 原生的 beta 版本
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

我的seeds.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 是正常的,因为对于测试版,它位于 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 本机并使用 boot2docker 时,它正在运行。

有没有人知道问题出在哪里??

您需要使用

- db:27017

mongoid.yml 的主机配置中,其中 "db" 是 docker-compose.yml 中 mongo 服务的名称。