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

查看:33
本文介绍了如何将 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 条目添加到容器的主机文件中.为此,我连接到容器的命令外壳并运行以下命令:

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:windowssystem32driversetc

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

所以基本上,我的问题是:

So basically, my question is:

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

我正在使用 Docker 桌面版本 2.3.0.4(46911),在 Windows 10 上具有 Engine version 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.

推荐答案

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

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

docker exec --user "NT AUTHORITYSYSTEM" -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:windowssystem32driversetchosts

为了确保您使用的是正确的值而不是 XXXX,打开主机的 hosts 文件,您将看到类似 XXXX host.docker.internal.这是从容器的角度来看主机的 IP.在上述命令中使用该 IP 将正确的 DNS 条目添加到容器的主机文件中.要测试结果,您可以使用 type c:windowssystem32driversetchosts 查看文件内容,并使用 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:windowssystem32driversetchosts and get contents from that website using curl mysite.local.

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

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