sort ports mapping before allocating

prioritize the ports with static mapping before dynamic mapping. This removes
the port conflicts when we allocate static port in the reserved range
together with dynamic ones.
When static port is allocated first, Docker will skip those when determining
free ports for dynamic ones.

Signed-off-by: Daniel, Dao Quang Minh <dqminh89@gmail.com>
This commit is contained in:
Daniel, Dao Quang Minh
2015-04-30 02:18:04 +00:00
parent 79d086c47d
commit cd2b019214
4 changed files with 150 additions and 2 deletions
+33
View File
@@ -2243,6 +2243,39 @@ func (s *DockerSuite) TestRunPortProxy(c *check.C) {
}
}
// https://github.com/docker/docker/issues/12148
func (s *DockerSuite) TestRunAllocatePortInReservedRange(c *check.C) {
// allocate a dynamic port to get the most recent
cmd := exec.Command(dockerBinary, "run", "-d", "-P", "-p", "80", "busybox", "top")
out, _, err := runCommandWithOutput(cmd)
if err != nil {
c.Fatalf("Failed to run, output: %s, error: %s", out, err)
}
id := strings.TrimSpace(out)
cmd = exec.Command(dockerBinary, "port", id, "80")
out, _, err = runCommandWithOutput(cmd)
if err != nil {
c.Fatalf("Failed to get port, output: %s, error: %s", out, err)
}
strPort := strings.Split(strings.TrimSpace(out), ":")[1]
port, err := strconv.ParseInt(strPort, 10, 64)
if err != nil {
c.Fatalf("invalid port, got: %s, error: %s", strPort, err)
}
// allocate a static port and a dynamic port together, with static port
// takes the next recent port in dynamic port range.
cmd = exec.Command(dockerBinary, "run", "-d", "-P",
"-p", "80",
"-p", fmt.Sprintf("%d:8080", port+1),
"busybox", "top")
out, _, err = runCommandWithOutput(cmd)
if err != nil {
c.Fatalf("Failed to run, output: %s, error: %s", out, err)
}
}
// Regression test for #7792
func (s *DockerSuite) TestRunMountOrdering(c *check.C) {
testRequires(c, SameHostDaemon)