如何将DNS条目添加到Windows容器的主机文件? [英] How can I add DNS entries to hosts file of a Windows Container?

查看:95
本文介绍了如何将DNS条目添加到Windows容器的主机文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在主机的IIS中有一个网站,可通过 http://mysite.local 地址访问.我还在该主机上运行了一个Windows容器,我正在尝试使用"curl http://mysite.local"从我的Windows容器连接到该网站.

I have a website in IIS of my host machine which is accessible by http://mysite.local address. I also have a windows container running on this host and I'm trying to connect to that website, from my windows container by using "curl http://mysite.local".

我将正确的DNS条目添加到容器的hosts文件中.为此,我连接到容器的命令外壳并运行以下命令:

I'm going to add the proper DNS entry to hosts file of the container. To do so, I connect to command shell of the container and run the following command:

echo 192.168.0.144 mysite.local >> c:\windows\system32\drivers\etc

但是控制台显示"访问被拒绝."

基本上,我的问题是:

  • 如何将DNS条目添加到Windows容器的主机文件中?

我正在使用 Docker Desktop版本2.3.0.4(46911),在 Windows 10 上具有 Engine版本19.03.12 ./>我已经使用 mcr.microsoft.com/windows/servercore:ltsc2019 作为Windows基本映像创建了容器,还尝试了 nanoserver:1903 .

I'm using Docker Desktop version 2.3.0.4(46911), having Engine version 19.03.12 on Windows 10.
I've created the container using mcr.microsoft.com/windows/servercore:ltsc2019 as windows base image, also tried nanoserver:1903.

推荐答案

您可以以管理员身份获取容器的命令外壳并运行命令:

You can get the command shell of the container as administrator and run the command:

docker exec --user "NT AUTHORITY\SYSTEM" -it yourcontainername cmd

然后,您可以在容器的hosts文件中添加适当的记录:

Then you can add a proper record to hosts file of the container:

echo X.X.X.X mysite.local >> c:\windows\system32\drivers\etc\hosts

要确保您使用的是正确的值而不是 XXXX ,请打开主机的hosts文件,然后您会看到一个DNS条目,例如 XXXX host.docker.internal .从容器的角度来看,这是主机的IP.在上面的命令中使用该IP将正确的DNS条目添加到容器的hosts文件中.要测试结果,您可以使用 type c:\ windows \ system32 \ drivers \ etc \ hosts 查看文件的内容,并使用 curl mysite.local 从该网站获取内容.代码>.

To make sure you are using proper value instead of X.X.X.X, open hosts file of your host machine, and you will see a DNS entry like X.X.X.X host.docker.internal. This is the IP of host in container's point of view. Use that IP in above command to add thee correct DNS entry to hosts file of the container. To test the result, you can see the content of the file using type c:\windows\system32\drivers\etc\hosts and get contents from that website using curl mysite.local.

这篇关于如何将DNS条目添加到Windows容器的主机文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆