'What's the automatic port assigning strategy in Linux kernel?

I found a specific port is used more frequently than others inside my docker occasionally.

Then I toke a test inside docker and inside a vm as a control group:

  • Server: listen 1000 - 1099 port; do nothing until client closing the connection
  • Client: connect to the server and close the connection; do it to port 1000 - 1099 consequently
  • Repeate: the client repeates this action for 1000 times; totally 100,000 connecting actions
  • Statistic: client ports range; ports used count

The result is as following:

|============= target_ip ===============|============ control_group ============|
| Total connection time: 100000         | Total connection time: 100000         |
| Min port used by client: 32770        | Min port used by client: 32768        |
| Max port used by client: 60989        | Max port used by client: 61000        |
| Count of used ports: 2581             | Count of used ports: 27399            |
| System config ip_local_port_range:    | System config ip_local_port_range:    |
|   [32768,61000]                       |    [32768,61000]                      |
| Top 20 frequent ports used by client: | Top 20 frequent ports used by client: |
|                                       |                                       |
|   Count Port                          |   Count Port                          |
|    6913 52002                         |      13 56601                         |
|    1081 52003                         |      13 56501                         |
|     331 54853                         |      13 56401                         |
|     298 42098                         |      13 56301                         |
|     291 58356                         |      13 56201                         |
|     287 60847                         |      13 56101                         |
|     286 56269                         |      13 56001                         |
|=======================================|=======================================|

Somary:

  • Inside docker, only 2581 ports were used in 100,000 times, which only taking 1/10 share.
  • On the control group, 27399 ports were used, covering almost all net.ipv4.ip_local_port_range
  • 52002 and 52003 are used more frequently than other ports inside docker

The kernel versions in docker and vm are the same - 3.10.107

More information:

The docker's host has set net.ipv4.ip_local_reserved_ports = 50000-52000.

Could you help me figure out the reason lead to several concentrating ports used?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source