Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running docker gives Ports are not available error

Tags:

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?

like image 406
VivekDev Avatar asked Mar 24 '20 12:03

VivekDev


People also ask

How do I check if port is running on Docker?

Check your Docker daemon. After restarting docker service, you can see the port in the output of systemctl status docker. service like /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock . Thank you!

How do you expose a port 8080 Docker?

To publish a port for our container, we'll use the --publish flag ( -p for short) on the docker run command. The format of the --publish command is [host_port]:[container_port] . So if we wanted to expose port 8080 inside the container to port 3000 outside the container, we would pass 3000:8080 to the --publish flag.


2 Answers

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

like image 86
Itamar Turner-Trauring Avatar answered Sep 28 '22 01:09

Itamar Turner-Trauring


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

like image 40
Kenan Güler Avatar answered Sep 28 '22 01:09

Kenan Güler