简体   繁体   English

什么是 linux 相当于“host.docker.internal”

[英]What is linux equivalent of “host.docker.internal”

On Mac and Windows it is possible to use docker.for.mac.host.internal (replaces docker.for.mac.localhost ) and docker.for.win.host.internal (replaces docker.for.win.localhost ) host.docker.internal (Docker 18.03+) inside container.在 Mac 和 Windows 上,可以使用 docker.for.mac.host.internal (替换 docker.for.mac.localhost )和 docker.for.win.host.internal (替换 docker.for.win.localhost host.docker.internal (Docker 18.03+) 容器内。

Is there one for Linux that will work out of the box without passing env variables or extracting it using various CLI commands?有没有一种适用于 Linux 的工具可以在不传递 env 变量或使用各种 CLI 命令提取它的情况下开箱即用?

Depends what you're trying to do.取决于你想做什么。 If you're running with --net=host , localhost should work fine.如果您使用--net=host运行,则localhost应该可以正常工作。 If you're using default networking, use the static IP 172.17.0.1 .如果您使用默认网络,请使用静态 IP 172.17.0.1 I suspect neither will behave quite the same as those domains.我怀疑它们的行为都不会与那些域完全相同。

For linux systems, you can – starting from major version 20.04 of the docker engine – now also communicate with the host via host.docker.internal .对于 linux 系统,从host.docker.internal引擎的主要版本20.04开始,您现在还可以通过host.docker.internal与主机通信。 This won't work automatically , but you need to provide the following run flag:这不会自动工作,但您需要提供以下运行标志:

--add-host=host.docker.internal:host-gateway

See the answer here: https://stackoverflow.com/a/61424570/3757139在此处查看答案: https : //stackoverflow.com/a/61424570/3757139

See also this answer below to add to a docker-compose file - https://stackoverflow.com/a/67158212/243392另请参阅下面的此答案以添加到 docker-compose 文件 - https://stackoverflow.com/a/67158212/243392

One solution is to use a special container which redirects traffic to the host.一种解决方案是使用特殊容器将流量重定向到主机。 You can find such a container here: https://github.com/qoomon/docker-host .你可以在这里找到这样的容器: https : //github.com/qoomon/docker-host The idea is to grab the default route from within the container and install that as a NAT gateway for incoming connections.这个想法是从容器内获取默认路由并将其安装为传入连接的 NAT 网关。

An imaginary example usage:一个想象的例子用法:

docker-host:
  image: qoomon/docker-host
  cap_add: [ 'NET_ADMIN', 'NET_RAW' ]
  restart: on-failure
  environment:
    - PORTS=999

some-service:
  image: ...
  environment:
    SERVER_URL: "http://docker-host:999"
  command: ...
  depends_on:
    - docker-host

If you are using Docker Compose + Linux , you have to add it manually (at least for now).如果您使用的是Docker Compose + Linux ,则必须手动添加它(至少现在是这样)。 Use extra_hosts on your docker-compose.yaml file:extra_hosts docker-compose.yaml文件中使用extra_hosts

version: '3.7'

services:

  fpm:
    build:
      context: .
    extra_hosts:
      - "host.docker.internal:host-gateway"

Docs - https://docs.docker.com/compose/compose-file/compose-file-v3/#extra_hosts文档 - https://docs.docker.com/compose/compose-file/compose-file-v3/#extra_hosts

Do not forget to update Docker since this only works with Docker v20.10+ .不要忘记更新 Docker,因为这仅适用于Docker v20.10+

Source : https://github.com/docker/for-linux/issues/264#issuecomment-784985736来源: https : //github.com/docker/for-linux/issues/264#issuecomment-784985736

This is my solution:这是我的解决方案:

IP_ADDRESS=$(ip addr show | grep "\binet\b.*\bdocker0\b" | awk '{print $2}' | cut -d '/' -f 1)

then in docker-compose:然后在 docker-compose 中:

extra_hosts:
  docker.host: ${IP_ADDRESS}

For linux there isn't a default DNS name for the host machine.对于 linux,主机没有默认的 DNS 名称。 This can be verified by running the command:这可以通过运行以下命令来验证:

docker run -it alpine cat /etc/hosts

This feature has been requested, however wasn't implemented.已请求此功能,但未实现。 You can check this issue .你可以检查这个问题 As discussed you can use the following command to find the IP of the host from the container.如前所述,您可以使用以下命令从容器中查找主机的 IP。

netstat -nr | grep '^0\.0\.0\.0' | awk '{print $2}'

Alternatively, you can provide the host ip to the run command via docker run --add-host dockerHost:<ip-address> ...或者,您可以通过docker run --add-host dockerHost:<ip-address> ...

https://github.com/docker/for-linux/issues/264 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

It will add host.docker.internal to your hosts.它会将host.docker.internal添加到您的主机。 Then you can use it in xdebug config.然后你可以在 xdebug 配置中使用它。

Here is example of env variable in docker-compose.yml这是docker-compose.yml中 env 变量docker-compose.yml

XDEBUG_CONFIG: remote_host=host.docker.internal remote_autostart=On remote_enable=On idekey=XDEBUG remote_log=/tmp/xdebug.log remote_port=9999

Using the docker0 interface ip, say 172.17.0.1, could be a good workaround.使用 docker0 接口 ip,比如 172.17.0.1,可能是一个很好的解决方法。

Just be sure that the service you need to reach listens to external connections.只需确保您需要访问的服务侦听外部连接。 A typical example is Mysql who binds to 127.0.0.1 by default, resulting unreachable until you allow external connections (es. binding to 0.0.0.0)一个典型的例子是 Mysql 默认绑定到 127.0.0.1,导致无法访问,直到您允许外部连接(例如绑定到 0.0.0.0)

host.docker.internal exists only in Windows WSL because Docker Desktop for Windows runs Docker daemon inside the special WSL VM Docker-Desktop. host.docker.internal 仅存在于 Windows WSL 中,因为 Docker Desktop for Windows 在特殊的 WSL VM Docker-Desktop 内运行 Docker 守护进程。 It has its own localhost and its own WSL2 interface to communicate with Windows.它有自己的本地主机和自己的 WSL2 接口来与 Windows 通信。 This VM has no static IP.此 VM 没有静态 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.每次创建 VM 时都会生成 IP,并通过生成的 /etc/hosts 中的 host.docker.internal 传递给每个发行版。 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.尽管没有网桥或真正的 v-switch,所有在 VM 内部网络的 eth0 上打开的端口都映射到主机本地网络上,但不在主机的 ETH0 上。 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.在 WSL VM 中,它的 Localhost 与 Linux 机器的 localhost 相同。 2 processes inside WSL VM can communicate via localhost. WSL VM 中的 2 个进程可以通过 localhost 进行通信。 Cross-distro IPC must use host.docker.internal.跨发行版 IPC 必须使用 host.docker.internal。 It is possible to create bridge inside WSL VM -Docker does it.可以在 WSL VM 内部创建网桥——Docker 做到了。

  • For allowing internal HTTP requests in Linux, use the static IP 172.17.0.1要在 Linux 中允许内部 HTTP 请求,请使用静态 IP 172.17.0.1

  • Run the following command to get the static IP运行以下命令获取静态IP

$(ip addr show | grep "\\binet\\b.*\\bdocker0\\b" | awk '{print $2}' | cut -d '/' -f 1)

  • Add the new IP to allowed hosts将新 IP 添加到允许的主机

  • Now change the request URL with this IP现在使用此 IP 更改请求 URL

req = requests.get('http://172.17.0.1:8000/api/YOUR_ENDPOINT')

对于 linux,我可以使用我尝试连接的服务名称,例如我的一个容器 (php-fpm) 尝试连接到 mysql,所以我使用mysql作为主机名,因为这是服务名称我的 docker-compose

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

相关问题 host.docker.internal 主机上正在运行什么? - What is running on host.docker.internal host? 什么是 Compose 文件中的 --add-host=host.docker.internal:host-gateway 的等价物 - What is the equivalent of --add-host=host.docker.internal:host-gateway in a Compose file 等效于 Rancher Desktop 中 Docker 桌面的 'host.docker.internal' - Equivalent to Docker Desktop's 'host.docker.internal' in Rancher Desktop ์Nginx Linux 容器无法解析主机。docker.internal - ์Nginx Linux container could not resolve host.docker.internal 如何将域命名为“host.docker.internal”? - How to CNAME a domain to “host.docker.internal”? 当使用 nerdctl 代替 docker 时,应该使用什么来代替“host.docker.internal”? - what should be used instead of 'host.docker.internal' when using nerdctl in place of docker? Docker - host.docker.internal&quot; (192.168.65.2) 拒绝连接 - Docker - host.docker.internal" (192.168.65.2) refused to connect Docker - 如何通过 SSL 连接到 host.docker.internal? - Docker - How do you connect to host.docker.internal on SSL? 引起:java.net.UnknownHostException:host.docker.internal - Caused by: java.net.UnknownHostException: host.docker.internal 引用 ip 时的 host.docker.internal 与容器名称 - host.docker.internal vs container name when referencing ip
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM