5

I have just attempted to move an app into a docker container using boot2docker on OS X.

This app requires to connect to a mysql server running on the host system (not in the app's container or in another container).

Now I'm struggling with configuring the mysql hostname within the "dockerized" app: So far it just connected to localhost, but this doesn't work anymore because this no longer points to the host mysql is actually running on.

As a quick workaround, I have added my workstations private IP (10.0.0.X in my case) to the app's mysql connection config.

However I wonder: Is there an automatic way to find the host's private IP address from within a docker container?

1 Answers1

2

You can provide the IP and port through environment variables to the contained application.

Create a script in your container like:

#!/bin/bash
export $MYSQL_HOST
export $MYSQL_PORT

echo $MYSQL_HOST
echo $MYSQL_PORT

# TODO: maybe your have to write some config files at this point

/start_your_app.sh # use the enviroment variables e.g. in your app config.

Run your containerimage with:

docker run -e MYSQL_HOST=192.168.172.1 MYSQL_PORT=3306 your_image

Have a look at e.g. http://serverascode.com/2014/05/29/environment-variables-with-docker.html

Pluto1010
  • 103
  • 8
  • This is a valid solution / a way to go - however I'm still curious to find out how to find the host system IP:) – Patrick Hagemeister May 04 '15 at 10:10
  • /sbin/ip route|awk '/default/ { print $3 }' [look here](http://stackoverflow.com/questions/22944631/how-to-get-the-ip-address-of-the-docker-host-from-inside-a-docker-container) – Pluto1010 May 04 '15 at 10:12
  • @Pluto1010. That doesn't work when using boot2docker. – foldl Jul 28 '15 at 17:32
  • I think there should be one more **-e** with `MYSQL_PORT=3306` making it `docker run -e MYSQL_HOST=192.168.172.1 -e MYSQL_PORT=3306 your_image` – Harmeet Singh Mar 02 '16 at 16:03