title: Connecting SQL Server on Docker to Python
body: <p>I am trying to perform a table creation using pyodbc on a SQL Server 2017 database hosted using Docker. I'm also using a network so that I can connect to it later from another Docker image. However, I get the following error</p>
  
  <pre><code>pyodbc.OperationalError: ('HYT00', '[HYT00] [Microsoft][ODBC Driver 17 for SQL Server]Login timeout expired (0) (SQLDriverConnect)')
  </code></pre>
  
  <p>This is how I went about creating the connection.</p>
  
  <p>To create and run the DB server,</p>
  
  <pre><code>docker run --name mssqldocker -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=&lt;password&gt;' -e 'MSSQL_PID=Express' -p 7000:7000 --network=lambda-local-mssql -v &lt;my_path&gt; -d mcr.microsoft.com/mssql/server:2017-latest-ubuntu
  </code></pre>
  
  <p>I also tried adding </p>
  
  <pre><code>-h "mssqldocker"
  </code></pre>
  
  <p>to the command for running the Docker image and then using "mssqldocker" instead of localhost, but to no avail, since mismatched hostnames seem to be the recurring theme when using DBs and Docker together. Also tried adding in <code>\sqlexpress</code> without effect as well. The Python code is as follows</p>
  
  <pre><code>import pyodbc
  import sql_clauses
  from settings import ENDPOINT, PORT, USERNAME, PASSWORD
  
  cnxn = pyodbc.connect(
         'DRIVER={ODBC Driver 17 for SQL Server}' + 
         ';SERVER=' + ENDPOINT + ';UID=' + USERNAME + 
         ';PWD=' + PASSWORD)
  
  cursor = db.cursor()
  cursor.execute(create_database(dbname))
  cnxn.commit()
  cnxn.close()
  print("Database created")
  </code></pre>
  
  <p>The settings file is as follows</p>
  
  <pre><code>ENDPOINT="localhost"
  PORT = 7000
  USERNAME="SA"
  PASSWORD=&lt;password&gt;
  </code></pre>