在Mac和Windows上,可以在容器内部使用host.docker.internal (Docker 18.03+)。
Linux上是否有一种不需要传递env变量或使用各种CLI命令提取它就能开箱即用的方法?
在Mac和Windows上,可以在容器内部使用host.docker.internal (Docker 18.03+)。
Linux上是否有一种不需要传递env变量或使用各种CLI命令提取它就能开箱即用的方法?
当前回答
https://github.com/docker/for-linux/issues/264
IP=$(ip -4 route list match 0/0 | awk '{print $3}')
echo "Host ip is $IP"
echo "$IP host.docker.internal" | sudo tee -a /etc/hosts
它会将host.docker.internal添加到您的主机。然后可以在xdebug配置中使用它。
下面是docker-compose.yml中env变量的例子
XDEBUG_CONFIG: remote_host=host.docker.internal remote_autostart=On remote_enable=On idekey=XDEBUG remote_log=/tmp/xdebug.log remote_port=9999
其他回答
这是我的解决方案:
IP_ADDRESS=$(ip addr show | grep "\binet\b.*\bdocker0\b" | awk '{print $2}' | cut -d '/' -f 1)
然后在docker-compose中:
extra_hosts:
docker.host: ${IP_ADDRESS}
如果你正在使用Docker Compose + Linux,你必须手动添加它(至少现在)。在docker-compose上使用extra_hosts。yaml文件:
version: '3.7'
services:
fpm:
build:
context: .
extra_hosts:
- "host.docker.internal:host-gateway"
不要忘记更新Docker,因为这只适用于Docker v20.10+。
来源:https://github.com/docker/for-linux/issues/264#issuecomment-784985736
这取决于你想做什么。如果您使用——net=host运行,那么localhost应该可以正常工作。如果使用默认网络,请使用静态IP 172.17.0.1。我怀疑这两个领域的表现都不会与那些领域完全相同。
https://github.com/docker/for-linux/issues/264
IP=$(ip -4 route list match 0/0 | awk '{print $3}')
echo "Host ip is $IP"
echo "$IP host.docker.internal" | sudo tee -a /etc/hosts
它会将host.docker.internal添加到您的主机。然后可以在xdebug配置中使用它。
下面是docker-compose.yml中env变量的例子
XDEBUG_CONFIG: remote_host=host.docker.internal remote_autostart=On remote_enable=On idekey=XDEBUG remote_log=/tmp/xdebug.log remote_port=9999
host.docker.internal exists only in Windows WSL because Docker Desktop for Windows runs Docker daemon inside the special WSL VM Docker-Desktop. It has its own localhost and its own WSL2 interface to communicate with Windows. This VM has no static IP. The IP is generated every time when VM is created and passed via host.docker.internal in the generated /etc/hosts to every distro. Although there is no bridge or real v-switch all ports opened on eth0 of VM internal network are mapped on the host Local network, BUT NOT ON THE ETH0 OF THE HOST. There is no real bridge and port mapping - nothing to configure. Inside WSL VM its Localhost is the same as the localhost of the Linux machine. 2 processes inside WSL VM can communicate via localhost. Cross-distro IPC must use host.docker.internal. It is possible to create bridge inside WSL VM -Docker does it.