简体   繁体   English

mongodb连接拒绝了docker-compose

[英]mongodb connection refused docker-compose

The dockerfile consists as:- dockerfile包括: -

FROM ruby:2.2.3-slim

MAINTAINER Milan Rawal <milan@gmail.com>

RUN apt-get update && apt-get install -qq -y build-essential nodejs libmagickcore-dev imagemagick libmagickwand-dev libxml2-dev libxslt1-dev git-core curl htop --fix-missing --no-install-recommends

ENV INSTALL_PATH /air_scout
RUN mkdir -p $INSTALL_PATH

WORKDIR $INSTALL_PATH

COPY Gemfile Gemfile
RUN bundle install

COPY . .

RUN bundle exec rake RAILS_ENV=production   SECRET_TOKEN=f6801f0744ff2e86b0baa542fc55075b5b79c922c518e34484cfe6a6c2149510973fa6c90d36c05907f8bf9114b6b33594f3630810b332ef3717b8b8f4f04b1f assets:precompile

VOLUME ["$INSTALL_PATH/public"]

CMD bundle exec unicorn -c config/unicorn.rb  

And the docker-compose.yml file contains as:- docker-compose.yml文件包含: -

version: '2'
services:
  mongodb:
    image: mongo:latest
    ports:
      - '27017:27017'
    volumes:
      - air_scout-mongodb:/data/db
  redis:
    image: redis:3.0.5
    ports:
  - '6379:6379'
    volumes:
      - air_scout-redis:/var/lib/redis
  air_scout:
    build: .
    ports:
      - '8000:8000'
    environment:
      - DATABASE_URL=mongodb:27017
    links:
      - mongodb
      - redis
    env_file:
      - .air_scout.env
  resque:
    build: .
    environment:
      - QUEUE=*
      - DATABASE_URL=mongodb:27017
    links:
      - mongodb
      - redis
    command: bundle exec rake environment resque:work
    env_file:
      - .air_scout.env
volumes:
  air_scout-redis:
  air_scout-mongodb:  

When I do "docker-compose build" every thing build properly and when I do "docker-compose up" then the app boots and I'm able to access the app running in air_scout container on vm host, but on db access app. 当我做“docker-compose build”时,每个东西都正确构建,当我做“docker-compose up”时,应用程序启动,我就可以访问在vm主机上的air_scout容器中运行的应用程序,但是在db access app上。 page I get error as: 页面我收到错误:

"air_scout_1 | [fe9cdec8-36e4-4974-aef3-18b1e73ea030] [DEBUG] MONGODB | Connection refused - connect(2) for 127.0.0.1:27017". “air_scout_1 | [fe9cdec8-36e4-4974-aef3-18b1e73ea030] [DEBUG] MONGODB |连接被拒绝 - 连接(2)为127.0.0.1:27017”。

In my mongoid.yml file I have did config like below: 在我的mongoid.yml文件中,我做了如下配置:

hosts:
    - localhost:27017
    - <%= ENV['DATABASE_URL'] %>  

What actually is the issue, I'm really banging my head since yesterday. 究竟是什么问题,我从昨天开始就真的很开心。 Thankyou. 谢谢。

Doing "docker inspect CID" gives the json data in which the Ipaddress under Networsetting is "" empty. 执行“docker inspect CID”会给出jwson数据,其中Networsetting下的Ipaddress为“空”。 How can I access this empty IP. 如何访问此空IP。

EDIT:- contents in mongoid.yml file is as:- 编辑: - mongoid.yml文件中的内容如下: -

production:
  clients:
    default:
      database: air_scout_test
      hosts:
        - localhost:27017
        - <%= ENV['DATABASE_URL'] %>
      options:
        max_pool_size: 1
  options:
     raise_not_found_error: false

You have to connect to mongodb:27017 instead of 127.0.0.1:27017 from your air_scout container. 您必须从air_scout容器连接到mongodb:27017而不是127.0.0.1:27017。

The links for the air_scout container will create /etc/hosts entries for the redis and mongodb containers. air_scout容器的链接将为redis和mongodb容器创建/ etc / hosts条目。 The linked services will be reachable under those names. 链接的服务可以在这些名称下访问。

See https://docs.docker.com/compose/compose-file/#/links 请参阅https://docs.docker.com/compose/compose-file/#/links

You do set an environment variable DATABASE_URL with the correct value mongodb:27017, but the error message still contains 127.0.0.1:27017. 您使用正确的值mongodb:27017设置环境变量DATABASE_URL,但错误消息仍包含127.0.0.1:27017。 So it's trying to connect there instead of mongodb:27017. 所以它试图连接那里而不是mongodb:27017。 I have no experience with mongoid but I am guessing you should only leave the line with DATABASE_URL. 我没有mongoid的经验,但我猜你应该只留下DATABASE_URL。 If that does not work, use mongodb:27017 如果这不起作用,请使用mongodb:27017

If you want to see what is going, docker exec into your running air_scout container 如果你想知道发生了什么,docker exec进入你正在运行的air_scout容器

docker ps
# take the exact name of the air_scout container in the output
# i am guessing compose_air_scout but i might be off
docker exec -ti compose_air_scout /bin/bash
cat /etc/hosts
ping mongodb
# maybe try an interactive mongodb client

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM