title: Connecting to MySQL from Flask Application using docker-compose
body: <p>I have an application using Flask and MySQL. The application does not connect to MySQL container from the Flask Application but it can be accessed using Sequel Pro with the same credentials.</p>
  
  <p><strong>Docker Compose File</strong></p>
  
  <pre><code>version: '2' 
  services:
    web:
      build: flask-app
      ports:
       - "5000:5000"
      volumes:
       - .:/code
    mysql:
      build: mysql-server
      environment:
        MYSQL_DATABASE: test
        MYSQL_ROOT_PASSWORD: root
        MYSQL_ROOT_HOST: 0.0.0.0
        MYSQL_USER: testing
        MYSQL_PASSWORD: testing
      ports:
        - "3306:3306"
  </code></pre>
  
  <p><strong>Docker file for MySQL</strong></p>
  
  <p>The docker file for MySQL will add schema from <code>test.dump</code> file.</p>
  
  <pre><code>FROM mysql/mysql-server
  ADD test.sql  /docker-entrypoint-initdb.d
  </code></pre>
  
  <p><strong>Docker file for Flask</strong></p>
  
  <pre><code>FROM python:latest
  COPY . /app
  WORKDIR /app
  RUN pip install -r requirements.txt
  ENTRYPOINT ["python"]
  CMD ["app.py"]
  </code></pre>
  
  <p><strong>Starting point <code>app.py</code></strong></p>
  
  <pre><code>from flask import Flask, request, jsonify, Response
  import json
  import mysql.connector
  from flask_cors import CORS, cross_origin
  
  app = Flask(__name__)
  
  def getMysqlConnection():
      return mysql.connector.connect(user='testing', host='0.0.0.0', port='3306', password='testing', database='test')
  
  @app.route("/")
  def hello():
      return "Flask inside Docker!!"
  
  @app.route('/api/getMonths', methods=['GET'])
  @cross_origin() # allow all origins all methods.
  def get_months():
      db = getMysqlConnection()
      print(db)
      try:
          sqlstr = "SELECT * from retail_table"
          print(sqlstr)
          cur = db.cursor()
          cur.execute(sqlstr)
          output_json = cur.fetchall()
      except Exception as e:
          print("Error in SQL:\n", e)
      finally:
          db.close()
      return jsonify(results=output_json)
  
  if __name__ == "__main__":
      app.run(debug=True,host='0.0.0.0')
  </code></pre>
  
  <p>When I do a GET request on <code>http://localhost:5000/</code> using REST Client I get a valid response. </p>
  
  <p>A GET request on <code>http://localhost:5000/api/getMonths</code> gives error message:</p>
  
  <pre><code>mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '0.0.0.0:3306' (111 Connection refused)
  </code></pre>
  
  <p>When the same credentials were used on Sequel Pro, I was able to access the database.
  <a href="https://i.stack.imgur.com/LdPon.png" rel="noreferrer"><img src="https://i.stack.imgur.com/LdPon.png" alt="enter image description here"></a></p>
  
  <p>Please advice me on  how to connect the MySQL container from the Flask Application. This is my first time suing Docker and do forgive me if this is a silly mistake from my part.</p>