dockerdockerfiledocker-for-windowsasp.net-docker-extensions

localhost not working docker windows 10


I am using VS2017 docker support. VS created DockerFile for me and when I build docker-compose file, it creates the container and runs the app on 172.x.x.x IP address. But I want to run my application on localhost.

I did many things but nothing worked. Followed the docker docs as a starter and building microsoft sample app . The second link is working perfectly but I get HTTP Error 404 when tried the first link approach.

Any help is appreciated.


Solution

  • In order to access the example posted on Docker Docs, that you pointed out as not working, follow the below steps,

    1 - List all the running docker containers

    docker ps -a
    

    After you run this command you should be able to view all your docker containers that are currently running and you should see a container with the name webserver listed there, if you have followed the docker docs example correctly.

    2 - Get the IP address where your webserver container is running. To do that run the following command.

     docker inspect -f "{{ .NetworkSettings.Networks.nat.IPAddress }}" webserver
    

    You should now get the IP address which the webserver container is running, hope you are familiar with this step as it was even available within the building Microsoft sample app example that you attached with the question.

    Access the IP address you get once running the above command and you should see the desired output.

    Answering to your first question (accessing docker container with localhost in docker for windows), in Windows host you cannot access the container with localhost due to a limitation in the default NAT network stack. A more detailed explanation for this issue can be obtained by visiting this link. Seems like the docker documentation is not yet updated but this issue only exists in Windows hosts.

    There is an issue reported for this as well - Follow this link to see that.

    Hope this helps you out.

    EDIT

    The solution for this issue seems to be coming in a future Windows release. Yet that release comes out this limitation is available in Windows host. Follow this link -> https://github.com/MicrosoftDocs/Virtualization-Documentation/issues/181