I'm new to Docker, and I'm trying the simplest of setups with docker-compose, but don't succeed to connect to Mongodb.

My docker-compose.local.yaml file:

version: "2"
services:
  posts-api:
    build:
      dockerfile: Dockerfile.local
      context: ./
    volumes:
      - ".:/app"
    ports:
      - "6820:6820"
    depends_on:
      - mongodb
  mongodb:
    image: mongo:3.5
    ports:
      - "27018:27018"
    command: mongod --port 27018

My Docker file:

FROM node:7.8.0
MAINTAINER Livefeed 'project.livefeed@gmail.com'

RUN mkdir /app
VOLUME /app
WORKDIR /app

ADD package.json yarn.lock ./
RUN eval rm -rf node_modules && \
yarn

ADD server.js .
RUN mkdir config src
ADD config config/
ADD src src/

EXPOSE 6820
EXPOSE 27018

CMD yarn run local

In server.js I try to connect with:

mongoose.connect('mongodb://localhost:27018');

I also tried:

mongoose.connect('mongodb://mongodb:27018');

To run docker-compose:

docker-compose -f docker-compose.local.yaml up --build 

And I receive the error:

connection error: { MongoError: failed to connect to server [localhost:27018] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27018]

What am I missing?

share|improve this question
    
@Tamas, yes I have done this in my Docker.local.yaml file, I'll add it to the description – Kim Gysen Jun 12 at 20:34
    
sorry I meant services: mongo: image: 'mongo:3.5' ports: '27018:27017'. – Tamas Jun 12 at 20:37
    
@Tamas I would like to run at port 27018, because I want to run a different mongo instance per microservice – Kim Gysen Jun 12 at 20:39
    
But you'd still need to link the mongodb port to another port. If you want to change the default listening port of mongodb, you need to tell it so by 'command: mongod --port 27018' when you define your 'mongodb' service. – Tamas Jun 12 at 20:40
    
@Tamas Added it, but I get the same result... – Kim Gysen Jun 12 at 20:41
up vote 3 down vote accepted

In server.js use mongodb instead of localhost:

mongoose.connect('mongodb://mongodb:27018');

Because containers in the same network can communicate using their service name.

Bear in mind that each container and your host have their own localhost. Each localhost is a different host: container A, container B, your host (each one has its own network interface).


Edit:

Be sure to get your mongo up:

docker-compose logs mongodb
docker-compose ps

Sometimes it doesn't get up because of disk space.


Edit 2:

With newer versions of mongo, you need to specify to listen to all interfaces too:

command: mongod --port 27018 --bind_ip_all
share|improve this answer
    
I've tried this, but unfortunately, with the exact same result... But with the depends_on, or links, shouldn't it work? I'll update the question details. – Kim Gysen Jun 12 at 21:02
1  
Indeed, you don't need both. With version: "2" or 3, you get your containers in a docker network so they are able to connect using their service names. (links and depends_on add dependency information only) – Robert Jun 12 at 21:03
    
I've added some information – Robert Jun 12 at 21:11
    
when I try this, it says: Can't find a suitable configuration file in this directory or any parent. Are you in the right directory? I use a file named `docker-compose.local.yaml', according to the environment (I use config.js). Docker ps -a shows mongo:3.5 being up 6 minutes. Strange. – Kim Gysen Jun 12 at 21:13
1  
Awesome! Thanks! – Kim Gysen Jun 12 at 21:26

I think, that you should add links option in your config. Like this:

ports:
  - "6820:6820"
depends_on:
  - mongodb
links:
  - mongodb

update

As I promised

version: '2.1'
services:
    pm2:
      image: keymetrics/pm2-docker-alpine:6
      restart: always
      container_name: pm2
      volumes:
        - ./pm2:/app
      links:
        - redis_db
        - db
      environment:
        REDIS_CONNECTION_STRING: redis://redis_db:6379

    nginx:
      image: firesh/nginx-lua
      restart: always
      volumes:
        - ./nginx:/etc/nginx
        - /var/run/docker.sock:/tmp/docker.sock:ro
      ports:
        - 80:80
      links:
        - pm2

        s3: # mock for development
      image: lphoward/fake-s3:latest

    redis_db:
      container_name: redis_db
      image: redis
      ports:
        - 6379:6379

    db: # for scorebig-syncer
      image: mysql:5.7
      ports:
        - 3306:3306
share|improve this answer
    
That didn't change anything, unfortunately. – Kim Gysen Jun 12 at 20:33
    
In 1h I will be near my computer and share my docker-compose.yaml (that working) – Николай Крещенко Jun 12 at 20:36
    
Would be cool, thanks... – Kim Gysen Jun 12 at 20:36
3  
Links are considered deprecated. Use docker's DNS and networks to connect containers together as Robert suggests. – BMitch Jun 12 at 21:04

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.