2

So, I'm able to generally contact my localhost through Docker by running a container with --add-host=localbox:192.168.59.3. ping localbox works just fine. Problem is, I can't seem to be able to even get a response from MySQL Server. mysql -h localbox, which works fine from outside of the docker container, just gets me ERROR 2003 (HY000): Can't connect to MySQL server on 'localbox' (111) from within.

I've done GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;

I've added bind-address = 0.0.0.0 into /etc/my.cnf. None of this helps. What gives?

Context: I'm running all of this through boot2docker on OS X Yosemite.

Bryan
  • 11,398
  • 3
  • 53
  • 78
Eli
  • 36,793
  • 40
  • 144
  • 207
  • It seems that localbox is not recognized as a valid host name. What does it happen if you use the IP address instead of localbox from within the container? – mgaido Dec 23 '14 at 09:26
  • localbox is a valid hostname. Like I said, pinging localbox works fine. – Eli Dec 23 '14 at 09:37
  • Do you mean pinging it from within the container? – mgaido Dec 23 '14 at 09:38
  • Yes. Everything I mention aside from running the container and modifying mysql server configs, I'm doing from within the container. – Eli Dec 23 '14 at 09:52
  • What do you get running `netstat`? Is MySQL listening? – mgaido Dec 23 '14 at 09:58
  • Yes. Through `lsof -i 3306`, I see `localhost:mysql (LISTEN)` (on localhost) – Eli Dec 23 '14 at 10:08
  • What about `mysql -h localhost`? Does it work? Which command do you use to run MySQL? – mgaido Dec 23 '14 at 10:17
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/67525/discussion-between-eli-and-mark91). – Eli Dec 23 '14 at 10:18

2 Answers2

4

So, turns out this is homebrew's fault with a really questionable design decision. You start-up mysql-server in homebrew by running the recommended launchctl load -w ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist. But then, when examining this file, you'll find the bind-address is hardcoded!

  <array>
    <string>/usr/local/opt/mysql/bin/mysqld_safe</string>
    <string>--bind-address=127.0.0.1</string>
    <string>--datadir=/usr/local/var/mysql</string>
  </array>

So, no matter what you do in any of your my.cnf files, it will always be bound to 127.0.0.1, and you'll never be able to query from a container. My fix is just editing this file directly not to provide a bind address so we can let /etc/my.cnf do it for us. Alternatively, though I wouldn't recommend it, you can just change the bind-address directly in this file.

Eli
  • 36,793
  • 40
  • 144
  • 207
1

You can add the mac host's network to your docker container (running in boot2docker) with

--add-host=dh:10.0.2.2

as part of your docker run command or using

extra_hosts:
  - "dh:10.0.2.2"

in your docker-compose.yml file.

After this, connecting to mysql from a container with the client installed is as simple as (e.g.):

$ mysql -h dh -uroot -p1234

This solution does not require modifying the settings for your mariadb/mysql installation, even if it is bound to 127.0.0.0.

untitled90
  • 98
  • 5
  • 2
    Thanks to http://stackoverflow.com/questions/30495905/accessing-host-machine-as-localhost-from-a-docker-container-thats-also-inside-a and https://forums.docker.com/t/access-host-not-vm-from-inside-container/11747/4 for guiding me to this answer. – untitled90 Jan 12 '17 at 17:51