title: Docker container not able to connect to remote MongoDB
body: <p>I have a <code>flask</code> based python code which simply connects to <code>mongodb</code>.It has two routes <code>Get</code> <code>Post</code>. <code>Get</code> simply prints <code>hello world</code> and using <code>Post</code> we can post any json data which is later saved in <code>MongoDB</code> This python code is working fine. <code>MongoDB</code> is hosted on cloud. </p>
  
  <p>I have now created a Dockerfile:</p>
  
  <pre><code>FROM tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7
  
  RUN pip3 install pymongo
  
  ENV LISTEN_PORT=8000
  EXPOSE 8000
  
  COPY /app /app
  </code></pre>
  
  <p>Using command to run </p>
  
  <p><code>docker run --rm -it -p 8000:8000 myflaskimage</code></p>
  
  <p>After starting the container for this docker image, I am getting response of <code>GET</code> but no response from <code>POST</code>. I am using <code>Postman</code> software to post json data. I get below error:</p>
  
  <pre><code>pymongo.errors.ServerSelectionTimeoutError: No servers found yet
  </code></pre>
  
  <p>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 <code>Dockerfile</code> to enable connections to <code>MongoDB</code>.</p>
  
  <p>Please help. Thanks</p>
  
  <p>Python Code:</p>
  
  <pre><code>from flask import Flask, request
  from pymongo import MongoClient
  
  app = Flask(__name__)
  
  def connect_db():
      try:
          client = MongoClient(&lt;mongodbURL&gt;)
          return client.get_database(&lt;DBname&gt;)
  
      except Exception as e:
          print(e)
  
  
  def main():
      db = connect_db()
      collection = db.get_collection('&lt;collectionName&gt;')
  
      @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()
  </code></pre>