docker does not connect with local mongo

1

I have a basic node container

ROM node:boron
RUN npm install
EXPOSE 1337
CMD ["npm","start"]

but when you launch it, try to connect with mongodb://localhost:27017/my-bd which is a local mongo on my mac ...

This generates the following error:

 Unable to ensure uniqueness for usernames:  MongoNetworkError: 
failed to connect to server [localhost:27017] on first connect 
[MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

Any ideas?

    
asked by Pablo Cegarra 25.03.2018 в 13:08
source

1 answer

2

Try deploying your container with the --netwok host option, or, instead of telling it to connect to localhost: 27017, that connects to your host's ip.

This is because when you deploy your container you will have your own ip unless you tell it otherwise.

The machine where you have the docker engine (and mongo) will have an ip X, while the container will have an ip Y. The container when you tell it to connect to localhost will look in the ip And, when it should look in X.

My advice is that you modify the connector so that it connects to an address called mongohost (for example) and when you run the container you do it in the following way:

run -d --add-host mongohost:"IP-Host-de-mongo" nombre-imagen

With this you can have the mongo server on any host that you specify to the container when you set it up.

I hope it helps you.

    
answered by 30.05.2018 / 10:25
source