Docker – running docker gives Ports are not available error

docker

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?

Best Answer

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