1
0
mirror of https://github.com/bingohuang/docker-labs.git synced 2025-07-14 01:57:32 +08:00

Return ordered ports to the interface (#101)

Fixes #100
This commit is contained in:
Marcos Nils 2017-03-07 11:04:16 -03:00 committed by GitHub
parent 4394be9cf2
commit b999083ec7
2 changed files with 6 additions and 4 deletions

View File

@ -8,7 +8,7 @@ type checkUsedPortsTask struct {
func (c checkUsedPortsTask) Run(i *Instance) error {
if ports, err := GetUsedPorts(i); err == nil {
for _, p := range ports {
i.setUsedPort(p)
i.setUsedPort(uint16(p))
}
} else {
log.Println(err)

View File

@ -5,6 +5,7 @@ import (
"io"
"log"
"os"
"sort"
"strconv"
"strings"
@ -100,7 +101,7 @@ func SetInstanceSwarmPorts(i *Instance) error {
return nil
}
func GetUsedPorts(i *Instance) ([]uint16, error) {
func GetUsedPorts(i *Instance) ([]int, error) {
if i.dockerClient == nil {
return nil, fmt.Errorf("Docker client for DinD (%s) is not ready", i.IP)
}
@ -110,15 +111,16 @@ func GetUsedPorts(i *Instance) ([]uint16, error) {
return nil, err
}
openPorts := []uint16{}
openPorts := sort.IntSlice{}
for _, c := range containers {
for _, p := range c.Ports {
// When port is not published on the host docker return public port as 0, so we need to avoid it
if p.PublicPort != 0 {
openPorts = append(openPorts, p.PublicPort)
openPorts = append(openPorts, int(p.PublicPort))
}
}
}
sort.Sort(openPorts)
return openPorts, nil
}