Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connecting to local mongodb from docker container

I have a local mongoDB server running on mongodb://127.0.0.1:27017. My DB name is localv2. I have a node/express app with the Dockerfile as follows:

FROM node:7.5  RUN npm install -g pm2  RUN mkdir -p /usr/src/app WORKDIR /usr/src/app  COPY package.json /usr/src/app RUN npm install  COPY . /usr/src/app  EXPOSE 3002  ENV NODE_ENV local  CMD pm2 start --no-daemon server.js 

The server.js file has a connection to local mongodb with the following code:

app.db = mongoose.connect("mongodb://127.0.0.1:27017/localv2", options); 

This doesn't work when I spin up a container from the image created using the Dockerfile above. I read somewhere that Docker creates a VLAN with a gatway IP address of its own. When I docker inspect my container, my gateway IP address: 172.17.0.1.

Even on changing the mongodb connection to

app.db = mongoose.connect("mongodb://172.17.0.1:27017/localv2", options) 

and re-building the image and starting a new container, I still get the error:

MongoError: failed to connect to server [172.17.0.1:27017] on first connect [MongoError: connect ECONNREFUSED 172.17.0.1:27017] 

Command to run the container: docker run -p 3002:3002 image-name

Please help.

like image 266
chaudharyp Avatar asked May 05 '17 08:05

chaudharyp


People also ask

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.

How do I connect to a local database in MongoDB?

To connect to your local MongoDB, you set Hostname to localhost and Port to 27017 . These values are the default for all local MongoDB connections (unless you changed them). Press connect, and you should see the databases in your local MongoDB.

How do I access MongoDB containers?

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.


2 Answers

On Docker for Mac, you can use host.docker.internal if your mongo is running on your localhost. You could have your code read in an env variable for the mongo host and set it in the Dockerfile like so:

ENV MONGO_HOST "host.docker.internal"

See here for more details on https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

like image 52
airpower44 Avatar answered Sep 28 '22 04:09

airpower44


Adding @vlad-holubiev as answer here because it has worked for me and help users to find it.

Using the network host option on docker run, as specified in the docs:

With the network set to host a container will share the host’s network stack and all interfaces from the host will be available to the container. The container’s hostname will match the hostname on the host system.

docker run -d -e ROOT_URL=http://localhost -e MONGO_URL=mongodb://localhost:27017 --network="host" 
like image 44
elpddev Avatar answered Sep 28 '22 04:09

elpddev