Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker container not able to connect to remote MongoDB

I have a flask based python code which simply connects to mongodb.It has two routes Get Post. Get simply prints hello world and using Post we can post any json data which is later saved in MongoDB This python code is working fine. MongoDB is hosted on cloud.

I have now created a Dockerfile:

FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7

RUN pip3 install pymongo

ENV LISTEN_PORT=8000
EXPOSE 8000

COPY /app /app

Using command to run

docker run --rm -it -p 8000:8000 myflaskimage

After starting the container for this docker image, I am getting response of GET but no response from POST. I am using Postman software to post json data. I get below error:

pymongo.errors.ServerSelectionTimeoutError: No servers found yet

I am bit confused as to why the python code is working fine but when I put the same in docker and start container, it throws error. Do we have to include anything in Dockerfile to enable connections to MongoDB.

Please help. Thanks

Python Code:

from flask import Flask, request
from pymongo import MongoClient

app = Flask(__name__)

def connect_db():
    try:
        client = MongoClient(<mongodbURL>)
        return client.get_database(<DBname>)

    except Exception as e:
        print(e)


def main():
    db = connect_db()
    collection = db.get_collection('<collectionName>')

    @app.route('/data', methods=['POST'])
    def data():
        j_data = request.get_json()
        x = collection.insert_one(j_data).inserted_id
        return "Data added successfully"

    @app.route('/')
    def hello_world():
        return "Hello World"

main()

if __name__ == '__main__':
   app.run()
like image 365
S Andrew Avatar asked Aug 21 '18 08:08

S Andrew


People also ask

How do I connect to a MongoDB Docker container?

For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.

Why is my MongoDB not connecting?

If you have created a user and are having trouble authenticating, try the following: Check that you are using the correct username and password for your database user, and that you are connecting to the correct database deployment. Check that you are specifying the correct authSource database in your connection string.

Can I use MongoDB in Docker?

MongoDB can be run in a Docker container. There is an official image available on Docker Hub containing the MongoDB community edition, used in development environments. For production, you may custom-build a container with MongoDB's enterprise version.


1 Answers

You probably don't have internet connection from the container. I had a similar issue when connecting from containerized java application to public web service.

At first I would try to restart docker:

systemctl restart docker

If it does not help then look into resolv.conf in you container:

docker run --rm myflaskimage cat /etc/resolv.conf

If it shows nameserver 127.x.x.x then you can try:

1) on the host system comment dns=dnsmasq line in /etc/NetworkManager/NetworkManager.conf file with a # and restart NetworkManager using systemctl restart network-manager

2) or explicitly set DNS for docker adding this into the /etc/docker/daemon.json file and restarting the docker:

{
    "dns": ["my.dns.server"]
}
like image 154
cgrim Avatar answered Oct 21 '22 08:10

cgrim