1

I have deployed a docker container on a digital ocean droplet (Ubuntu 16.04) that should connect to a JAR on door 9000 and to Mongo on door 27017 in the same machine. Both the services (the JAR and Mongo) are not containers and run on localhost.

I am able to connect to the JAR without problems, but every time I try to connect to mongo I end up in this:

pymongo.errors.ServerSelectionTimeoutError: 178.128.206.98:27017: timed out

This is what I did so far. The Jar started working when I added the container IP to the ufw rules:

sudo ufw allow from DOCKER_IP to any port 9000

So I tried the same for mongo:

sudo ufw allow from DOCKER_IP to any port 27017

But once again, I end up in a timeout error. Therefore I followed some guidelines as:

1) drivers issue. Followed by timeout issue I added the srv with dns package, but still did not work.

2) So I followed a networking issue and I commented out the door from mongod.conf:

sudo nano /etc/mongod.conf

That now looks like this:

# network interfaces
net:
  port: 27017
# bindIp: 127.0.0.1

Of course I have restarted the mongo service

sudo service mongod restart

But still same timeout error.

These are all the variants of python code I have tried reading here and there:

client = MongoClient('mongodb+srv://MACHINE_IP:27017/') #first variant
client = MongoClient(MACHINE_IP, 27017) #second variant
client = MongoClient('mongodb://mongo:27017/') #third variant

Are there any other suggestions on how to connect to mongo? And at the same time, why I am able to connect to the JAR but not to mongo?

Maroun
  • 94,125
  • 30
  • 188
  • 241
Mazzespazze
  • 111
  • 12

2 Answers2

0

I will not suggest to allowing access of Mongo DB from outside of the network, also will not suggest accessing the Mongo from your app using the public IP of the machine, better to access using the private IP.

client = MongoClient('mongodb+srv://PRIVATE_IP:27017/') #first variant
client = MongoClient(PRIVATE_IP, 27017) #second variant

For the third variend, it will only work if both container part of same network or same docker-compose file or you link app and mongo container.

client = MongoClient('mongodb://mongo:27017/') #third variant

This variation is best in case of service to service communication as there will low latency if we compared with Machine IP that will route from the internet, also for this approach you do need to expose or allow port in UFW.

Now the last question, How you are running both containers? is the mongo db running inside the container?

You can debug using

docker exec -it mongo_container_id bash

then run mongo cli to check connectivity.

Adiii
  • 54,482
  • 7
  • 145
  • 148
  • So mongo is not running on a container. And the reason is that I would like mongo to be accessed from different servers as a shared db. This is why I was using the MACHINE_IP instead of another container. Same for the JAR that should be able to be called by several docker images without having a the same JAR application deployed in each container. – Mazzespazze Oct 23 '19 at 10:23
  • but in the same machine you can docker-compose network or private it, for the rest of machine you can public – Adiii Oct 23 '19 at 10:27
  • When I type "mongo" on the terminal I connect without issues and it returns `connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb` Sorry I am new to docker and I did not really understand your second message. I know I can link/compose containers together. Are you suggesting to run a mongo container and link it to my first container and then opening it to my network? But then isn't that the same thing of accessing mongo without a container? :) – Mazzespazze Oct 23 '19 at 10:35
  • 1
    with docker-compose network both your app and mongo will connect each other using the third varient that we mentioned, for external access you need to publish port as well in the docker-compose. yes link them using same network it will work fine in same machine. – Adiii Oct 23 '19 at 10:37
  • Do you suggest to do the same also for the JAR? I mean to have everything connected in one single container and eventually, when I will need more servers just to open a door and connect to docker directly? – Mazzespazze Oct 23 '19 at 10:42
  • yes have single docker-compose in this machine, and publish port for the other container. – Adiii Oct 23 '19 at 13:01
0

Your mongod.conf looks incorrect for your use case.

Try setting net to:

net:
   port: 27017
   bindIpAll: true

then restart the mongod service and retry.

Belly Buster
  • 8,224
  • 2
  • 7
  • 20