Windows Docker port occupation error resolution

Windows Docker port occupation error resolution

Error source

Error invoking remote method ‘docker-start-container’: Error: (HTTP code 500) server error - Ports are not available: exposing port TCP 192.168.0.157:6555 -> 0.0.0.0:0: listen tcp 192.168.0.157:6555: can’t bind on the specified endpoint.

or

Error invoking remote method ‘docker-start-container’: error: (http code 500) server error - ports are not available.

or

Error invoking remote method ‘docker-start-container’: Error: (HTTP code 500) server error - Ports are not available: listen tcp 0.0.0.0:xxxx: bind: An attempt was made to access a socket in a way forbidden by access permissions.

These are all port occupation issues. In many cases, Windows will reserve some TCP ports, and these port ranges are unavailable:

  • There is something called "TCP dynamic port range" in Windows. Ports in this range are sometimes occupied by some services. Before Windows Vista (or Windows Server 2008), the dynamic port range was 1025 to 5000; in subsequent versions, the new default start port is 49152 and the new default end port is 65535.
  • If Hyper-V is installed, Hyper-V reserves some random port numbers for use by the Windows Container Host Network Service.
    Generally (under normal circumstances) Hyper-V will reserve some random port numbers in the "TCP Dynamic Port Range", but the reserved port numbers are generally very large, so even if hundreds or thousands of ports are reserved, It doesn't have much impact either. However, Windows automatic updates sometimes go wrong (evil automatic updates), and the starting port of the "TCP Dynamic Port Range" is reset to 1024, causing Hyper-V to occupy commonly used port numbers when reserving ports, making some commonly used ports Unavailable because it is reserved.

Dynamic port reuse is a common technology in operating systems.
First of all, whether sending or receiving, you need to listen to the port
because the sender's port is generally not mandatory as long as it is not 80 443 3306 3389. Common ports and ports with special meanings are fine and can be sent along with it. And close it in time after receiving the return to facilitate reuse by other programs.
Therefore, with this technology, this is why most http libraries and browsers do not require you to specify your own sending port.
Similarly, Linux also has this technology.

You can use netsh int ipv4 show dynamicport tcpthe command to view the tcp dynamic port range:

image-20230424145640377

Use netsh int ipv4 show excludedportrange protocol=tcpthe command to view the TCP port exclusion range:

image-20230424145940036

Solution

1. Restart directly

Generally, the port allocation bug of Hyper-V will be eliminated after restarting, and the problem will naturally disappear, but this is not necessarily the case. Occasionally, there may be situations where restarting cannot solve the problem.

2. Reassign Hyper-V port range

Simply reset the "TCP Dynamic Port Range" so that Hyper-V only retains ports within the range we set. You can reset the TCP Dynamic Port Range to 49152–65535 by running the following command with administrator privileges, but you can also change it to a smaller range if you think it is too large.

Please execute the following command in the command line to set the dynamic port range:

netsh int ipv4 set dynamic tcp start=49152 num=16384
netsh int ipv6 set dynamic tcp start=49152 num=16384

Then restart the computer.

3. Let Hyper-V re-randomly allocate ports without restarting

Error solution from StackOverflow :

net stop winnat
docker start container_name
net start winnat

The essence of this command is a simplified version of restarting the computer and letting Hyper-V initialize some random ports to reserve. If it still does not release the ports you need, you may need to do it again. That is to say, some people's replies below this answer are useful, and some people's replies are useless, because the probability of solving the problem with this solution is very random.

references

1: Completely solve the port binding problem of docker on windows

2: Solve the problem that the port is occupied when Docker starts the container under Windows - bkycmd - Blog Park

3:docker - Ports are not available: listen tcp 0.0.0.0/50070: bind: An attempt was made to access a socket in a way forbidden by its access permissions - Stack Overflow

4: Unable to start the container, prompting that the port cannot be used, but cannot find which process is occupying it? _UVE’s Blog-CSDN Blog

If you have any questions or errors, please feel free to message me privately for corrections.
All rights reserved. Please do not reproduce without authorization!
Copyright © 2023 by Mr.Idleman. All rights reserved.

Guess you like

Origin blog.csdn.net/qq_42059060/article/details/130343788