localhost not working docker windows 10
Asked Answered
M

6

28

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.

Matriarchy answered 30/10, 2017 at 7:32 Comment(5)
That's how Docker works. Every container gets its own internal address. If you want to access an interal service using a local port, you expose it with the -p parameter, eg -p 18000:80 will expose a web app at 127.0.0.1:80. For specifics, post the parameters you used to create the containerSybyl
Downvoters, remember when you tried to create your first web site running on that new thing called Docker? How long did you spend trying to find out how to talk to it?Sybyl
Please specify the Windows and Docker versions you are running, as well as the command-line parameters you used. Not Windows 10 but eg 10.0.16299.15. You can get this by typing ver at a command line. For docker: docker version. Do you already run something at port 80? Perhaps IIS with a default site? Have you tried changing the forwarded port, eg to 18000 with -p 18000:80 and connect to localhost:18000 ?Sybyl
Docker version 17.09.0-ce, build afdb6d4 and Windows 10 pro Version 10.0.15063 .... I used to speify port 5000:80 but nothing works... So I reset the docker to default and now its working on linux container, earlier I was working on windows container as guided by microsoft siteMatriarchy
There's nothing wrong with Windows containers. Linux containers are for Linux apps. What exactly is that nothing works that you tried? As for the tutorial, you don't need these steps anymore, VS templates already provide Docker support. You can just create a new web app from the template and run it.Sybyl
S
8

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

Shon answered 30/10, 2017 at 9:33 Comment(14)
That's an old issue. I'm using SQL Server inside a container with port forwarding and connect through localhost just fineSybyl
Maybe its because of an old version. The docker docs example which was referred in the question has all the steps pointed out clearly and failing that example to access via localhost would only happen due to the above issue.Shon
It would also happen if something else run on port 80, like the default web site of IIS, or any other deployed web project. Docker won't complain if the port is already takenSybyl
Yes agree with thatShon
thanks for the answer... I searched deeply and got the answer. I switched to windows container as I was following microsoft tutorial so it mapped onto 172.x.x.x , now I switched back to linux container and everything is running fine.Matriarchy
@UsamaSaleem that is not a solution. There's nothing wrong with Windows containers. If you needed a Linux container for your application, it means that you were building a Llinux app.Sybyl
@UsamaSaleem can you please specify the Windows and Docker versions you are running, as well as the command-line parameters you used as Panagiotis earlier mentioned. So we can find the real issue behind your problem.Shon
Docker version 17.09.0-ce, build afdb6d4 and Windows 10 pro Version 10.0.15063 .... and command line parameters -d -p --name etc from the tutorialMatriarchy
@UsamaSaleem is ther any other application running on port 80 in your Windows host? Whats the port that IIS server runs on?Shon
@RavinduFernando unable to findMatriarchy
@UsamaSaleem try this and tell! knowledge.autodesk.com/support/vault-products/troubleshooting/…Shon
By default its running on port 80. So doing a port forwarding is the best way. Try docker run -d -p 5000:80 --name webserver nginx insted of docker run command provided in the docker docs and access it via localhost:5000Shon
@UsamaSaleem Refer the answer given by Panagiotis to this question to understand what is really happening if you are still unclear.Shon
@UsamaSaleem I updated the answer with the issue link you have providedShon
S
9

Most likely a different application already runs at port 80. You'll have to forward your web site to a different port, eg:

docker run -d -p 5000:80 --name myapp myasp

And point your browser to http://localhost:5000.

When you start a container you specify which inner ports will be exposed as ports on the host through the -p option. -p 80:80 exposes the inner port 80 used by web sites to the host's port 80.

Docker won't complain though if another application already listens at port 80, like IIS, another web application or any tool with a web interface that runs on 80 by default.

The solution is to:

  1. Make sure nothing else runs on port 80 or
  2. Forward to a different port.

Forwarding to a different port is a lot easier.

To ensure that you can connect to a port, use the telnet command, eg :

telnet localhost 5000

If you get a blank window immediatelly, it means a server is up and running on this port. If you get a message and timeout after a while, it means nobody is running. You anc use this both to check for free ports and ensure you can connect to your container web app.

PS I run into this just a week ago, as I was trying to set up a SQL Server container for tests. I run 1 default and 2 named instances already, and docker didn't complain at all when I tried to create the container. Took me a while to realize what was wrong.

Sybyl answered 30/10, 2017 at 10:7 Comment(4)
I followed your commands but I got this when connecting to localhost -> "This site can’t be reached", besides I ran telnet command on 80 and I got a blank window(server is up), then I ran on 5000 and I got "Could not open connection to the host, on port 5000"Matriarchy
@RavinduFernando I can't, that's what I was trying to fix.I am only able to access it through NATed IP... I found this reply related to my problem.. github.com/MicrosoftDocs/Virtualization-Documentation/issues/… read the last commentMatriarchy
@UsamaSaleem so that means the answer I added is still valid ryt?Shon
according to my research, Yes. but if someone have other ideas or opinions, I am here and happy to apply.Matriarchy
S
8

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

Shon answered 30/10, 2017 at 9:33 Comment(14)
That's an old issue. I'm using SQL Server inside a container with port forwarding and connect through localhost just fineSybyl
Maybe its because of an old version. The docker docs example which was referred in the question has all the steps pointed out clearly and failing that example to access via localhost would only happen due to the above issue.Shon
It would also happen if something else run on port 80, like the default web site of IIS, or any other deployed web project. Docker won't complain if the port is already takenSybyl
Yes agree with thatShon
thanks for the answer... I searched deeply and got the answer. I switched to windows container as I was following microsoft tutorial so it mapped onto 172.x.x.x , now I switched back to linux container and everything is running fine.Matriarchy
@UsamaSaleem that is not a solution. There's nothing wrong with Windows containers. If you needed a Linux container for your application, it means that you were building a Llinux app.Sybyl
@UsamaSaleem can you please specify the Windows and Docker versions you are running, as well as the command-line parameters you used as Panagiotis earlier mentioned. So we can find the real issue behind your problem.Shon
Docker version 17.09.0-ce, build afdb6d4 and Windows 10 pro Version 10.0.15063 .... and command line parameters -d -p --name etc from the tutorialMatriarchy
@UsamaSaleem is ther any other application running on port 80 in your Windows host? Whats the port that IIS server runs on?Shon
@RavinduFernando unable to findMatriarchy
@UsamaSaleem try this and tell! knowledge.autodesk.com/support/vault-products/troubleshooting/…Shon
By default its running on port 80. So doing a port forwarding is the best way. Try docker run -d -p 5000:80 --name webserver nginx insted of docker run command provided in the docker docs and access it via localhost:5000Shon
@UsamaSaleem Refer the answer given by Panagiotis to this question to understand what is really happening if you are still unclear.Shon
@UsamaSaleem I updated the answer with the issue link you have providedShon
C
6

For those who encountering this issue in 2022, changing localhost to 127.0.0.1 solved an issue for me.

Chock answered 8/2, 2022 at 23:19 Comment(0)
B
1

The answer is to move the -p argument to the front, after the run. The order of arguments makes a difference..

So this will not work:

docker run --rm example/app -p 5000:80

And this will work:

docker run -p 5000:80 --rm example/app
Barbarese answered 23/3, 2023 at 10:33 Comment(1)
This solved the issue I was having. Was running container from CLI and had the -p statements AFTER the image name, and the ports would not appear in netstat. Running from Docker Desktop, the ports would appear in netstat. Wish I would have found this answer 4 hours ago. Thanks for posting it!Brae
F
-1

I ran into this issue in 2024, but the solution was different. I didn't need to port forward, instead, I had to change the -p flag in my docker run ... command.

Originally, I was trying to run a Uvicorn server on port 8000 in my container. I setup my container to forward its port 8000 to the host port 8000 using the following command:

-p 8000:8000 (host-port:container-port)

But this didn't work. I think it may be because the WSL2 container port 8000 was blocked by a firewall. So, instead, I setup my Uvicorn server to run on the containers port 80, and changed my port forwarding command to:

-p 8000:80 (host-port:container-port)

and everything worked perfectly!

Hopefully, this solution works for someone else who's coming to this problem in 2024.

Frore answered 16/5 at 12:56 Comment(3)
Why will this answer break in seven months? The second docker run -p port needs to match the port the process inside the container is using; if you've written that wrong then published ports won't work, on any platform, at any date.Leguminous
Interesting that you downvoted. Apologies, I assumed that part was obvious. I'll alter my answer.Frore
@DavidMaze Why do you think my container wouldn't connect on port 8000? Do you agree it's probably because of a WSL2 firewall?Frore
K
-2

There is other problem too You must have correct order with parameters

This is WRONG

docker run container:latest -p 5001:80

This sequence start container but parameter -p is ignore, therefore container have no ports mapping

This is good

docker run -p 5001:80 container:latest 
Kneepad answered 5/7, 2022 at 9:52 Comment(1)
the ordering of program arguments not be mandatory for passing the argumentsErinnerinna

© 2022 - 2024 — McMap. All rights reserved.