9

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 '[email protected]'

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?

8
  • @Tamas, yes I have done this in my Docker.local.yaml file, I'll add it to the description Commented Jun 12, 2017 at 20:34
  • sorry I meant services: mongo: image: 'mongo:3.5' ports: '27018:27017'. Commented Jun 12, 2017 at 20:37
  • @Tamas I would like to run at port 27018, because I want to run a different mongo instance per microservice Commented Jun 12, 2017 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. Commented Jun 12, 2017 at 20:40
  • @Tamas Added it, but I get the same result... Commented Jun 12, 2017 at 20:41

2 Answers 2

12

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
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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)
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.
1

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

1 Comment

Links are considered deprecated. Use docker's DNS and networks to connect containers together as Robert suggests.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.