Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker rails mongodb NoServerAvailable

I am building a project with 3 docker.

  • API with Rails
  • Website angular / webpack
  • DB with mongodb

So i have a docker-compose to handle everything.

My problem is when i want to seed my database, i have an error :

Mongo::Error::NoServerAvailable: No server is available matching  preference: #<Mongo::ServerSelector::Primary:0x47121755943460 tag_sets=[] server_selection_timeout=30 local_threshold=0.015>

I tried many thing, i am currently using the beta version of Docker native on OSX.

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

My 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

And my mongoid.yml It's normal i use localhost because with the beta version, it's on localhost. when i tried with docker-machine, i changed it to the 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

Once my docker-compose up -d i try to rake with docker-compose run api rake db:seed

All the containers a running, and i can access to it from my navigator without a problem.

When i uninstall docker native and use boot2docker, it's working.

Did anyone have an idea where the problem is from ??

like image 909
albttx Avatar asked May 27 '16 14:05

albttx


1 Answers

You need to use

- db:27017

in your hosts configuration of the mongoid.yml, where "db" is the name of your mongo service in the docker-compose.yml.

like image 144
Maxim Zubarev Avatar answered Oct 15 '22 06:10

Maxim Zubarev