10

docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1
docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/sdk:3.1
docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/aspnet:3.1

When I run any of the above docker commands to create a container, I get the following error. And I get this for both for linux as well as windows.

C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: Ports are not available: listen tcp 0.0.0.0:8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions. time="2020-03-24T17:20:44+05:30" level=error msg="error waiting for container: context canceled"

I tried the suggestion given in this SO ans to find the process Id and kill it.

netstat to find process for given port

Further I got the process hacker as suggested here to observe whats that process. Looks like its a system process.

Process hacker showing process with id 4

Can anybody suggest what can be done?

VivekDev
  • 20,868
  • 27
  • 132
  • 202

5 Answers5

11

-p 8080:80 says "forward port 8080 on the host to port 80 in the container". Port 80 is determined by the container image. Port 8080 is arbitrary—it's a port you're choosing.

So instead do -p 8081:80, and now you point your browser at localhost:8081 instead of localhost:8080.

If that doesn't work then maybe it's your firewall?

(See https://pythonspeed.com/articles/docker-connection-refused/ for diagrams of how port forwarding works).

Itamar Turner-Trauring
  • 3,430
  • 1
  • 13
  • 17
  • 8081 works. And thats how I am working now. But was curious about 8080 as to why its not working. In fact 8080 used to work a few days ago. Now I am puzzled why the hell its not working. – VivekDev Mar 24 '20 at 13:55
4

You assign the same host port 8080 multiple times, which is not allowed - on any operating system.

After running this command

docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1

the first container gets immediately the port 8080 assigned on the host machine, what we can also see in the console screenshot that you provided. And others fail, because they simply don't get the port they want. So that all containers can be started, you should use a different port for each container, a la

docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1
docker run --rm -it -p 8081:80 mcr.microsoft.com/dotnet/core/sdk:3.1
docker run --rm -it -p 8082:80 mcr.microsoft.com/dotnet/core/aspnet:3.1

You should then be able to access those containers via the respective ports 8080, 8081, and 8082 (on localhost or local network IP of ur machine, e.g. 192.168.1.20).

Kenan Güler
  • 1,868
  • 5
  • 16
  • 1
    I was not running all of the three one after another to generate three containers there by getting the port conflict. May be my question presentation was not proper. – VivekDev Mar 24 '20 at 13:57
  • @VivekDev At first glance, it seemed so, yes. But, okay, no problem. Could you try to run the containers all three in a row, as I pointed out in my answer, and give feedback on it. Thanks. – Kenan Güler Mar 24 '20 at 14:16
  • I mean, as feedback you can share the output of `docker ps`, after you start the containers. – Kenan Güler Mar 24 '20 at 14:17
  • 8080 is giving the problem as I had said. And with the other two I had to run them on different command prompts and not on the same. Then they are working. The reason is -it flag. Its in interactive mode and get back a command prompt from within the created container – VivekDev Mar 24 '20 at 14:22
  • The interesting thing is, with 8080, I get back the error I said. But if you I look at docker ps -a output, I see that a container is created but its in a stopped state. – VivekDev Mar 24 '20 at 14:27
  • 1
    Well, maybe the port `8080` is blocked by something else other than docker. I could just run all 3 containers on my Linux machine and it worked. If you want to let them work in the background, you can use the `-d` flag instead of `-it` (which u probably already know). – Kenan Güler Mar 24 '20 at 14:38
1

This answer solves two errors I believe.

Error 1 (if the wrong port is specified in the Windows Defender Firewall for an existing rule for Docker):

Unable to find image 'docker102tutorial:latest' locally docker: Error response from daemon: pull access denied for docker102tutorial, repository does not exist or may require 'docker login': denied: requested access to the resource is denied.

Error 2 (if no Windows Defender Firewall rule at all and #:8080 is specified in docker run command in the -p parameter):

Error response from daemon: Ports are not available: listen tcp 0.0.0.0:8080: bind: An attempt was made to access a socket in a way forbidden by its access permissions.

In Windows 10, you will need to allow this through the Windows Defender Firewall. You'll get this dialog. You might be able to restrict the port for either the TCP by default (or UDP) line in the Windows Defender Firewall. The table screen shot of rules was taken before the port was modified and the last error was corrected. I believe the client in this case is WSL 2 and the server is Windows which means the incoming port needs to be opened on the server.

enter image description here

enter image description here

Allow Local Port 8080 in the Windows Defender Firewall so it matches the port after the ":" in the run command:

enter image description here

You will then get this error.

enter image description here

To correct, change from "Defer to user" to "Defer to application"

enter image description here

JustBeingHelpful
  • 18,332
  • 38
  • 160
  • 245
0

You can assign external port in the following ways:

  • Add an EXPOSE instruction in the Dockerfile such as EXPOSE 8080
  • Use the –expose flag at runtime to expose a port like the following: docker -expose=8080 test
  • Use the -p flag or -P flag in the Docker run string to publish a port as mentioned above, i.e. docker run --rm -it -p 8080:80 mcr.microsoft.com/dotnet/core/runtime:3.1

But you are allowed to do it only ONCE

Roman
  • 19,236
  • 15
  • 93
  • 97
0

Open PowerShell in administrator mode and enter the command

net stop http

The command lists (and gives you the option to stop) all services currently using port 80.

You can now start your docker container with port 80.

Udo E.
  • 2,665
  • 2
  • 21
  • 33