ggeise
ggeise

Reputation: 497

Connecting to mongo docker container from host

I'm running Docker on OS X with:

docker run --name mongo -p 27017:27017 -v ./data/db:/data/db -d mongo mongod

and using the ip I get from:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' <cid>

in:

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var url = 'mongodb://<ip>:27017';

MongoClient.connect(url, function(err, db) {
   assert.equal(null, err);
   db.close();
});

and I'm getting a timed out error.

I'm using the official mongo repository from Docker Hub. Is there any additional setup that I need to do in order to connect from the host?

Upvotes: 41

Views: 77563

Answers (4)

Ismail
Ismail

Reputation: 1316

Using docker-compose, same can be achieved as below

version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example
      ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/

The connection string is as below, On Host -

const uri = "mongodb://root:example@localhost:27017/

On Container -

const url = 'mongodb://root:example@mongo:27017'

Port mapping can be configured to use a custom host port [8000] like - 8000:27017

Upvotes: 2

Rolly
Rolly

Reputation: 3375

Another option for anyone who use docker-compose

version: '3.1'
services:
  mongo:
    image: mongo
    container_name: "mongo"
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    volumes:
      - './dockervolume/mongodb:/data/db'
    ports:
      - 27017:27017

And u can connect using the url string

    MongoClient.connect("mongodb://root:example@localhost:27017")
        .then(()=>{
            console.log("db connect success");
        })
        .catch((err)=>{
            throw err
        });

Upvotes: 10

fullstacklife
fullstacklife

Reputation: 2061

If you use a user defined network you should be able to pick it up without linking or specifying 27017

const MONGO_NAME_STR = "mongodb://" + "your_docker_container_name";
var db = {};

mongo_client.connect(MONGO_NAME_STR, function(err, _db){
  //some err handling
  db = _db;
});

Upvotes: 8

Adrian Mouat
Adrian Mouat

Reputation: 46480

Is the node.js code being run from a container or from the host?

If it's on the host, just use the localhost address i.e:

var url = 'mongodb://localhost:27017';

This will work because you published the port with -p 27017:27017.

If the code is running inside a container, it would be best to rewrite it to use links and referring to the mongo container by name e.g:

var url = 'mongodb://mongo:27017';

Then when you launch the container with the Node.js code, you can just do something like:

docker run -d --link mongo:mongo my_container

Docker will then add an entry to /etc/hosts inside the container so that the name mongo resolves to the IP of the mongo container.

Upvotes: 85

Related Questions