22

This is more a general question for how to connect to local services through Docker. There's a similar question in a Github issue here that doesn't seem to have any resolution. What I'm really looking for is to be able to do development locally against my local development MySQL server, then once I'm ready to deploy, to test locally against a newly created deploy candidate docker image.

Ideally, both get settings from the same place as well, so I could put mysql_server: host_ip. This seems like a typical use case. Is anything like this currently possible?

I'm using Boot2Docker specifically with MySQL server running on my host mac's OS X Yosemite NOT in a container. Would be cool to have a more general answer for future readers though.

halfer
  • 19,824
  • 17
  • 99
  • 186
Eli
  • 36,793
  • 40
  • 144
  • 207

4 Answers4

19

The Docker CLI docs give this solution (which assumes you are running on a Linux host with ):

Sometimes you need to connect to the Docker host from within your container. To enable this, pass the Docker host’s IP address to the container using the --add-host flag. To find the host’s address, use the ip addr show command.

The flags you pass to ip addr show depend on whether you are using IPv4 or IPv6 networking in your containers. Use the following flags for IPv4 address retrieval for a network device named eth0:

$ HOSTIP=`ip -4 addr show scope global dev eth0 | grep inet | awk '{print \$2}' | cut -d / -f 1`
$ docker run  --add-host=docker:${HOSTIP} --rm -it debian

Then the name docker inside the container will map to the host's IP address. For your case, you could use docker run --add-host=mysql_server:$(hostip) ...

If using Boot2Docker, it sets up a mapping to the host at a predefined address, so on that platform the equivalent to the above is just the one command:

$ docker run  --add-host=docker:192.168.59.3 --rm -it debian
Community
  • 1
  • 1
Bryan
  • 11,398
  • 3
  • 53
  • 78
  • 3
    This is great, but as far as I can tell it doesn't work on Mac. The `ip` command isn't installed by default, and I can't find any way of installing it ("ip" is a hard thing to google for). Can you provide more advice around getting this to work on Mac? – Eli Dec 19 '14 at 21:38
  • You have MySQL running on the Mac? Docker is running inside some VM running on the Mac? Could you give some details on your set-up? – Bryan Dec 22 '14 at 11:48
  • 2
    If using Boot2Docker, it sets up the Mac host as 192.168.59.3 – Bryan Dec 22 '14 at 11:58
  • 1
    OK, so I'm able to contact localhost through Docker now (i.e. running with `--add-host=localbox:192.168.59.3`), but MySQL still doesn't support connections: `$ mysql -h localbox ERROR 2003 (HY000): Can't connect to MySQL server on 'localbox' (111)` – Eli Dec 22 '14 at 23:59
  • Hi @Eli I got the same issue as you, have you found a solution for this? – jaycode Apr 16 '15 at 13:10
  • on your mysql did you grant permission that connection? – mpmeyer Apr 16 '15 at 16:11
  • 2
    @jaycode Yup, this was a MySQL Brew issue. Answer here: http://stackoverflow.com/questions/27617343/connecting-to-mysql-server-on-localhost-through-docker – Eli Apr 17 '15 at 04:58
  • @Bryan. Just curious, how did you determine boot2docker sets up the Mac host as 192.168.59.3? – Joseph Siefers Jun 30 '15 at 17:58
  • 1
    @JosephSiefers I do not recall exactly what steps I went through to arrive at that conclusion last year, but it is mentioned as the default config in the [CLI README](https://github.com/boot2docker/boot2docker-cli) and can be seen in the [source code](https://github.com/boot2docker/boot2docker-cli/blob/master/config.go#L121) – Bryan Jul 03 '15 at 09:17
  • `ip route show 0.0.0.0/0` shows the ip of my router, not my ip – maxgalbu Oct 14 '15 at 08:48
  • 2
    @maxgalbu Good point. Docker have changed their advice so I edited my answer. Note it now depends on knowing the name of the network device with the IP address. – Bryan Oct 16 '15 at 09:36
9

To connect local MySQL you can definitely use --network="host" in your docker run command.
Then 127.0.0.1 or localhost in your docker container will point to your docker host.

docker run --network="host" -p 8080:8080 <your-docker-Image>
naib khan
  • 928
  • 9
  • 16
0

To help with several of the additional questions and the main post I would like to link to a repo I have been managing to manage my local development. I have stopped trying to run any service for my development directly on OS X and use Docker containers as they are the exact same running on production and my environments can be matched and streamlined.

This repo consists of a web server, database server and a data container to load the MySQL databases.

I have and will continue to support this repo and have recently upgraded the documentation to make it turn key for other developer.

Docker Repo on GitHub

gegere
  • 69
  • 5
0

On a mac with boot2docker, you can use homebrew's default mysql/mariadb settings by adding the Mac OS Host.

This worked for me (with, what I believe, are default settings).

Community
  • 1
  • 1
untitled90
  • 98
  • 5