for-win icon indicating copy to clipboard operation
for-win copied to clipboard

Adding hosts for windows containers (--add-host, extra_hosts) does not work

Open Dresel opened this issue 8 years ago • 37 comments

Expected behavior

Either docker run --add-host argument or docker compose extra_hosts option should add host entry to etc/host for windows containers.

Actual behavior

Neither docker run --add-host argument nor docker compose extra_hosts option does add host entry to etc/host for windows containers.

Information

  • Windows: Windows 10, 1709
  • Docker: 17.12.0-ce-rc4
  • Compose: 1.18.0

Steps to reproduce the behavior

Not working example for windows container

  1. Run docker run -it --add-host me:127.0.0.1 microsoft/nanoserver
  2. Run ping me

Additional working example for linux container

  1. Run docker run -it --add-host me:127.0.0.1 ubuntu
  2. Run apt-get update
  3. Run apt-get install iputils-ping
  4. Run ping me

Dresel avatar Dec 22 '17 11:12 Dresel

I also see this behavior.

JackUkleja avatar Jan 08 '18 21:01 JackUkleja

I also have the same problem

hamish-riahi avatar Jan 12 '18 02:01 hamish-riahi

Same behavior confirmed.

pasiorovuo avatar Jan 27 '18 20:01 pasiorovuo

Is not this supposed to be filed under moby/moby?

artisticcheese avatar Feb 06 '18 21:02 artisticcheese

anyone has a workaround for this?

icenold avatar Feb 09 '18 12:02 icenold

@icenold, I did the following within the Dockerfile, it has worked for my needs.

RUN $file = $Env:windir+'\System32\drivers\etc\hosts'; `
'10.0.0.1 some.host.com' | Add-Content -PassThru $file;

note that i use the back tick as my escape character.

cnickel avatar Feb 09 '18 13:02 cnickel

Probably much easier just

 "10.0.0.1 some.host.com" | Out-File -Append "$($Env:windir)\drivers\etc\hosts"

artisticcheese avatar Feb 09 '18 14:02 artisticcheese

is there no way to do this via compose?

icenold avatar Feb 09 '18 16:02 icenold

It's bugged out as you can see on this thread. It does not do what is expected. I beleive this shall be filed under moby/mody not docker for win.

artisticcheese avatar Feb 09 '18 16:02 artisticcheese

any update/s?

icenold avatar Mar 30 '18 16:03 icenold

I have the same issue. Any updates?

ghost avatar Apr 24 '18 20:04 ghost

I also ran into this issue today. I seem to be unable to host map at all on microsoft/servercore, even with the above workarounds.

zachChilders avatar Apr 25 '18 06:04 zachChilders

@zachChilders The only way I managed to configure it was with echo... PowerShell would not work for some reason...

RUN echo 10.0.0.1 somehost.domain.local >> "C:\Windows\System32\drivers\etc\hosts"

Not sure it helps...

cblackuk avatar Apr 25 '18 07:04 cblackuk

any updates on this ?

lkts avatar May 20 '18 08:05 lkts

It's a real pain this isn't supported. It would be really helpful to dynamically add hosts from the command line for development and staging purposes where real host names are not practical to add.

jtaylor100 avatar Jun 19 '18 16:06 jtaylor100

I've run into the same issue. I need to add some host entries purely for development. Works great on linux containers but we have cases where we must be able to run on Window containers as well. This seems fairly fundamental...

tkoestler avatar Aug 02 '18 00:08 tkoestler

I have run into this issue as well and it is a platform-breaking feature in my opinion. The only option to work around this seems to be manually adding entries to the hosts file in a "RUN" directive (but this only works when building on windows!!). If I build a linux image on a linux how, any modifications I make to the hosts file inside the image using a RUN directive are ignored, which means I won't be able to use it properly when pushing and pulling it to a windows LCOW host.

EDIT: Just to be clear, I am encountering this problem both on Windows and Linux containers, I tried the reproducer @Dresel posted above with ubuntu and it doesn't work, when I try to ping me, I get ping: me: No address associated with hostname and the /etc/hosts file is completely empty.

I am running Docker CE Stable 18.06.0-ce-win72 (19098) with experimental features enabled on Windows 10 Professional.

starcraft66 avatar Aug 28 '18 14:08 starcraft66

I have run into this issue as well and it is a platform-breaking feature in my opinion. The only option to work around this seems to be manually adding entries to the hosts file in a "RUN" directive (but this only works when building on windows!!). If I build a linux image on a linux how, any modifications I make to the hosts file inside the image using a RUN directive are ignored, which means I won't be able to use it properly when pushing and pulling it to a windows LCOW host.

EDIT: Just to be clear, I am encountering this problem both on Windows and Linux containers, I tried the reproducer @Dresel posted above with ubuntu and it doesn't work, when I try to ping me, I get ping: me: No address associated with hostname and the /etc/hosts file is completely empty.

I am running Docker CE Stable 18.06.0-ce-win72 (19098) with experimental features enabled on Windows 10 Professional.

I'm reporting the same exact issue. Any updates?

poliez avatar Nov 20 '18 09:11 poliez

Same issue here. I think it makes sense to expedite addressing this problem.

Arash-Sabet avatar Jan 02 '19 16:01 Arash-Sabet

we are facing the same issue. --add-host is not working, --network option is not supported.

rpramodd avatar Jan 09 '19 08:01 rpramodd