Connecting to mongo docker container from host
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;
});
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.