mirror of https://github.com/docker/docs.git
Merge pull request #5958 from erikh/5738-docker_port_in_use
docker port in use
This commit is contained in:
commit
b7f9e683c3
|
@ -380,7 +380,7 @@ func AllocatePort(job *engine.Job) engine.Status {
|
||||||
ip = defaultBindingIP
|
ip = defaultBindingIP
|
||||||
id = job.Args[0]
|
id = job.Args[0]
|
||||||
hostIP = job.Getenv("HostIP")
|
hostIP = job.Getenv("HostIP")
|
||||||
hostPort = job.GetenvInt("HostPort")
|
origHostPort = job.GetenvInt("HostPort")
|
||||||
containerPort = job.GetenvInt("ContainerPort")
|
containerPort = job.GetenvInt("ContainerPort")
|
||||||
proto = job.Getenv("Proto")
|
proto = job.Getenv("Proto")
|
||||||
network = currentInterfaces[id]
|
network = currentInterfaces[id]
|
||||||
|
@ -390,29 +390,45 @@ func AllocatePort(job *engine.Job) engine.Status {
|
||||||
ip = net.ParseIP(hostIP)
|
ip = net.ParseIP(hostIP)
|
||||||
}
|
}
|
||||||
|
|
||||||
// host ip, proto, and host port
|
|
||||||
hostPort, err = portallocator.RequestPort(ip, proto, hostPort)
|
|
||||||
if err != nil {
|
|
||||||
return job.Error(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
hostPort int
|
||||||
container net.Addr
|
container net.Addr
|
||||||
host net.Addr
|
host net.Addr
|
||||||
)
|
)
|
||||||
|
|
||||||
if proto == "tcp" {
|
/*
|
||||||
host = &net.TCPAddr{IP: ip, Port: hostPort}
|
Try up to 10 times to get a port that's not already allocated.
|
||||||
container = &net.TCPAddr{IP: network.IP, Port: containerPort}
|
|
||||||
} else {
|
In the event of failure to bind, return the error that portmapper.Map
|
||||||
host = &net.UDPAddr{IP: ip, Port: hostPort}
|
yields.
|
||||||
container = &net.UDPAddr{IP: network.IP, Port: containerPort}
|
*/
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
// host ip, proto, and host port
|
||||||
|
hostPort, err = portallocator.RequestPort(ip, proto, origHostPort)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return job.Error(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if proto == "tcp" {
|
||||||
|
host = &net.TCPAddr{IP: ip, Port: hostPort}
|
||||||
|
container = &net.TCPAddr{IP: network.IP, Port: containerPort}
|
||||||
|
} else {
|
||||||
|
host = &net.UDPAddr{IP: ip, Port: hostPort}
|
||||||
|
container = &net.UDPAddr{IP: network.IP, Port: containerPort}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = portmapper.Map(container, ip, hostPort); err == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
job.Logf("Failed to bind %s:%d for container address %s:%d. Trying another port.", ip.String(), hostPort, network.IP.String(), containerPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := portmapper.Map(container, ip, hostPort); err != nil {
|
if err != nil {
|
||||||
portallocator.ReleasePort(ip, proto, hostPort)
|
|
||||||
return job.Error(err)
|
return job.Error(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
network.PortMappings = append(network.PortMappings, host)
|
network.PortMappings = append(network.PortMappings, host)
|
||||||
|
|
||||||
out := engine.Env{}
|
out := engine.Env{}
|
||||||
|
|
|
@ -2,21 +2,21 @@ package portallocator
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"github.com/dotcloud/docker/pkg/collections"
|
|
||||||
"net"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type (
|
||||||
|
portMap map[int]bool
|
||||||
|
protocolMap map[string]portMap
|
||||||
|
ipMapping map[string]protocolMap
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
BeginPortRange = 49153
|
BeginPortRange = 49153
|
||||||
EndPortRange = 65535
|
EndPortRange = 65535
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
|
||||||
portMappings map[string]*collections.OrderedIntSet
|
|
||||||
ipMapping map[string]portMappings
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
var (
|
||||||
ErrAllPortsAllocated = errors.New("all ports are allocated")
|
ErrAllPortsAllocated = errors.New("all ports are allocated")
|
||||||
ErrPortAlreadyAllocated = errors.New("port has already been allocated")
|
ErrPortAlreadyAllocated = errors.New("port has already been allocated")
|
||||||
|
@ -24,165 +24,106 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
currentDynamicPort = map[string]int{
|
mutex sync.Mutex
|
||||||
"tcp": BeginPortRange - 1,
|
|
||||||
"udp": BeginPortRange - 1,
|
defaultIP = net.ParseIP("0.0.0.0")
|
||||||
}
|
globalMap = ipMapping{}
|
||||||
defaultIP = net.ParseIP("0.0.0.0")
|
|
||||||
defaultAllocatedPorts = portMappings{}
|
|
||||||
otherAllocatedPorts = ipMapping{}
|
|
||||||
lock = sync.Mutex{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
defaultAllocatedPorts["tcp"] = collections.NewOrderedIntSet()
|
|
||||||
defaultAllocatedPorts["udp"] = collections.NewOrderedIntSet()
|
|
||||||
}
|
|
||||||
|
|
||||||
// RequestPort returns an available port if the port is 0
|
|
||||||
// If the provided port is not 0 then it will be checked if
|
|
||||||
// it is available for allocation
|
|
||||||
func RequestPort(ip net.IP, proto string, port int) (int, error) {
|
func RequestPort(ip net.IP, proto string, port int) (int, error) {
|
||||||
lock.Lock()
|
mutex.Lock()
|
||||||
defer lock.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
if err := validateProtocol(proto); err != nil {
|
if err := validateProto(proto); err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the user requested a specific port to be allocated
|
ip = getDefault(ip)
|
||||||
|
|
||||||
|
mapping := getOrCreate(ip)
|
||||||
|
|
||||||
if port > 0 {
|
if port > 0 {
|
||||||
if err := registerSetPort(ip, proto, port); err != nil {
|
if !mapping[proto][port] {
|
||||||
|
mapping[proto][port] = true
|
||||||
|
return port, nil
|
||||||
|
} else {
|
||||||
|
return 0, ErrPortAlreadyAllocated
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
port, err := findPort(ip, proto)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
return registerDynamicPort(ip, proto)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleasePort will return the provided port back into the
|
|
||||||
// pool for reuse
|
|
||||||
func ReleasePort(ip net.IP, proto string, port int) error {
|
func ReleasePort(ip net.IP, proto string, port int) error {
|
||||||
lock.Lock()
|
mutex.Lock()
|
||||||
defer lock.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
if err := validateProtocol(proto); err != nil {
|
ip = getDefault(ip)
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
allocated := defaultAllocatedPorts[proto]
|
mapping := getOrCreate(ip)
|
||||||
allocated.Remove(port)
|
delete(mapping[proto], port)
|
||||||
|
|
||||||
if !equalsDefault(ip) {
|
|
||||||
registerIP(ip)
|
|
||||||
|
|
||||||
// Remove the port for the specific ip address
|
|
||||||
allocated = otherAllocatedPorts[ip.String()][proto]
|
|
||||||
allocated.Remove(port)
|
|
||||||
}
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReleaseAll() error {
|
func ReleaseAll() error {
|
||||||
lock.Lock()
|
mutex.Lock()
|
||||||
defer lock.Unlock()
|
defer mutex.Unlock()
|
||||||
|
|
||||||
currentDynamicPort["tcp"] = BeginPortRange - 1
|
globalMap = ipMapping{}
|
||||||
currentDynamicPort["udp"] = BeginPortRange - 1
|
|
||||||
|
|
||||||
defaultAllocatedPorts = portMappings{}
|
|
||||||
defaultAllocatedPorts["tcp"] = collections.NewOrderedIntSet()
|
|
||||||
defaultAllocatedPorts["udp"] = collections.NewOrderedIntSet()
|
|
||||||
|
|
||||||
otherAllocatedPorts = ipMapping{}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerDynamicPort(ip net.IP, proto string) (int, error) {
|
func getOrCreate(ip net.IP) protocolMap {
|
||||||
|
ipstr := ip.String()
|
||||||
|
|
||||||
if !equalsDefault(ip) {
|
if _, ok := globalMap[ipstr]; !ok {
|
||||||
registerIP(ip)
|
globalMap[ipstr] = protocolMap{
|
||||||
|
"tcp": portMap{},
|
||||||
ipAllocated := otherAllocatedPorts[ip.String()][proto]
|
"udp": portMap{},
|
||||||
|
|
||||||
port, err := findNextPort(proto, ipAllocated)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
}
|
||||||
ipAllocated.Push(port)
|
|
||||||
return port, nil
|
|
||||||
|
|
||||||
} else {
|
|
||||||
|
|
||||||
allocated := defaultAllocatedPorts[proto]
|
|
||||||
|
|
||||||
port, err := findNextPort(proto, allocated)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
allocated.Push(port)
|
|
||||||
return port, nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerSetPort(ip net.IP, proto string, port int) error {
|
|
||||||
allocated := defaultAllocatedPorts[proto]
|
|
||||||
if allocated.Exists(port) {
|
|
||||||
return ErrPortAlreadyAllocated
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if !equalsDefault(ip) {
|
return globalMap[ipstr]
|
||||||
registerIP(ip)
|
|
||||||
|
|
||||||
ipAllocated := otherAllocatedPorts[ip.String()][proto]
|
|
||||||
if ipAllocated.Exists(port) {
|
|
||||||
return ErrPortAlreadyAllocated
|
|
||||||
}
|
|
||||||
ipAllocated.Push(port)
|
|
||||||
} else {
|
|
||||||
allocated.Push(port)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func equalsDefault(ip net.IP) bool {
|
func findPort(ip net.IP, proto string) (int, error) {
|
||||||
return ip == nil || ip.Equal(defaultIP)
|
port := BeginPortRange
|
||||||
}
|
|
||||||
|
|
||||||
func findNextPort(proto string, allocated *collections.OrderedIntSet) (int, error) {
|
mapping := getOrCreate(ip)
|
||||||
port := nextPort(proto)
|
|
||||||
startSearchPort := port
|
for mapping[proto][port] {
|
||||||
for allocated.Exists(port) {
|
port++
|
||||||
port = nextPort(proto)
|
|
||||||
if startSearchPort == port {
|
if port > EndPortRange {
|
||||||
return 0, ErrAllPortsAllocated
|
return 0, ErrAllPortsAllocated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mapping[proto][port] = true
|
||||||
|
|
||||||
return port, nil
|
return port, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func nextPort(proto string) int {
|
func getDefault(ip net.IP) net.IP {
|
||||||
c := currentDynamicPort[proto] + 1
|
if ip == nil {
|
||||||
if c > EndPortRange {
|
return defaultIP
|
||||||
c = BeginPortRange
|
|
||||||
}
|
}
|
||||||
currentDynamicPort[proto] = c
|
|
||||||
return c
|
return ip
|
||||||
}
|
}
|
||||||
|
|
||||||
func registerIP(ip net.IP) {
|
func validateProto(proto string) error {
|
||||||
if _, exists := otherAllocatedPorts[ip.String()]; !exists {
|
if proto != "tcp" && proto != "udp" {
|
||||||
otherAllocatedPorts[ip.String()] = portMappings{
|
|
||||||
"tcp": collections.NewOrderedIntSet(),
|
|
||||||
"udp": collections.NewOrderedIntSet(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func validateProtocol(proto string) error {
|
|
||||||
if _, exists := defaultAllocatedPorts[proto]; !exists {
|
|
||||||
return ErrUnknownProtocol
|
return ErrUnknownProtocol
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue