sort ports and select the smallest

This commit is contained in:
yusing 2024-03-01 11:01:41 +08:00
parent 6e829907e0
commit 47733ec05f

View file

@ -9,6 +9,7 @@ import (
"net/url" "net/url"
"reflect" "reflect"
"runtime" "runtime"
"sort"
"strings" "strings"
"time" "time"
@ -36,6 +37,7 @@ type Route struct {
var dockerClient *client.Client var dockerClient *client.Client
var subdomainRouteMap map[string][]Route // subdomain -> path var subdomainRouteMap map[string][]Route // subdomain -> path
// TODO: default + per proxy
var transport = &http.Transport{ var transport = &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{ DialContext: (&net.Dialer{
@ -158,6 +160,11 @@ func buildContainerCfg(container types.Container) {
} }
} }
if config.Port == "" { if config.Port == "" {
// usually the smaller port is the http one
// so make it the last one to be set (if 80 or 8080 are not exposed)
sort.Slice(container.Ports, func(i, j int) bool {
return container.Ports[i].PrivatePort > container.Ports[j].PrivatePort
})
for _, port := range container.Ports { for _, port := range container.Ports {
// set first, but keep trying // set first, but keep trying
config.Port = fmt.Sprintf("%d", port.PrivatePort) config.Port = fmt.Sprintf("%d", port.PrivatePort)