title: Connection refused [Errno 111] when trying to access to MongoDB with Docker
body: <p>i'm new to docker, i have a simple Flask program that prints data from MongoDB to an HTML page. When i execute the app using Python it works like a charm. However, using Docker i get the message :</p>
  
  <pre><code>pymongo.errors.ServerSelectionTimeoutError: 0.0.0.0:27017: [Errno 111] Connection refused
  </code></pre>
  
  <p>I've tried changing :</p>
  
  <pre><code>db = MongoClient('mongodb:27017').mydatabase
  </code></pre>
  
  <p>to one of these </p>
  
  <pre><code>db = MongoClient('mongodb://mongodb:27017').mydatabase
  db = MongoClient('localhost:27017').mydatabase
  db = MongoClient('0.0.0.0:27017').mydatabase
  </code></pre>
  
  <p>and nothing worked.</p>
  
  <p>My app.py file:</p>
  
  <pre class="lang-py prettyprint-override"><code>from flask import Flask, render_template
  import database.config as db_conf
  from base64 import b64encode
  from altair import Chart
  from pymongo import MongoClient
  import os
  app = Flask(__name__)
  db = MongoClient('mongodb:27017/').mydatabase
  
  @app.route('/', methods=['GET'])
  def get_all_images():
      pictures = db.pictures
      output = []
      html_body = "&lt;ul&gt;"
      collection = pictures.find()
      if collection:
          for q in collection:
              html_body += " &lt;li&gt;&lt;a href="+q['md5']+"&gt;"+q['md5']+"&lt;/a&gt;&lt;/li&gt;"
  
              output.append({'md5': q['md5'], 'md5': q['md5'], 'height': q['height'], 'width': q['width'],
                             'timestamp': q['timestamp']})
          html_body += "&lt;/ul&gt;"
      else:
          html_body = "&lt;p&gt;No images on database&lt;/p&gt;"
      return html_body.format()
  if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0')
  </code></pre>
  
  <p>My docker-compose.yml file:</p>
  
  <pre><code>version: '2'
  services:
    app:
      build:
        context: ./
        dockerfile: ./DockerFile
      ports:
       - "5000:5000"
      links:
       - mongo
      depends_on:
       - mongo
      volumes:
       - .:/code
    mongo:
      image: "mongo:latest"
      ports:
       - "27017:27017"
      command: mongod --port 27017 --bind_ip 0.0.0.0
  </code></pre>
  
  <p>My Docker file:</p>
  
  <pre><code>FROM python:3.6
  WORKDIR /app
  COPY . /app
  RUN pip install --trusted-host pypi.python.org -r requirements.txt
  CMD ["python", "app.py"]
  </code></pre>
  
  <p>I executed my script on WINDOWS 10 using : </p>
  
  <pre><code>docker-compose build
  docker-compose up
  </code></pre>