mongodb连接被拒绝docker-compose [英] mongodb connection refused docker-compose

查看:222
本文介绍了mongodb连接被拒绝docker-compose的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dockerfile包含以下内容:-

The dockerfile consists as:-

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  

并且docker-compose.yml文件包含以下内容:-

And the docker-compose.yml file contains as:-

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:  

当我执行 docker-compose build时,所有东西都会正确构建;当我执行 docker-compose up时,应用程序启动,我能够访问在VM主机上的air_scout容器中运行的应用程序,但可以在数据库访问应用程序上访问该应用程序。页面,我收到以下错误消息:

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. page I get error as:

air_scout_1 | [fe9cdec8-36e4-4974-aef3-18b1e73ea030] [DEBUG] MONGODB |连接被拒绝-127.0的connect(2)。 0.1:27017。

"air_scout_1 | [fe9cdec8-36e4-4974-aef3-18b1e73ea030] [DEBUG] MONGODB | Connection refused - connect(2) for 127.0.0.1:27017".

在mongoid.yml文件中,我进行了如下配置:

In my mongoid.yml file I have did config like below:

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

实际上是个问题,从昨天开始我真是在头。谢谢。

What actually is the issue, I'm really banging my head since yesterday. Thankyou.

执行 docker inspect CID会给出JSON数据,其中Networsetting下的Ipaddress为为空。如何访问此空IP。

Doing "docker inspect CID" gives the json data in which the Ipaddress under Networsetting is "" empty. How can I access this empty IP.

编辑:-mongoid.yml文件中的内容为:-

- contents in mongoid.yml file is as:-

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


推荐答案

您必须从air_scout容器连接到mongodb:27017而不是127.0.0.1:27017。

You have to connect to mongodb:27017 instead of 127.0.0.1:27017 from your air_scout container.

air_scout容器的链接将为redis和mongodb容器创建/ etc / hosts条目。可以使用这些名称访问链接的服务。

The links for the air_scout container will create /etc/hosts entries for the redis and mongodb containers. The linked services will be reachable under those names.

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

您确实设置了环境变量DATABASE_URL的正确值为mongodb:27017,但错误消息仍然包含127.0.0.1:27017。因此,它尝试连接那里而不是mongodb:27017。我没有使用过Mongoid的经验,但我想您应该只将DATABASE_URL保留下来。如果那行不通,请使用mongodb:27017

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. So it's trying to connect there instead of mongodb:27017. I have no experience with mongoid but I am guessing you should only leave the line with DATABASE_URL. If that does not work, use mongodb:27017

如果您想查看运行情况,将docker exec插入正在运行的air_scout容器中

If you want to see what is going, docker exec into your running air_scout container

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

这篇关于mongodb连接被拒绝docker-compose的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆