Signed-off-by: Alessandro Boch <aboch@docker.com>
| ... | ... |
@@ -290,7 +290,6 @@ func (container *Container) BuildJoinOptions(n libnetwork.Network) ([]libnetwork |
| 290 | 290 |
// BuildCreateEndpointOptions builds endpoint options from a given network. |
| 291 | 291 |
func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network, epConfig *network.EndpointSettings, sb libnetwork.Sandbox) ([]libnetwork.EndpointOption, error) {
|
| 292 | 292 |
var ( |
| 293 |
- portSpecs = make(nat.PortSet) |
|
| 294 | 293 |
bindings = make(nat.PortMap) |
| 295 | 294 |
pbList []types.PortBinding |
| 296 | 295 |
exposeList []types.TransportPort |
| ... | ... |
@@ -343,10 +342,6 @@ func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network, epC |
| 343 | 343 |
return createOptions, nil |
| 344 | 344 |
} |
| 345 | 345 |
|
| 346 |
- if container.Config.ExposedPorts != nil {
|
|
| 347 |
- portSpecs = container.Config.ExposedPorts |
|
| 348 |
- } |
|
| 349 |
- |
|
| 350 | 346 |
if container.HostConfig.PortBindings != nil {
|
| 351 | 347 |
for p, b := range container.HostConfig.PortBindings {
|
| 352 | 348 |
bindings[p] = []nat.PortBinding{}
|
| ... | ... |
@@ -359,6 +354,7 @@ func (container *Container) BuildCreateEndpointOptions(n libnetwork.Network, epC |
| 359 | 359 |
} |
| 360 | 360 |
} |
| 361 | 361 |
|
| 362 |
+ portSpecs := container.Config.ExposedPorts |
|
| 362 | 363 |
ports := make([]nat.Port, len(portSpecs)) |
| 363 | 364 |
var i int |
| 364 | 365 |
for p := range portSpecs {
|
| ... | ... |
@@ -4,6 +4,7 @@ package daemon |
| 4 | 4 |
|
| 5 | 5 |
import ( |
| 6 | 6 |
"fmt" |
| 7 |
+ "net" |
|
| 7 | 8 |
"os" |
| 8 | 9 |
"path" |
| 9 | 10 |
"path/filepath" |
| ... | ... |
@@ -25,10 +26,12 @@ import ( |
| 25 | 25 |
"github.com/docker/docker/runconfig" |
| 26 | 26 |
containertypes "github.com/docker/engine-api/types/container" |
| 27 | 27 |
networktypes "github.com/docker/engine-api/types/network" |
| 28 |
+ "github.com/docker/go-connections/nat" |
|
| 28 | 29 |
"github.com/docker/go-units" |
| 29 | 30 |
"github.com/docker/libnetwork" |
| 30 | 31 |
"github.com/docker/libnetwork/netlabel" |
| 31 | 32 |
"github.com/docker/libnetwork/options" |
| 33 |
+ "github.com/docker/libnetwork/types" |
|
| 32 | 34 |
"github.com/opencontainers/runc/libcontainer/configs" |
| 33 | 35 |
"github.com/opencontainers/runc/libcontainer/devices" |
| 34 | 36 |
"github.com/opencontainers/runc/libcontainer/label" |
| ... | ... |
@@ -320,6 +323,9 @@ func (daemon *Daemon) buildSandboxOptions(container *container.Container, n libn |
| 320 | 320 |
dns []string |
| 321 | 321 |
dnsSearch []string |
| 322 | 322 |
dnsOptions []string |
| 323 |
+ bindings = make(nat.PortMap) |
|
| 324 |
+ pbList []types.PortBinding |
|
| 325 |
+ exposeList []types.TransportPort |
|
| 323 | 326 |
) |
| 324 | 327 |
|
| 325 | 328 |
sboxOptions = append(sboxOptions, libnetwork.OptionHostname(container.Config.Hostname), |
| ... | ... |
@@ -394,6 +400,59 @@ func (daemon *Daemon) buildSandboxOptions(container *container.Container, n libn |
| 394 | 394 |
sboxOptions = append(sboxOptions, libnetwork.OptionExtraHost(parts[0], parts[1])) |
| 395 | 395 |
} |
| 396 | 396 |
|
| 397 |
+ if container.HostConfig.PortBindings != nil {
|
|
| 398 |
+ for p, b := range container.HostConfig.PortBindings {
|
|
| 399 |
+ bindings[p] = []nat.PortBinding{}
|
|
| 400 |
+ for _, bb := range b {
|
|
| 401 |
+ bindings[p] = append(bindings[p], nat.PortBinding{
|
|
| 402 |
+ HostIP: bb.HostIP, |
|
| 403 |
+ HostPort: bb.HostPort, |
|
| 404 |
+ }) |
|
| 405 |
+ } |
|
| 406 |
+ } |
|
| 407 |
+ } |
|
| 408 |
+ |
|
| 409 |
+ portSpecs := container.Config.ExposedPorts |
|
| 410 |
+ ports := make([]nat.Port, len(portSpecs)) |
|
| 411 |
+ var i int |
|
| 412 |
+ for p := range portSpecs {
|
|
| 413 |
+ ports[i] = p |
|
| 414 |
+ i++ |
|
| 415 |
+ } |
|
| 416 |
+ nat.SortPortMap(ports, bindings) |
|
| 417 |
+ for _, port := range ports {
|
|
| 418 |
+ expose := types.TransportPort{}
|
|
| 419 |
+ expose.Proto = types.ParseProtocol(port.Proto()) |
|
| 420 |
+ expose.Port = uint16(port.Int()) |
|
| 421 |
+ exposeList = append(exposeList, expose) |
|
| 422 |
+ |
|
| 423 |
+ pb := types.PortBinding{Port: expose.Port, Proto: expose.Proto}
|
|
| 424 |
+ binding := bindings[port] |
|
| 425 |
+ for i := 0; i < len(binding); i++ {
|
|
| 426 |
+ pbCopy := pb.GetCopy() |
|
| 427 |
+ newP, err := nat.NewPort(nat.SplitProtoPort(binding[i].HostPort)) |
|
| 428 |
+ var portStart, portEnd int |
|
| 429 |
+ if err == nil {
|
|
| 430 |
+ portStart, portEnd, err = newP.Range() |
|
| 431 |
+ } |
|
| 432 |
+ if err != nil {
|
|
| 433 |
+ return nil, fmt.Errorf("Error parsing HostPort value(%s):%v", binding[i].HostPort, err)
|
|
| 434 |
+ } |
|
| 435 |
+ pbCopy.HostPort = uint16(portStart) |
|
| 436 |
+ pbCopy.HostPortEnd = uint16(portEnd) |
|
| 437 |
+ pbCopy.HostIP = net.ParseIP(binding[i].HostIP) |
|
| 438 |
+ pbList = append(pbList, pbCopy) |
|
| 439 |
+ } |
|
| 440 |
+ |
|
| 441 |
+ if container.HostConfig.PublishAllPorts && len(binding) == 0 {
|
|
| 442 |
+ pbList = append(pbList, pb) |
|
| 443 |
+ } |
|
| 444 |
+ } |
|
| 445 |
+ |
|
| 446 |
+ sboxOptions = append(sboxOptions, |
|
| 447 |
+ libnetwork.OptionPortMapping(pbList), |
|
| 448 |
+ libnetwork.OptionExposedPorts(exposeList)) |
|
| 449 |
+ |
|
| 397 | 450 |
// Link feature is supported only for the default bridge network. |
| 398 | 451 |
// return if this call to build join options is not for default bridge network |
| 399 | 452 |
if n.Name() != "bridge" {
|