简体   繁体   English

从boot2docker / osx中运行的docker容器中查找主机ip

[英]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. 我刚刚尝试使用OS X上的boot2docker将应用程序移动到docker容器中。

This app requires to connect to a mysql server running on the host system (not in the app's container or in another container). 此应用程序需要连接到主机系统上运行的mysql服务器(不在应用程序的容器或其他容器中)。

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. 现在我正在努力在“dockerized”应用程序中配置mysql主机名:到目前为止它只是连接到localhost ,但这不再起作用,因为这不再指向主机mysql实际上正在运行。

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. 作为一种快速解决方法,我已将我的工作站私有IP(在我的情况下为10.0.0.X )添加到应用程序的mysql连接配置中。

However I wonder: Is there an automatic way to find the host's private IP address from within a docker container? 但我想知道: 是否有自动方式从docker容器中查找主机的私有IP地址

You can provide the IP and port through environment variables to the contained application. 您可以通过环境变量为包含的应用程序提供IP和端口。

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 eg http://serverascode.com/2014/05/29/environment-variables-with-docker.html 请查看http://serverascode.com/2014/05/29/environment-variables-with-docker.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM