简体   繁体   English

如何在docker-compose.yml中包含Docker主机的IP?

[英]How to include IP of Docker host in docker-compose.yml?

I want to include support for Xdebug in a PHP Docker container, however as part of this, I need to specify the IP of the Windows machine running the Docker container via XDEBUG_CONFIG=remote_host=${HOST_IP} - Currently HOST_IP is manually specified in a .env file, but I'd like to automate this to reduce the setup steps for other users. 我希望在PHP Docker容器中包含对Xdebug的支持,但是作为其中的一部分,我需要通过XDEBUG_CONFIG=remote_host=${HOST_IP}指定运行Docker容器的Windows机器的IP - 当前HOST_IP是在a中手动指定的.env文件,但我想自动执行此操作以减少其他用户的设置步骤。

My problem is that I can't seem to find a way to easily determine the IP of the host machine. 我的问题是我似乎无法找到一种方法来轻松确定主机的IP。 It also needs to work on both Windows and Linux Docker hosts, as not all users use Windows as their desktop environment. 它还需要在Windows和Linux Docker主机上运行,​​因为并非所有用户都使用Windows作为其桌面环境。 I also can't use ${HOSTNAME} , as this fails to resolve in DNS. 我也无法使用${HOSTNAME} ,因为这无法在DNS中解析。

Does anyone have any suggestions on how to achieve this? 有没有人对如何实现这一点有任何建议?

EDIT2: Updating this answer for the newer versions of Docker: From 18.03 onward, Docker For Windows and other Docker platforms have been updated to include a cross-platform hostname for their Docker host, host.docker.internal - which is bloody helpful. EDIT2:为更新版本的Docker更新这个答案:从18.03开始,Docker For Windows和其他Docker平台已经更新,包括他们的Docker主机的跨平台主机名, host.docker.internal - 这对血腥有帮助。

You might try a formatted docker info : https://docs.docker.com/engine/reference/commandline/info/#format-the-output 您可以尝试格式化的docker infohttps//docs.docker.com/engine/reference/commandline/info/#format-the-output

docker info --format '{{json .}}'
docker info --format '{{json .path.to.ip}}'

Eg in a (single-host) Docker Swarm you can get the host ip by: 例如,在(单主机) Docker Swarm您可以通过以下方式获取主机IP:

docker info --format '{{json .Swarm.NodeAddr}}'

Via command substition stored in a variable: 通过命令替换存储在变量中:

docker_host_ip=$(docker info --format '{{json .Swarm.NodeAddr}}')

I could not try it on on Windows or on Docker without Swarm ... but docker info should work across platforms. 我无法在没有Swarm的Windows或Docker上试用它,但是docker info应该可以跨平台工作。

Update (according to comments below): 更新(根据以下评论):
Not really (syntactically) "beautiful" you can use --format {{index path arrayIndex "Key"}} with docker network inspect and access the first element of an array (index 0 ) and then access the map inside this array via its key ( "Gateway" ): 不是(语法上)“漂亮”你可以使用--format {{index path arrayIndex "Key"}}--format {{index path arrayIndex "Key"}} docker network inspect并访问数组的第一个元素(索引0 ),然后通过它访问这个数组中的map密钥( "Gateway" ):

docker network inspect docker_dockernet --format '{{index .IPAM.Config 0 "Gateway"}}'

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

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