title: Can't access a MySQL DB from Python within the same Docker container
body: <p>I'm relatively new to Docker so bear with me. I have a Python webapp and a MySQL DB running in the same Docker container.</p>
  
  <pre><code>$ vi Dockerfile
  FROM mysql
  
  ENV MYSQL_HOST=127.0.0.1 \
      MYSQL_ROOT_PASSWORD=pass
  
  ADD testDB.sql /docker-entrypoint-initdb.d
  EXPOSE 3306
  
  FROM python:3.4
  
  RUN pip install Flask
  RUN pip install flask_cors
  RUN pip install mysql-connector==2.1.6
  
  COPY . /app
  WORKDIR /app
  
  ENTRYPOINT ["python"]
  CMD ["Api.py"]
  </code></pre>
  
  <p>Here's a snippet from the API which should connect to the DB:</p>
  
  <pre><code>d_host='127.0.0.1'
  d_user='root'
  d_password='pass'
  d_database='testDB'
  
  @webapp.route('/testGet')
  def testGet():
      import mysql.connector
      conn = mysql.connector.connect(host=d_host,
                         user=d_user, 
                         password=d_password, 
                         database=d_database, )
      cursor = conn.cursor()
      SQL = """SELECT text FROM testing ORDER BY testID DESC LIMIT 1;"""
      cursor.execute( SQL )
      c = cursor.fetchone()
      conn.commit()
      cursor.close()
      conn.close() 
      return c[0]
  </code></pre>
  
  <p>However, I keep getting the following error: 
  <code>mysql.connector.errors.InterfaceError: 2003: Can't connect to MySQL server on '127.0.0.1:3306' (111 Connection refused)</code></p>
  
  <p>Any help/ideas are greatly appreciated.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>MySQL wasn't running. Now it is running, but I'm still getting the same error. Additionally, my .sql dump file is not being imported. The updated code:</p>
  
  <pre><code>$ vi Dockerfile:
  FROM mysql
  ADD dump.sql /docker-entrypoint-initdb.d
  
  FROM python:3.4
  
  RUN pip install Flask
  RUN pip install flask_cors
  RUN pip install mysql-connector==2.1.6
  
  COPY . /app
  WORKDIR /app
  
  ENTRYPOINT ["python"]
  CMD ["Api.py"]
  </code></pre>
  
  <p>I feel like I should be importing the MySQL in this file though:</p>
  
  <pre><code>$ vi docker-compose.yml
  version: '2.1'
  services:
      web:
          build: .
          ports:
              - "5000:5000"
          links:
              - mysql
          container_name: flask_app
  
      mysql:
          image: mysql
          container_name: db
          environment:
              - MYSQL_ROOT_PASSWORD=root
          ports:
              - "3306:3306"
  </code></pre>
  
  <p>The current code is based on this question: <a href="https://stackoverflow.com/questions/36617682/docker-compose-mysql-import-sql">Docker Compose mysql import .sql</a></p>