Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 795
ggeise Avatar asked Oct 26 '15 00:10

ggeise


People also ask

How does MongoDB run in Docker container from host?

You can connect to MongoDB on localhost:27017 . Then use the following command to open the MongoDB shell. I have used mymongo as an arbitrary container name, though you can replace mymongo with test-mongo or any other container name of your choosing. The show dbs command will display all your existing databases.

How can I access a MongoDB container from another container?

If you need to access the MongoDB server from another application running locally, you will need to expose a port using the -p argument. Using this method, you will be able to connect to your MongoDB instance on mongodb://localhost:27017 . You can try it with Compass, MongoDB's GUI to visualize and analyze your data.

Can you ssh into a running Docker container?

The SSH method works fine for Docker containers, too. That said, you can SSH into a Docker container using Docker's built-in docker exec . If you do not need an interactive shell, you can also use the docker attach command to connect the host's stdin and stdout to the running container and execute remote commands.


2 Answers

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.

like image 171
Adrian Mouat Avatar answered Oct 09 '22 15:10

Adrian Mouat


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; }); 
like image 27
fullstacklife Avatar answered Oct 09 '22 16:10

fullstacklife