Description
We met "port already allocated" problem in our docker environment. It always happened after docker-daemon restarts abnormally or machine restarts abnormally.
I read the related code, and found a possible bug about this.
In the source code, the process of creating a container using port mapping is like below:
- create container
- create sandbox
- create endpoint
- allocate ports
- update driver endpoint store
- join sandbox
- update sandbox store
And the restore process after the daemon restarts is like below:
- restore port mapping based on driver endpoint restore
- clean up sandbox based on sandbox store
- clean up endpoint based on endpoint store
In the creating process, if the docker daemon or the machine restarts abnormally between step 5 and step 7 which truely happened in our environment, after docker daemon restarts, the port mapping would be restored in step 1 of restore process and will not be released in step 2 of restore process because sandbox was not updated, which causing ports leak.
I have made a simple fix which has been tested in our environment. I will make a PR later. Looking forward to your suggestions!