diff --git a/container.go b/container.go index 707872187a..1e44ddd76c 100644 --- a/container.go +++ b/container.go @@ -1188,10 +1188,18 @@ func (container *Container) allocateNetwork() error { portJob.Setenv("Proto", port.Proto()) portJob.Setenv("ContainerPort", port.Port()) + portEnv, err := portJob.Stdout.AddEnv() + if err != nil { + return err + } if err := portJob.Run(); err != nil { eng.Job("release_interface", container.ID).Run() return err } + b.HostIp = portEnv.Get("HostIP") + b.HostPort = portEnv.Get("HostPort") + + binding[i] = b } bindings[port] = binding } diff --git a/networkdriver/lxc/driver.go b/networkdriver/lxc/driver.go index 84e7b3ef95..ab7a18808b 100644 --- a/networkdriver/lxc/driver.go +++ b/networkdriver/lxc/driver.go @@ -50,10 +50,10 @@ var ( "192.168.44.1/24", } - bridgeIface string - defaultBindingIP net.IP - bridgeNetwork *net.IPNet + bridgeIface string + bridgeNetwork *net.IPNet + defaultBindingIP = net.ParseIP("0.0.0.0") currentInterfaces = make(map[string]*networkInterface) ) @@ -72,7 +72,9 @@ func InitDriver(job *engine.Job) engine.Status { bridgeIP = job.Getenv("BridgeIP") ) - defaultBindingIP = net.ParseIP(job.Getenv("DefaultBindingIP")) + if defaultIP := job.Getenv("DefaultBindingIP"); defaultIP != "" { + defaultBindingIP = net.ParseIP(defaultIP) + } bridgeIface = job.Getenv("BridgeIface") if bridgeIface == "" { @@ -382,6 +384,8 @@ func Release(job *engine.Job) engine.Status { // Allocate an external port and map it to the interface func AllocatePort(job *engine.Job) engine.Status { var ( + err error + ip = defaultBindingIP id = job.Args[0] hostIP = job.Getenv("HostIP") @@ -396,7 +400,7 @@ func AllocatePort(job *engine.Job) engine.Status { } // host ip, proto, and host port - hostPort, err := portallocator.RequestPort(ip, proto, hostPort) + hostPort, err = portallocator.RequestPort(ip, proto, hostPort) if err != nil { job.Error(err) return engine.StatusErr @@ -423,6 +427,14 @@ func AllocatePort(job *engine.Job) engine.Status { } network.PortMappings = append(network.PortMappings, host) + out := engine.Env{} + out.Set("HostIP", ip.String()) + out.SetInt("HostPort", hostPort) + + if _, err := out.WriteTo(job.Stdout); err != nil { + job.Error(err) + return engine.StatusErr + } return engine.StatusOK } diff --git a/networkdriver/portallocator/portallocator.go b/networkdriver/portallocator/portallocator.go index 2566ea8500..71cac82703 100644 --- a/networkdriver/portallocator/portallocator.go +++ b/networkdriver/portallocator/portallocator.go @@ -51,7 +51,7 @@ func RequestPort(ip net.IP, proto string, port int) (int, error) { } // If the user requested a specific port to be allocated - if port != 0 { + if port > 0 { if err := registerSetPort(ip, proto, port); err != nil { return 0, err }