[英]How to connect to docker host from container on Windows 10 (Docker for Windows)
At which IP address can a docker container connect to its host on Docker for Windows (on Windows 10)? At which IP address can a docker container connect to its host on Docker for Windows (on Windows 10)? How do you find this IP address?
如何找到这个 IP 地址?
Example: you have a service running at port 1234 on your Windows 10 machine.示例:您在 Windows 10 机器上的端口 1234 上运行了一项服务。 A program inside your container must access this service.
容器内的程序必须访问此服务。 What IP address should the program use to connect to the host?
程序应该使用什么 IP 地址连接到主机?
Short answer : in most cases, you'll need 10.0.75.1 .简短回答:在大多数情况下,您需要10.0.75.1 。
In Docker for Windows, the container communicates through a vEthernet adapter called DockerNAT.在 Docker for Windows 中,容器通过名为 DockerNAT 的 vEthernet 适配器进行通信。 To find its details, open Command Prompt and type
要查找其详细信息,请打开命令提示符并键入
ipconfig
Look for an entry that looks like寻找一个看起来像的条目
Ethernet adapter vEthernet (DockerNAT):
Connection-specific DNS Suffix . :
Link-local IPv6 Address . . . . . : fe80::fd29:297:4583:3ad4%4
IPv4 Address. . . . . . . . . . . : 10.0.75.1
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . :
The IP address to the right of IPv4 Address
is the one you need. IPv4 Address
右侧的IPv4 Address
是您需要的 IP 地址。
Note: make sure the service allows connections from outside your host.注意:确保该服务允许来自主机外部的连接。 As far as that service is concerned, your docker container is a different machine.
就该服务而言,您的 docker 容器是一台不同的机器。 Also make sure Windows Firewall allows communication to and from the service.
还要确保 Windows 防火墙允许与服务进行通信。
One of options that allows you to connect from container to host, is to run your container with parameter允许您从容器连接到主机的选项之一是使用参数运行容器
--net="host"
Example:示例:
docker run -it --net="host" container_name
Then from container, you can connect to service on host using:然后从容器中,您可以使用以下命令连接到主机上的服务:
localhost:port
But in this case, you will not be able to link more containers using --link
parameter.但在这种情况下,您将无法使用
--link
参数链接更多容器。
More on this topic: http://phillbarber.blogspot.sk/2015/02/connect-docker-to-service-on-parent-host.html有关此主题的更多信息: http : //phillbarber.blogspot.sk/2015/02/connect-docker-to-service-on-parent-host.html
UPDATE :更新:
From version 18.03 , you can use DNS name host.docker.internal
, which resolves to the internal IP address used by the host.从版本18.03 开始,您可以使用 DNS 名称
host.docker.internal
,该名称解析为主机使用的内部 IP 地址。
More: https://docs.docker.com/docker-for-windows/networking/更多: https : //docs.docker.com/docker-for-windows/networking/
On older versions, you can connect to service running on host Windows using IP address you get executing command ipconfig on host -> Ethernet adapter -> IPv4 Address在旧版本上,您可以使用在主机上执行命令ipconfig -> 以太网适配器 -> IPv4 地址的 IP 地址连接到在主机 Windows 上运行的服务
UPDATE As per Datz comment below, docker.for.win.localhost
is working in Docker for Windows (confirmed).更新根据下面的 Datz 评论,
docker.for.win.localhost
正在 Docker for Windows 中工作(已确认)。
The host will have a host.docker.internal
registered in the default DNS used by containers.主机将在容器使用的默认 DNS 中注册一个
host.docker.internal
。 So you can use something like curl http://host.docker.internal/
to access a web server running on your machine even if that server is running in another container provided you exposed the port.因此,您可以使用
curl http://host.docker.internal/
类的东西来访问在您的机器上运行的 Web 服务器,即使该服务器在另一个容器中运行,前提是您公开了端口。
On Windows 10, after your docker container is started, you can run docker-machine ip
in command line (cmd or Docker QuickStart Terminal, etc) to get the ip address of your docker container.在 Windows 10 上,启动 docker 容器后,您可以在命令行(cmd 或 Docker QuickStart 终端等)中运行
docker-machine ip
以获取 docker 容器的 ip 地址。 This ip address is usually, 192.168.99.100.这个 IP 地址通常是 192.168.99.100。
I'm using Windows containers with Docker version 20.10.11.我正在使用带有 Docker 版本 20.10.11 的 Windows 容器。 I'm trying to reach a server running on the host machine.
我正在尝试访问在主机上运行的服务器。 I tried all other answers/comments.
我尝试了所有其他答案/评论。 None are working for me.
没有人为我工作。 I tried...
我试过...
The only way I can communicate is using the standard ethernet adapter's IPv4 on the host (not the container):我可以通信的唯一方法是在主机(而不是容器)上使用标准以太网适配器的 IPv4:
Using curl http://10.0.0.4:8080
accesses the server just fine.使用
curl http://10.0.0.4:8080
访问服务器就好了。
Side note - running a config script beforehand would also solve the problem... just shouldn't be necessary.旁注 - 事先运行配置脚本也可以解决问题......只是没有必要。 https://stackoverflow.com/a/67434367/7991646
https://stackoverflow.com/a/67434367/7991646
It's so strange that in 2020 April, this is still a question.太奇怪了,2020年4月,这还是个问题。 and most of the "host.docker.internal" and "172.17.0.1" don't work for WINDOWS docker.
大多数“host.docker.internal”和“172.17.0.1”不适用于 WINDOWS docker。
so, I suggest for windows docker user, just simple type "ipconfig" in cmd:所以,我建议 windows docker 用户,只需在 cmd 中输入简单的“ipconfig”:
and you will get some ips for your windows (host ) machine:您将为您的 windows (主机)机器获得一些 ip:
then,in your docker, install "ping/curl/ifconfig/telnet", then type "ifconfig" to get your "docker" ip address, then type "ping <windows host ip" to see if the ip is correct.然后,在您的 docker 中,安装“ping/curl/ifconfig/telnet”,然后键入“ifconfig”以获取您的“docker”ip 地址,然后键入“ping <windows 主机 ip”以查看 Z957B5206.8832E14F 是否正确
this needs you install external tools to docker, but it worth.这需要您将外部工具安装到 docker,但值得。
open Command Prompt and type ipconfig打开命令提示符并输入 ipconfig
look for寻找
IPv4 Address. IPv4 地址。 .
. .
. .
. .
. .
. .
. .
. .
. .
. .
. : 192.xxx.0.xxx
: 192.xxx.0.xxx
The IP address to the right of IPv4 Address is the one you need. IPv4 地址右侧的 IP 地址是您需要的地址。
OR或者
"host": "host.docker.internal" “主机”:“主机.docker.internal”
also work也工作
I had the similar issue.我有类似的问题。 In my case /etc/hosts file was missing from my container.
在我的情况下,我的容器中缺少 /etc/hosts 文件。 This was because I was not using docker desktop.
这是因为我没有使用 docker 桌面。 Docker desktop allows docker deamon to write this entry in /etc/hosts file.
Docker 桌面允许 docker 守护程序将此条目写入 /etc/hosts 文件。 I was using docker engine with lcow on windows server 2019. I simply had to use my host machine IP instead of host.docker.internal to access the services on my host machine.
I was using docker engine with lcow on windows server 2019. I simply had to use my host machine IP instead of host.docker.internal to access the services on my host machine. I could use this IP since it was static.
我可以使用这个 IP,因为它是 static。 In case of dynamic IP you will need dns to resolve that IP or use bridge network IP.
在动态 IP 的情况下,您将需要 dns 来解决 IP 或使用桥接网络 ZA12A3079E14CED426E6950B See the details of the issue and how I fixed it on this thread: Unable to resolve docker.host.internal with docker-compose on windows server 2019
请参阅问题的详细信息以及我如何在此线程上修复它: Unable to resolve docker.host.internal with docker-compose on windows server 2019
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.