2016-10-23 81 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后 –

相关问题