簡體   English   中英

host.docker.internal 主機上正在運行什么?

[英]What is running on host.docker.internal host?

我只是好奇,host.docker.internal 主機上運行的是什么?

此服務將包從 docker 容器路由到主機網絡上的服務。

但它到底是什么?

我發現它不是網關。

我將回答一些關於Linux實現的發現。 我敢打賭,Docker for Windows / Mac 的實現細節會有所不同。

簡而言之: host.docker.internal是一個名稱。 在您的主機上運行並綁定到也設置為 Docker 守護進程host-gateway的網絡接口的每個服務,都可以從位於host.docker.internal:[service_port]的容器內部訪問。

例子

$ docker run -it --rm --add-host=host.docker.internal:host-gateway alpine
/ # cat /etc/hosts
127.0.0.1   localhost
... (it has a few other lines here) ...
172.17.0.1  host.docker.internal 👈👈👈

這個怎么運作

  • --add-host :它將您在上面看到的最后一行添加到容器的/etc/hosts中,這樣host.docker.internal解析為host-gateway的 IP 地址

  • host-gateway :通常是172.17.0.1 (默認網橋),但也可以使用dockerd進行配置,請參閱相關 文檔👇:

 --host-gateway-ip ip IP address that the special 'host-gateway' string in --add-host resolves to. Defaults to the IP address of the default bridge

更多挖掘...

您可以檢查添加此功能的拉取請求: #40007

用戶描述他做了以下事情:

我做了什么
此 PR 允許容器通過將特殊字符串“host-gateway”附加到--add-host來連接到 Linux 主機,例如--add-host=host.docker.internal:host-gateway添加host.docker.internal DNS 條目在/etc/hosts中並將其映射到host-gateway-ip
此 PR 還添加了一個守護進程標志調用host-gateway-ip ,默認為默認網橋 IP
Docker Desktop 需要將此字段設置為主機代理 IP,以便可以將 host.docker.internal 的 DNS 請求路由到 VPNkit

歷史背景

我認為這一切背后的歷史或多或少如下:

  • Linux 用戶可以使用默認網橋的 IP 地址訪問主機服務。 Docker 設置網絡的方式提供了這一點。
  • Windows / Mac 用戶也需要訪問主機,但由於操作系統不同,那里的網絡更加復雜。 所以 Docker for Windows / Mac 引入了這個host.docker.internal東西。
  • Linux 用戶也可以使用它,因為使用名稱比使用 IP 地址更容易。

更多: 從 Docker 容器內部,如何連接到機器的本地主機?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM