26

I'm working to containerize a Django 1.5.x application that connects to a MySQL DB on a separate server via ODBC:

[mysql_default]
database = DB_NAME
driver = /usr/lib64/libmyodbc5.so
server = REMOTE_DB_SERVER
user = DB_USER
password = DB_USER_PWD
port = 3306

I'm able to run the Django app on my local machine (outside docker) with a connection to the remote DB via port forwarding & SSH:

 ssh -L 3307:127.0.0.1:3306 MYID@REMOTE_DB_SERVER

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.

My docker-compose.yml file looks like this:

version: "2"
services:
  web:
    build: .
    image: MY_IMAGE
    container_name: MY_CONTAINER
    network_mode: "host"
    ports:
      - "3307:3306"
    command: /bin/bash

With the container running, I can execute the following command (outside the container) to show databases on the remote DB:

docker exec MY_CONTAINER echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 127.0.0.1 --port=3307

But from inside the container the same command fails:

    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)
Brendenw
  • 785
  • 1
  • 6
  • 22
  • You can probably make the SSH tunnel to listen on the docker0 interface (172.17.0.1) or all interfaces (0.0.0.0, notice the risk) by `ssh -L 172.17.0.1:3307:127.0.0.1:3306 ...` (not tested). The other possibility is to establish an SSH tunnel from within the container. – Xiongbing Jin Apr 02 '16 at 01:24
  • What about using a socket to connect to mysql and running the "django" container with `-v /var/run/mysqld:/var/run/mysqld` – Vassilis Jan 09 '21 at 01:46

3 Answers3

12

The Docker works like a virtual machine. It has a local storage and a local environment. When you connect to 127.0.0.1 from the Docker it tries to connect to this Docker (not to local machine where the Docker was runned) because the localhost for the Docker is the Docker.

Please, read the following answer:

From inside of a Docker container, how do I connect to the localhost of the machine?

Danil Valov
  • 605
  • 1
  • 7
  • 16
  • 10
    Saying "Docker is a virtual machine" maybe can occur in misunderstands because Docker doesn't have the same structure as a definitely Virtual Machine. I think to say "Docker runs like a Virtual Machine" more adequate. – Marcos Freitas Sep 16 '17 at 16:35
6

I solved this by using the docker host address instead of '127.0.0.1' for queries from within the container:

echo "show databases" | mysql -u DB_USER -pDB_USER_PWD -h 10.0.2.2 --port=3306

Because Docker host ip can vary, this post describes steps to get the right address:

How to get the IP address of the docker host from inside a docker container

Community
  • 1
  • 1
Brendenw
  • 785
  • 1
  • 6
  • 22
0

I've created a bridge network:

$ docker network create -d bridge mynetwork

After, see the IP and port

$ docker inspect mysql
...
"Ports": {
    "3306/tcp": [
        {
            "HostIp": "0.0.0.0",
            "HostPort": "32031"
        }
    ],
    "33060/tcp": null
}
...

Connect with 0.0.0.0:32031

Solved by Basit

dixoen
  • 355
  • 4
  • 12