Search code examples
dockerdnshostsdocker-desktopwindows-container

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


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.


Solution

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