{"action":"created","comment":{"url":"https://api.github.com/repos/docker/docker/pulls/comments/29012893","id":29012893,"diff_hunk":"@@ -26,3 +29,55 @@ func Sort(ports []Port, predicate func(i, j Port) bool) {\n \ts := &portSorter{ports, predicate}\n \tsort.Sort(s)\n }\n+\n+type portMapEntry struct {\n+\tport    Port\n+\tbinding PortBinding\n+}\n+\n+type portMapSorter []portMapEntry\n+\n+func (s portMapSorter) Len() int      { return len(s) }\n+func (s portMapSorter) Swap(i, j int) { s[i], s[j] = s[j], s[i] }\n+func (s portMapSorter) Less(i, j int) bool {\n+\tpi, pj := toInt(string(s[i].port)), toInt(string(s[j].port))\n+\thpi, hpj := toInt(s[i].binding.HostPort), toInt(s[j].binding.HostPort)\n+\treturn (hpi > hpj) || (pi > pj)\n+}\n+\n+// SortPortMap sorts the list of ports and their respected mapping. The ports\n+// will explicit HostPort will be placed first.\n+func SortPortMap(ports []Port, bindings PortMap) {\n+\ts := portMapSorter{}\n+\tfor _, p := range ports {\n+\t\tbinding := bindings[p]\n+\t\tfor _, b := range binding {\n+\t\t\ts = append(s, portMapEntry{port: p, binding: b})\n+\t\t}\n+\t\tbindings[p] = []PortBinding{}\n+\t}\n+\n+\tsort.Sort(s)\n+\tvar (\n+\t\ti  int\n+\t\tpm = make(map[Port]struct{})\n+\t)\n+\t// reorder ports\n+\tfor _, entry := range s {\n+\t\tif _, ok := pm[entry.port]; !ok {\n+\t\t\tports[i] = entry.port\n+\t\t\tpm[entry.port] = struct{}{}\n+\t\t\ti++","path":"nat/sort.go","position":53,"original_position":53,"commit_id":"636941866533df320300af295f1c12b766533d8c","original_commit_id":"636941866533df320300af295f1c12b766533d8c","user":{"login":"dqminh","id":62707,"avatar_url":"https://avatars.githubusercontent.com/u/62707?v=3","gravatar_id":"","url":"https://api.github.com/users/dqminh","html_url":"https://github.com/dqminh","followers_url":"https://api.github.com/users/dqminh/followers","following_url":"https://api.github.com/users/dqminh/following{/other_user}","gists_url":"https://api.github.com/users/dqminh/gists{/gist_id}","starred_url":"https://api.github.com/users/dqminh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dqminh/subscriptions","organizations_url":"https://api.github.com/users/dqminh/orgs","repos_url":"https://api.github.com/users/dqminh/repos","events_url":"https://api.github.com/users/dqminh/events{/privacy}","received_events_url":"https://api.github.com/users/dqminh/received_events","type":"User","site_admin":false},"body":"i think it's safe. It's only changed when we encounter a new port. This is done so that we can override the original ordering of ports.","created_at":"2015-04-23T23:23:57Z","updated_at":"2015-04-23T23:23:57Z","html_url":"https://github.com/docker/docker/pull/12175#discussion_r29012893","pull_request_url":"https://api.github.com/repos/docker/docker/pulls/12175","_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/comments/29012893"},"html":{"href":"https://github.com/docker/docker/pull/12175#discussion_r29012893"},"pull_request":{"href":"https://api.github.com/repos/docker/docker/pulls/12175"}}},"pull_request":{"url":"https://api.github.com/repos/docker/docker/pulls/12175","id":32834330,"html_url":"https://github.com/docker/docker/pull/12175","diff_url":"https://github.com/docker/docker/pull/12175.diff","patch_url":"https://github.com/docker/docker/pull/12175.patch","issue_url":"https://api.github.com/repos/docker/docker/issues/12175","number":12175,"state":"open","locked":false,"title":"#12148: sort ports mapping before allocating","user":{"login":"dqminh","id":62707,"avatar_url":"https://avatars.githubusercontent.com/u/62707?v=3","gravatar_id":"","url":"https://api.github.com/users/dqminh","html_url":"https://github.com/dqminh","followers_url":"https://api.github.com/users/dqminh/followers","following_url":"https://api.github.com/users/dqminh/following{/other_user}","gists_url":"https://api.github.com/users/dqminh/gists{/gist_id}","starred_url":"https://api.github.com/users/dqminh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dqminh/subscriptions","organizations_url":"https://api.github.com/users/dqminh/orgs","repos_url":"https://api.github.com/users/dqminh/repos","events_url":"https://api.github.com/users/dqminh/events{/privacy}","received_events_url":"https://api.github.com/users/dqminh/received_events","type":"User","site_admin":false},"body":"Fix #12148\r\n\r\nPrioritize 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.\r\n\r\nWhen static port is allocated first, Docker will skip those when determining free ports for dynamic ones.\r\n","created_at":"2015-04-08T07:23:18Z","updated_at":"2015-04-23T23:23:57Z","closed_at":null,"merged_at":null,"merge_commit_sha":"91a5185e800bafea8ed32b923ce21eaac3bce935","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/docker/docker/pulls/12175/commits","review_comments_url":"https://api.github.com/repos/docker/docker/pulls/12175/comments","review_comment_url":"https://api.github.com/repos/docker/docker/pulls/comments{/number}","comments_url":"https://api.github.com/repos/docker/docker/issues/12175/comments","statuses_url":"https://api.github.com/repos/docker/docker/statuses/636941866533df320300af295f1c12b766533d8c","head":{"label":"dqminh:fix-allocate-port","ref":"fix-allocate-port","sha":"636941866533df320300af295f1c12b766533d8c","user":{"login":"dqminh","id":62707,"avatar_url":"https://avatars.githubusercontent.com/u/62707?v=3","gravatar_id":"","url":"https://api.github.com/users/dqminh","html_url":"https://github.com/dqminh","followers_url":"https://api.github.com/users/dqminh/followers","following_url":"https://api.github.com/users/dqminh/following{/other_user}","gists_url":"https://api.github.com/users/dqminh/gists{/gist_id}","starred_url":"https://api.github.com/users/dqminh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dqminh/subscriptions","organizations_url":"https://api.github.com/users/dqminh/orgs","repos_url":"https://api.github.com/users/dqminh/repos","events_url":"https://api.github.com/users/dqminh/events{/privacy}","received_events_url":"https://api.github.com/users/dqminh/received_events","type":"User","site_admin":false},"repo":{"id":23137560,"name":"docker","full_name":"dqminh/docker","owner":{"login":"dqminh","id":62707,"avatar_url":"https://avatars.githubusercontent.com/u/62707?v=3","gravatar_id":"","url":"https://api.github.com/users/dqminh","html_url":"https://github.com/dqminh","followers_url":"https://api.github.com/users/dqminh/followers","following_url":"https://api.github.com/users/dqminh/following{/other_user}","gists_url":"https://api.github.com/users/dqminh/gists{/gist_id}","starred_url":"https://api.github.com/users/dqminh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dqminh/subscriptions","organizations_url":"https://api.github.com/users/dqminh/orgs","repos_url":"https://api.github.com/users/dqminh/repos","events_url":"https://api.github.com/users/dqminh/events{/privacy}","received_events_url":"https://api.github.com/users/dqminh/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/dqminh/docker","description":"Docker - the open-source application container engine","fork":true,"url":"https://api.github.com/repos/dqminh/docker","forks_url":"https://api.github.com/repos/dqminh/docker/forks","keys_url":"https://api.github.com/repos/dqminh/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/dqminh/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/dqminh/docker/teams","hooks_url":"https://api.github.com/repos/dqminh/docker/hooks","issue_events_url":"https://api.github.com/repos/dqminh/docker/issues/events{/number}","events_url":"https://api.github.com/repos/dqminh/docker/events","assignees_url":"https://api.github.com/repos/dqminh/docker/assignees{/user}","branches_url":"https://api.github.com/repos/dqminh/docker/branches{/branch}","tags_url":"https://api.github.com/repos/dqminh/docker/tags","blobs_url":"https://api.github.com/repos/dqminh/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/dqminh/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/dqminh/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/dqminh/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/dqminh/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/dqminh/docker/languages","stargazers_url":"https://api.github.com/repos/dqminh/docker/stargazers","contributors_url":"https://api.github.com/repos/dqminh/docker/contributors","subscribers_url":"https://api.github.com/repos/dqminh/docker/subscribers","subscription_url":"https://api.github.com/repos/dqminh/docker/subscription","commits_url":"https://api.github.com/repos/dqminh/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/dqminh/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/dqminh/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/dqminh/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/dqminh/docker/contents/{+path}","compare_url":"https://api.github.com/repos/dqminh/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/dqminh/docker/merges","archive_url":"https://api.github.com/repos/dqminh/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/dqminh/docker/downloads","issues_url":"https://api.github.com/repos/dqminh/docker/issues{/number}","pulls_url":"https://api.github.com/repos/dqminh/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/dqminh/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/dqminh/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/dqminh/docker/labels{/name}","releases_url":"https://api.github.com/repos/dqminh/docker/releases{/id}","created_at":"2014-08-20T05:52:34Z","updated_at":"2015-04-23T22:32:49Z","pushed_at":"2015-04-23T22:46:20Z","git_url":"git://github.com/dqminh/docker.git","ssh_url":"git@github.com:dqminh/docker.git","clone_url":"https://github.com/dqminh/docker.git","svn_url":"https://github.com/dqminh/docker","homepage":"http://www.docker.com","size":50576,"stargazers_count":1,"watchers_count":1,"language":"Go","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"docker:master","ref":"master","sha":"06433cf812e26e468b9dcb6b0817f6597f1c397c","user":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"repo":{"id":7691631,"name":"docker","full_name":"docker/docker","owner":{"login":"docker","id":5429470,"avatar_url":"https://avatars.githubusercontent.com/u/5429470?v=3","gravatar_id":"","url":"https://api.github.com/users/docker","html_url":"https://github.com/docker","followers_url":"https://api.github.com/users/docker/followers","following_url":"https://api.github.com/users/docker/following{/other_user}","gists_url":"https://api.github.com/users/docker/gists{/gist_id}","starred_url":"https://api.github.com/users/docker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/docker/subscriptions","organizations_url":"https://api.github.com/users/docker/orgs","repos_url":"https://api.github.com/users/docker/repos","events_url":"https://api.github.com/users/docker/events{/privacy}","received_events_url":"https://api.github.com/users/docker/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/docker/docker","description":"Docker - the open-source application container engine","fork":false,"url":"https://api.github.com/repos/docker/docker","forks_url":"https://api.github.com/repos/docker/docker/forks","keys_url":"https://api.github.com/repos/docker/docker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/docker/docker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/docker/docker/teams","hooks_url":"https://api.github.com/repos/docker/docker/hooks","issue_events_url":"https://api.github.com/repos/docker/docker/issues/events{/number}","events_url":"https://api.github.com/repos/docker/docker/events","assignees_url":"https://api.github.com/repos/docker/docker/assignees{/user}","branches_url":"https://api.github.com/repos/docker/docker/branches{/branch}","tags_url":"https://api.github.com/repos/docker/docker/tags","blobs_url":"https://api.github.com/repos/docker/docker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/docker/docker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/docker/docker/git/refs{/sha}","trees_url":"https://api.github.com/repos/docker/docker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/docker/docker/statuses/{sha}","languages_url":"https://api.github.com/repos/docker/docker/languages","stargazers_url":"https://api.github.com/repos/docker/docker/stargazers","contributors_url":"https://api.github.com/repos/docker/docker/contributors","subscribers_url":"https://api.github.com/repos/docker/docker/subscribers","subscription_url":"https://api.github.com/repos/docker/docker/subscription","commits_url":"https://api.github.com/repos/docker/docker/commits{/sha}","git_commits_url":"https://api.github.com/repos/docker/docker/git/commits{/sha}","comments_url":"https://api.github.com/repos/docker/docker/comments{/number}","issue_comment_url":"https://api.github.com/repos/docker/docker/issues/comments{/number}","contents_url":"https://api.github.com/repos/docker/docker/contents/{+path}","compare_url":"https://api.github.com/repos/docker/docker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/docker/docker/merges","archive_url":"https://api.github.com/repos/docker/docker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/docker/docker/downloads","issues_url":"https://api.github.com/repos/docker/docker/issues{/number}","pulls_url":"https://api.github.com/repos/docker/docker/pulls{/number}","milestones_url":"https://api.github.com/repos/docker/docker/milestones{/number}","notifications_url":"https://api.github.com/repos/docker/docker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/docker/docker/labels{/name}","releases_url":"https://api.github.com/repos/docker/docker/releases{/id}","created_at":"2013-01-18T18:10:57Z","updated_at":"2015-04-23T22:42:51Z","pushed_at":"2015-04-23T23:23:34Z","git_url":"git://github.com/docker/docker.git","ssh_url":"git@github.com:docker/docker.git","clone_url":"https://github.com/docker/docker.git","svn_url":"https://github.com/docker/docker","homepage":"http://www.docker.com","size":185727,"stargazers_count":21102,"watchers_count":21102,"language":"Go","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":4875,"mirror_url":null,"open_issues_count":918,"forks":4875,"open_issues":918,"watchers":21102,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/docker/docker/pulls/12175"},"html":{"href":"https://github.com/docker/docker/pull/12175"},"issue":{"href":"https://api.github.com/repos/docker/docker/issues/12175"},"comments":{"href":"https://api.github.com/repos/docker/docker/issues/12175/comments"},"review_comments":{"href":"https://api.github.com/repos/docker/docker/pulls/12175/comments"},"review_comment":{"href":"https://api.github.com/repos/docker/docker/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/docker/docker/pulls/12175/commits"},"statuses":{"href":"https://api.github.com/repos/docker/docker/statuses/636941866533df320300af295f1c12b766533d8c"}}}}