Home > other >  Container, after deleting host mapping to the container port why and not recycled immediately?
Container, after deleting host mapping to the container port why and not recycled immediately?

Time:10-03

Host will automatically to the container of 22 and 80 mapping two ports, such as:
0.0.0.0:49155 - & gt; 22/TCP, 0.0.0.0:49156 - & gt; 80/TCP
When the container to stop and remove, 49155 and 49156 the two host port should be released and used for new container, but at this point to the new container mapping port, found that the two did not use, but the system automatically assigns new port behind the two ports, such as 49157...
Only docker service after restart the original free port will be allocated, doing experiments on three different docker host, seemingly not my configuration problem, I don't know how to our environment? To solve!

CodePudding user response:

By viewing the source docker/runtime/networkdriver/portallocator/portallocator may go know
The dynamic range of docker port from 49153-65535

Const (
BeginPortRange=49153
EndPortRange=65535
)
Increasing order, distribution of port to the maximum, to start from scratch cycle

Func nextPort (proto string) int {
C:=currentDynamicPort [proto] + 1
If c & gt; EndPortRange {
C=BeginPortRange
}
CurrentDynamicPort [proto]=c
Return c
}
Container is destroyed, will release port

Func ReleasePort (IP net. IP, proto string, port int) error {
.

Allocated:=defaultAllocatedPorts [proto]
Allocated. Remove (port)

.
}
FindNextPort method from nexPort method returns the value of one is not used, if find a circle didn't find, is thrown ErrAllPortsAllocated abnormalities,

Func findNextPort (proto string, allocated * collections in OrderedIntSet) (int, error) {
Port:=nextPort (proto)
StartSearchPort:=port
For allocated. The Exists (port) {
Port=nextPort (proto)
If startSearchPort==port {
Return 0, ErrAllPortsAllocated
}
}
Return the port, nil
}
In conclusion, the port has actually been released, but would not be used again, unless the port resources very nervous,
So what you see is normal, need not but heart (premise is you need to use more than v0.10 version),
Recycling port is v0.10 version with this feature, if less than this version, the port number to EndPortRange, will have to restart the Docker
  • Related