Search code examples
dockerdocker-composedocker-networknano-server

docker --add-hostname not working for windows container


I am trying to add host names to host file in the windows container with no success.

According to documentation, this should add an entry on host file

docker run -it --add-host me:127.0.0.1  microsoft/dotnet:nanoserver powershell

but i can't ping "me" from the container.

Looking up at host file i find no entry for me.

cat C:\Windows\System32\drivers\etc\hosts

returns only the entrys:

192.168.125.108    host.docker.internal
192.168.125.108    gateway.docker.internal

I also try to add by docker-compose file, but i am not familiar with the sintax:

Here is my docker-compose.override.yml

version: '3.4' services: trfout_wsrv: extra_hosts: - "me:127.0.1.1"

image: ${DOCKER_REGISTRY-}trfoutwsrv
build:
  context: .\TRFOUT\TRFOUT_WSRV
  dockerfile: Dockerfile

Solution

  • seems like --add-host not supported on the window, As its says

    --add-host=""      : Add a line to /etc/hosts (host:IP)
    

    I do think there is /etc/hosts in Window. Also example given in documentation is ubuntun base. docker run --add-host

    Also below is the open issue on Docker Github

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

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