title: Python/Docker - Unknown server host 'db' when migrating
body: <p>I am trying to migrate a program using <code>python manage.py db migrate</code> in my Visual Studio terminal but when I run it I am getting an error named <code>(2005, &quot;Unknown server host 'db' (11001)&quot;)</code>. I am also using the SQLAlchemy library as well to use it alongside flask to create my app. I have found some solutions on StackOverflow and GitHub like trying to recreate my network <a href="https://github.com/docker-library/mysql/issues/644" rel="nofollow noreferrer">https://github.com/docker-library/mysql/issues/644</a> and trying to import MySQLdb and using <code>MySQLdb.connect()</code> <a href="https://stackoverflow.com/questions/47825384/unknown-mysql-server-host-when-using-flask-in-python">&quot;Unknown MySQL server host&quot; when using Flask in Python</a>, but when I try both of these solutions on my program using Visual Studio they don't work.</p>
  <p>Here is the error that I am getting in it's full form:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py&quot;, line 2336, in _wrap_pool_connect
      return fn()
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 304, in unique_connection
      return _ConnectionFairy._checkout(self)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 778, in _checkout
      fairy = _ConnectionRecord.checkout(pool)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 495, in checkout
      rec = pool._do_get()
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\impl.py&quot;, line 241, in _do_get
      return self._create_connection()
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 309, in _create_connection
      return _ConnectionRecord(self)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 440, in __init__
      self.__connect(first_connect_check=True)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 661, in __connect
      pool.logger.debug(&quot;Error on connect(): %s&quot;, e)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\util\langhelpers.py&quot;, line 68, in __exit__
      compat.raise_(
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\util\compat.py&quot;, line 182, in raise_
      raise exception
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\pool\base.py&quot;, line 656, in __connect
      connection = pool._invoke_creator(self)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\strategies.py&quot;, line 114, in connect
      return dialect.connect(*cargs, **cparams)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\default.py&quot;, line 493, in connect
      return self.dbapi.connect(*cargs, **cparams)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\__init__.py&quot;, line 123, in Connect
      return Connection(*args, **kwargs)
    File &quot;C:\Users\trevo\AppData\Local\Programs\Python\Python39\lib\site-packages\MySQLdb\connections.py&quot;, line 185, in __init__
      super().__init__(*args, **kwargs2)
  MySQLdb._exceptions.OperationalError: (2005, &quot;Unknown server host 'db' (11001)&quot;)
  </code></pre>
  <p>Here are my required files respectivly:</p>
  <p>Dockerfile:</p>
  <pre><code>FROM python:3.9
  ENV PYTHONUNBUFFERED 1
  WORKDIR /app
  COPY requirements.txt /app/requirements.txt
  RUN pip install -r requirements.txt
  COPY . /app
  CMD python main.py
  </code></pre>
  <p>requirements.txt:</p>
  <pre><code>Flask==1.1.2
  Flask-SQLAlchemy==2.4.4
  SQLAlchemy==1.3.20
  Flask-Migrate==2.6.0
  Flask-Script==2.0.6
  Flask-Cors==3.0.9
  requests==2.25.0
  werkzeug==1.0.1
  mysqlclient==2.1.0
  pika==1.1.0
  itsdangerous==2.0.1
  wolframalpha==5.0.0
  </code></pre>
  <p>docker-compose.yml:</p>
  <pre><code>version: '3.8'
  services:
    backend:
      build:
        context: .
        dockerfile: Dockerfile
      command: 'python main.py'
      ports:
        - 8001:5000
      volumes:
        - .:/app
      depends_on:
        - db
  
    queue:
      build:
        context: .
        dockerfile: Dockerfile
      command: 'python -u consumer.py'
      depends_on:
        - db
  
    db:
      image: mysql:5.7.22
      restart: always
      environment:
        MYSQL_DATABASE: veganetworkscript
        MYSQL_USER: root
        MYSQL_PASSWORD: root
        MYSQL_ROOT_PASSWORD: root
      volumes:
        - .dbdata:/var/lib/mysql
      ports:
        - 33068:3306
  </code></pre>
  <p>And finally my manage.py file:</p>
  <pre><code>from main import app, db
  from flask_migrate import Migrate, MigrateCommand
  from flask_script import Manager
  
  migrate = Migrate(app, db)
  
  manager = Manager(app)
  manager.add_command('db', MigrateCommand)
  
  if __name__ == '__main__':
      manager.run()
  </code></pre>
  <p>main.py:</p>
  <pre><code>from flask_cors import CORS
  from sqlalchemy import UniqueConstraint
  from vegaaimain.vegamain import main
  from flask import Flask, jsonify
  from flask_sqlalchemy import SQLAlchemy
  from dataclasses import dataclass
  app = Flask(__name__)
  app.config[&quot;SQLALCHEMY_DATABASE_URI&quot;] = 'mysql://root:root@db/veganetworkscript'
  CORS(app)
  
  db = SQLAlchemy(app)
   
  
  class Product(db.Model):
      id: int
      title: str
      image: str
  
      id = db.Column(db.Integer, primary_key=True, autoincrement=False)
      title = db.Column(db.String(200))
      image = db.Column(db.String(200))
  
  
  class VegaScriptRun(db.Model):
      id = db.Column(db.Integer, primary_key=True, autoincrement=False)
      title = db.Column(db.String(200))
      description = db.Column(db.String(400))
      image = db.Column(db.String(200))
      main()
  
  class ProductUser(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      user_id = db.Column(db.Integer)
      product_id = db.Column(db.Integer)
  
      UniqueConstraint('product_id', 'user_id', name='user_product_unique')
  @app.route('/api/products')
  def index():
      return jsonify(Product.query.all())
  
  if __name__ == '__main__':
      app.run(debug=True, host='0.0.0.0')
  </code></pre>
  <p>What exactly am I missing or potentially doing wrong here, thank you!</p>