mongodb connection refused docker-compose
Asked Answered
B

1

7

The dockerfile consists as:-

FROM ruby:2.2.3-slim

MAINTAINER Milan Rawal <[email protected]>

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
Bebe answered 5/7, 2016 at 9:30 Comment(0)
P
10

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
Pastime answered 5/7, 2016 at 10:12 Comment(6)
Out of luck getting same debug error message as mentioned. But when I ping to mongodb I get as:- 64 bytes from 172.18.0.3: icmp_seq=14 ttl=64 time=0.161 ms. Any guess.Bebe
My guess is there is something wrong with your mongoid.yml - maybe some typo in the path to your hosts config, so the default is used which is localhost. Can you put the whole thing in your question?Pastime
thanks alot for quick response @Juergen Gmeiner, I have edited the question for mongoid.yml file.Bebe
Try removing the localhost:27017 line. If that does not work, verify that your application is in production mode (or also configure for development). If all that does not work, maybe the env-var substitution does not work, so hardcode mongodb:27017Pastime
Also try adding an environment variable RAILS_ENV=production for your air_scout container (like your DATABASE=mongodb:27017 line)Pastime
hardcoding mongodb:27017 worked @Juerge Gmeiner, thanks.Bebe

© 2022 - 2024 — McMap. All rights reserved.