title: Connect to remote MySQL db from docker container
body: <p>I'm working to containerize a Django 1.5.x application that connects to a MySQL DB on a separate server via ODBC:</p>
  
  <pre><code>[mysql_default]
  database = DB_NAME
  driver = /usr/lib64/libmyodbc5.so
  server = REMOTE_DB_SERVER
  user = DB_USER
  password = DB_USER_PWD
  port = 3306
  </code></pre>
  
  <p>I'm able to run the Django app on my local machine (outside docker) with a connection to the remote DB via port forwarding &amp; SSH:</p>
  
  <pre><code> ssh -L 3307:127.0.0.1:3306 MYID@REMOTE_DB_SERVER
  </code></pre>
  
  <p>I've set up a Docker container for the app using Centos 6.x, but can't get the MySQL connection working. The container has MySQL installed and the mysqld running.</p>
  
  <p>My docker-compose.yml file looks like this:</p>
  
  <pre><code>version: "2"
  services:
    web:
      build: .
      image: MY_IMAGE
      container_name: MY_CONTAINER
      network_mode: "host"
      ports:
        - "3307:3306"
      command: /bin/bash
  </code></pre>
  
  <p>With the container running, I can execute the following command (outside the container) to show databases on the remote DB:</p>
  
  <pre><code>docker exec MY_CONTAINER echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 127.0.0.1 --port=3307
  </code></pre>
  
  <p>But from inside the container the same command fails:</p>
  
  <pre><code>    echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 127.0.0.1 --port=3306
  
  ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)
  </code></pre>