Skip to content

Commit 266accf

Browse files
authored
Port mapping does not show external IP when using static IP (#8416)
When using static IP for container network, the port mapping will not be shown when the gateway is not set. If container network is from DHCP, so everything is working well because we can get gateway, netmask from DHCP ask packet. But if we set a static IP, the DHCP request will not be sent for container network, so gateway will be missed.
1 parent 4fa5961 commit 266accf

3 files changed

Lines changed: 47 additions & 1 deletion

File tree

lib/apiservers/portlayer/restapi/handlers/containers_handlers.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,12 +646,18 @@ func convertContainerToContainerInfo(c *exec.Container) *models.ContainerInfo {
646646
Direct: endpoint.Network.Type == constants.ExternalScopeType,
647647
}
648648

649+
// for static endpoints we may not have forced a state refresh and therefore not have the address copied into the Assigned fields.
650+
// In this case we used the configured data directly.
649651
if !ip.IsUnspecifiedIP(endpoint.Network.Assigned.Gateway.IP) {
650652
ep.Gateway = endpoint.Network.Assigned.Gateway.IP.String()
653+
} else if endpoint.Static {
654+
ep.Gateway = endpoint.Network.Gateway.IP.String()
651655
}
652656

653657
if !ip.IsUnspecifiedIP(endpoint.Assigned.IP) {
654658
ep.Address = endpoint.Assigned.String()
659+
} else if endpoint.Static {
660+
ep.Address = endpoint.IP.String()
655661
}
656662

657663
if len(endpoint.Ports) > 0 {

lib/portlayer/network/context.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1115,6 +1115,15 @@ func (c *Context) AddContainer(h *exec.Handle, options *AddContainerOptions) err
11151115
}
11161116

11171117
if s.Type() == constants.ExternalScopeType {
1118+
// Check that specified static IP address for container network should only be
1119+
// allow when gateway is set
1120+
if len(options.IP) > 0 && !ip.IsUnspecifiedIP(options.IP) {
1121+
if ip.IsUnspecifiedIP(s.Gateway()) {
1122+
err = fmt.Errorf("Cannot set static IP (%s) when default gateway is not set for network %s", options.IP.String(), s.Name())
1123+
log.Errorln(err)
1124+
return err
1125+
}
1126+
}
11181127
// Check that ports are only opened on published network firewall configuration.
11191128
// Redirects are allow for all network types other than Closed and Outbound
11201129
if len(options.Ports) > 0 {
@@ -1209,14 +1218,14 @@ func (c *Context) AddContainer(h *exec.Handle, options *AddContainerOptions) err
12091218
}
12101219

12111220
ne.Static = false
1221+
12121222
if len(options.IP) > 0 && !ip.IsUnspecifiedIP(options.IP) {
12131223
ne.Static = true
12141224
ne.IP = &net.IPNet{
12151225
IP: options.IP,
12161226
Mask: s.Subnet().Mask,
12171227
}
12181228
}
1219-
12201229
h.ExecConfig.Networks[s.Name()] = ne
12211230
return nil
12221231
}

tests/test-cases/Group1-Docker-Commands/1-10-Docker-PS.robot

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,34 @@ Docker ps with volume and network filters
362362
Should Contain ${output} buzFooContainer
363363
${output}= Split To Lines ${output}
364364
Length Should Be ${output} 2
365+
366+
367+
Docker ps container with specified static IP
368+
${rc}= Run And Return Rc docker %{VCH-PARAMS} pull ${nginx}
369+
Should Be Equal As Integers ${rc} 0
370+
371+
# published via the container-network with specified static ip
372+
${rc} ${output}= Run And Return Rc And Output docker %{VCH-PARAMS} create -p 8080:80 --net=public --ip 60.0.9.150 ${nginx}
373+
Should Contain ${output} Cannot set static IP (60.0.9.150) when default gateway is not set for network public
374+
375+
Set Suite Variable ${old-vch} %{VCH-NAME}
376+
Set Suite Variable ${old-vch-params} %{VCH-PARAMS}
377+
Set Suite Variable ${old-vch-bridge} %{BRIDGE_NETWORK}
378+
Set Suite Variable ${old-vic-admin} %{VIC-ADMIN}
379+
Install VIC Appliance To Test Server additional-args=--container-network-gateway %{CONTAINER_NETWORK}:50.0.9.1/24 --container-network-firewall=%{CONTAINER_NETWORK}:open
380+
${rc}= Run And Return Rc docker %{VCH-PARAMS} pull ${nginx}
381+
Should Be Equal As Integers ${rc} 0
382+
383+
${rc} ${output}= Run And Return Rc And Output docker %{VCH-PARAMS} run -d -p 8080:80 --net=public --ip 50.0.9.150 ${nginx}
384+
Should Be Equal As Integers ${rc} 0
385+
386+
${rc} ${output}= Run And Return Rc And Output docker %{VCH-PARAMS} ps
387+
Log To Console ${output}
388+
Should Be Equal As Integers ${rc} 0
389+
Should Contain ${output} 50.0.9.150:8080->80/tcp
390+
391+
Cleanup VIC Appliance On Test Server
392+
Set Environment Variable VCH-NAME ${old-vch}
393+
Set Environment Variable BRIDGE_NETWORK ${old-vch-bridge}
394+
Set Environment Variable VCH-PARAMS ${old-vch-params}
395+
Set Environment Variable VIC-ADMIN ${old-vic-admin}

0 commit comments

Comments
 (0)