mongodb connection refused docker-compose

2020-07-11 08:22发布

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  

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:  

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 | Connection refused - connect(2) for 127.0.0.1:27017".

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.

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

EDIT:- 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

1条回答
疯言疯语
2楼-- · 2020-07-11 08:57

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

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.

See 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. 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

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
查看更多
登录 后发表回答