新建容器时映射某些端口,如下:
主机将自动给容器的22和80映射两个端口,如:
0.0.0.0:49155->22/tcp, 0.0.0.0:49156->80/tcp
当该容器停止并且删除后,49155和49156这两个主机端口应该被释放,留给新的容器使用,但实际此时再新建容器映射端口发现,这两并未使用,而是系统自动分配这俩端口后面新的端口,比如49157...
只有docker服务重启后原来空出来的端口才会被分配。在三台不同的docker主机上做实验都是如此,貌似不是我配置的问题,不知大家的环境如何?求解!
You can know by viewing the source code
The way to allocate ports is in increasing order. After reaching the maximum value, the cycle starts from the beginningdocker / runtime / networkdriver / portallocator / portallocator.go
Docker's dynamic range port is from 49153-65535Exception.
To sum up, the port has actually been released, but will not be used again immediately unless the port resources are very tight.findNextPort
方法从nexPort
方法返回的值中,挑一个没被使用的,如果找一圈没找到,抛出ErrAllPortsAllocated
So what you see is normal, don’t worry (provided you need to use version v0.10 or above).
Reference: https://github.com/dotcloud/docker/pull/4949The feature of recycling ports was added in version v0.10. If it is lower than this version, after the port number reaches
, you can only restart Docker
EndPortRange