2016-10-23 77 views
1

通過rails控制檯在Model中插入值時出現此錯誤。無法使用Docker將mongodb與Rails容器連接撰寫

"Mongo::Error::NoServerAvailable: No server is available matching preference: # using server_selection_timeout=30 and local_threshold= 0.015 "

這兩個容器都運行良好,但Rails無法連接mongodb。 我只有一個Dockerfile。

我的搬運工,compose.yml文件的內容是:

version: '2' 

services: 
    mongo: 
    image: mongo:3.0 
    command: mongod --smallfiles --quiet 
    environment: 
     - RAILS_ENV=production 
     - RACK_ENV=production 
    ports: 
     - "27017:27017" 

    app: 
    depends_on: 
     - 'mongo' 
     # - 'redis' 
    build: . 
    ports: 
     - '3000:3000' 
    volumes: 
     - '.:/app' 
    command: rails s -b '0.0.0.0' 
    env_file: 
     - '.env' 

volumes: 
    mongo: 

我Dockerfile:

FROM ruby:2.3.0 
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs 

ENV APP_HOME /app 

RUN mkdir $APP_HOME 
WORKDIR $APP_HOME 


ADD Gemfile* $APP_HOME/ 
RUN bundle install 


ADD . $APP_HOME 
+0

您的rails應用程序嘗試連接的主機是什麼? – R0MANARMY

+0

localhost:27017 –

+0

[使用Docker的可能的重複我得到錯誤:「SQLSTATE \ [HY000 \] \ [2002 \]沒有這樣的文件或目錄」](http://stackoverflow.com/questions/40075065/using- docker -i-get-the-error-sqlstatehy000-2002-no-such-file-or-directory) – R0MANARMY

回答

0

你使用蒙戈(同泊塢窗,compose.yml提到的容器名稱)作爲你的主機在mongoid.yml中?

+0

是的,現在工作了,把主機換成mongo後 –

相關問題