通过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
您的rails应用程序尝试连接的主机是什么? – R0MANARMY
localhost:27017 –
[使用Docker的可能的重复我得到错误:“SQLSTATE \ [HY000 \] \ [2002 \]没有这样的文件或目录”](http://stackoverflow.com/questions/40075065/using- docker -i-get-the-error-sqlstatehy000-2002-no-such-file-or-directory) – R0MANARMY