3

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".

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

But the console shows "Access is denied."

So basically, my question is:

  • How can I add DNS entries to hosts file of a Windows Container?

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • Can you set up a local DNS server (like dnsmasq or BIND on a Linux server), or use a cloud-hosted DNS service like Amazon's Route 53? That would be much easier to maintain than manually copying hosts-file entries around. – David Maze Sep 14 '20 at 12:52
  • Using a local DNS server could be an option, however, the development environment is Windows 10 and it doesn't have built-in DNS server and I prefer to not rely on an extra 3rd party tool as a dependency. Also using a real cloud DNS server is not an option, because it's just development environment. I expected `--add-host` option do the trick but apparently [it's not working](https://github.com/docker/for-win/issues/1455) for windows containers at the moment. – Reza Aghaei Sep 14 '20 at 13:07
  • 1
    --add-host is STILL not working for windows containers as of this writing, 4 years after being originally reported. – Larry Smith Sep 21 '22 at 22:18

1 Answers1

6

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

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

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

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.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • 1
    ty ty ty... though I suspect that resolving this with a docker compose will be better long term, being able to TEST edits to the host file allows us to know what changes will work. – Larry Smith Sep 21 '22 at 22:17