Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find host ip from a docker container running in boot2docker / osx

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?

like image 408
Patrick Hagemeister Avatar asked Nov 01 '22 05:11

Patrick Hagemeister


1 Answers

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

like image 111
Pluto1010 Avatar answered Nov 09 '22 07:11

Pluto1010