6
votes

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?

4

4 Answers

5
votes

-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).

3
votes

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).

1
votes

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

0
votes

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