44

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.

6
  • How do you run your mongo? Is it in docker as well? Commented May 5, 2017 at 8:34
  • no. it is running as a standalone server. Commented May 5, 2017 at 8:35
  • 6
    docs.docker.com/engine/reference/run/#network-host Commented May 5, 2017 at 8:39
  • try found out your PC local IP address not a container adress and add it to the mongo connection config Commented May 5, 2017 at 8:45
  • @VladHolubiev that worked. thanks! Commented May 5, 2017 at 9:30

5 Answers 5

39

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

Sign up to request clarification or add additional context in comments.

1 Comment

I prefer this solution to all others
22

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"

5 Comments

trying to solve same problem, did not understand how you used this: you used this code to run your container?
Getting following error : node: bad option: --network=host ! What do I do?
@jackOfAll, did you use windows/mac OS?
@AbhishekPankar Mac
Network host works only on linux machine. Check it here docs.docker.com/network/host
14

A docker container is a separate from your computer,
So you will not be able to connect to your localhost.
You should run the mongo server on a container and the connect with the image name.

mongodb://CONTAINER-NAME:port

5 Comments

This is a far better answer.
It all depends if you exposed the port. If you exposed the internal docker container port you can connect using localhost.
the docker container is separate but connected, so it is possible to connect to your local machine from inside the container you just need to figure out what the local machines ip address is on the docker network, this was easier beforethey broke the local network dns integration (~ version 19.0) when you could specify the local machines name and the dns would autolookup
How to specify in docker-compose file for external mongo database connection..
Of course, you can connect to localhost. Based on what source, you affirmed this is not possible?
4

Change the mongoose connect url to "mongodb://host.docker.internal:27017/db-name", you will be able to access the mongoDB from the container. Refrence https://docs.docker.com/desktop/networking/#use-cases-and-workarounds

2 Comments

Thanks this worked on windows. I don't why it has no upvotes. A thing to notice, the doc you linked points out that it should only be used for development purposes and does not work in a production environment.
I was so confused until I arrived at this. If your mongo container name is mongodb_container, Name your URI as such without the port number: mongoose.connect(mongodb://mongodb_container/mydatabase, {});
0

Create .env file such as:

DB_HOST=myMongoServiceIp
DB_USER=myMongoDBUser
DB_PORT=myMongoServicePort
DB_PASSWORD=myMongoDBPort
DB_AUTH_NAME=myMongoDBAuthDatabase
DB_CONNECT_NAME=myStartDBConnection

If you have already a MongoDB Service in your local machine just replace the DB_HOST to host.docker.internal. This is special DNS name only, which resolves to the internal IP address used by the host. Of course, you need to replace the DB_HOST with the proper IP of the Server when running in Prod Mode.

Finally, the Localhost example .env:

DB_HOST=host.docker.internal
DB_USER=app1
DB_PORT=27017
DB_PASSWORD=123
DB_AUTH_NAME=admin
DB_CONNECT_NAME=test

If your app was written in with Mongoose, for example, the mongodb connection string might be something like:

var mongoConnectionString = `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}@${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_CONNECT_NAME}?authSource=${process.env.DB_AUTH_NAME}`

Ultimately, to run the docker container now, all you need to do is add your .env file on it, such as:

docker run --env-file .\src\.env --name MyContainer -dp 8080:8080 <myUser>/<repo>:<tagname>

Then you have a container using your local MongoDB instead of another container.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.