19

I have mysql running on my localhost I can connect it by running:

mysql -h 127.0.0.1 -P 3306 -u root -p

I also ran docker container with command:

docker run -tid -v $(pwd):/code -p 3306:3306 -p 5000:5000 --name container container

And I want to access my Mysql db from docker container. So I also type from docker container:

mysql -h 127.0.0.1 -P 3306 -u root -p

But it gives me error:

ERROR 2003 (HY000): Can't connect to MySQL server on '127.0.0.1' (111)

What am I doing wrong? Ports seems to be correct. EDIT 1 Output of ifconfig in docker:

eth0      Link encap:Ethernet  HWaddr 02:42:ac:11:00:02  
          inet addr:172.17.0.2  Bcast:0.0.0.0  Mask:255.255.0.0
          inet6 addr: fe80::42:acff:fe11:2/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:1780 errors:0 dropped:0 overruns:0 frame:0
          TX packets:977 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:2225781 (2.2 MB)  TX bytes:56572 (56.5 KB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:28 errors:0 dropped:0 overruns:0 frame:0
          TX packets:28 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1 
          RX bytes:1400 (1.4 KB)  TX bytes:1400 (1.4 KB)
simhumileco
  • 31,877
  • 16
  • 137
  • 115
Snobby
  • 1,067
  • 3
  • 18
  • 38
  • 4
    When you do this: `mysql -h 127.0.0.1 -P 3306 -u root -p`, you're telling docker to connect to itself. You may try to find out the ip of your host in docker interface with `ifconfig` (probably something like 172.17.0.1). Then you connect with `mysql -h 172.17.0.1 -P 3306 -u root -p`. – mrlew Jan 27 '17 at 15:21
  • 2
    Also, you must allow mysqld to listen to other interfaces (seting to 0.0.0.0 will listen all, for instance). By default, it listens only 127.0.0.1. – mrlew Jan 27 '17 at 15:24
  • Are you using the default [mysql](https://hub.docker.com/_/mysql/) container? – Salem Jan 27 '17 at 15:29
  • `ifconfig` says: 172.17.0.2 But It also gives error: `ERROR 2003 (HY000): Can't connect to MySQL server on '172.17.0.2' (111)` I haven't understood about allowing mysql to listen to other interfaces. Can you explain? – Snobby Jan 27 '17 at 15:30
  • You must find your host ip, so, you should find run `ifconfig` from the **host** (it'll be 172.17.0.1, probably). – mrlew Jan 27 '17 at 15:33
  • 2
    You should change the key `bind-address` in `/etc/mysql/mysql.conf.d/mysqld.cnf` or `/etc/mysql/my.cnf`(the file depends on your mysql version) to allow mysql listen on other interfaces. If you change to `bind-address = 0.0.0.0` you'll allow all, including from outside your network. Then, you might be able to connect **from your container** with `mysql -h 172.17.0.1 -P 3306 -u root -p` – mrlew Jan 27 '17 at 15:38
  • Sry, don't forget to restart you server. – mrlew Jan 27 '17 at 15:43
  • Possible duplicate of [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) – krlmlr Sep 24 '17 at 13:36

2 Answers2

9

Even if you configure MySQL to listen on all interfaces, and then from your container access MySQL from a non-loopback IP, you may find that Docker’s routing, NAT, and firewall rules do not allow you to access services running on the host. The fast workaround for this is to run your container on the host network stack with:

docker run -tid -v $(pwd):/code -p 3306:3306 -p 5000:5000 \
  --name container --net host container

You can also also move MySQL inside a container running on the same Docker network, and then access it via the container name using Docker’s DNS service discovery.

Rory O'Kane
  • 29,210
  • 11
  • 96
  • 131
BMitch
  • 231,797
  • 42
  • 475
  • 450
4

Use the default route. For example:

~# ip route show | grep "default" | awk '{print $3}'
172.18.0.1

then

mysql -h 172.18.0.1 -P 3306 -u root -p

if you need to automate that, let's say, use that IP on a shell script, send it to a shell script, take it as:

host=$(ip route show | grep \"default\" | awk '{print $3}' | xargs echo -n)

then you have the host ip addr on $host

Webert Lima
  • 13,197
  • 1
  • 11
  • 24
  • The output differs. Ifconfig says that default ip is 172.17.0.1. So then the error is: `ERROR 2013 (HY000): Lost connection to MySQL server at 'reading initial communication packet', system error: 0` – Snobby Jan 30 '17 at 07:50
  • it's normal for the output to be different because mine was just an example. Your output is the real one. But I don't know about that error. Sorry. – Webert Lima Feb 01 '17 at 16:44