Skip to content
This repository has been archived by the owner on Dec 7, 2023. It is now read-only.

Fix Docker client port mappings by actually exposing them after binding #350

Merged
merged 1 commit into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions pkg/runtime/docker/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,19 +118,21 @@ func (dc *dockerClient) RunContainer(image string, config *runtime.ContainerConf
}

stopTimeout := int(config.StopTimeout)
bindings, exposed := portBindingsToDocker(config.PortBindings)

c, err := dc.client.ContainerCreate(context.Background(), &container.Config{
Hostname: config.Hostname,
Tty: true, // --tty
OpenStdin: true, // --interactive
Cmd: config.Cmd,
Image: image,
Labels: config.Labels,
StopTimeout: &stopTimeout,
Hostname: config.Hostname,
ExposedPorts: exposed,
Tty: true, // --tty
OpenStdin: true, // --interactive
Cmd: config.Cmd,
Image: image,
Labels: config.Labels,
StopTimeout: &stopTimeout,
}, &container.HostConfig{
Binds: binds,
NetworkMode: container.NetworkMode(config.NetworkMode),
PortBindings: portBindingsToPortMap(config.PortBindings),
PortBindings: bindings,
AutoRemove: config.AutoRemove,
CapAdd: config.CapAdds,
Resources: container.Resources{
Expand Down
12 changes: 8 additions & 4 deletions pkg/runtime/docker/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
meta "github.com/weaveworks/ignite/pkg/apis/meta/v1alpha1"
)

func portBindingsToPortMap(portMappings meta.PortMappings) nat.PortMap {
portMap := make(nat.PortMap)
// portBindingsToDocker takes in portMappings and returns a nat.PortMap of the
// port bindings and a nat.PortSet of the exposed ports for the Docker client
func portBindingsToDocker(portMappings meta.PortMappings) (nat.PortMap, nat.PortSet) {
bindings, exposed := make(nat.PortMap, len(portMappings)), make(nat.PortSet, len(portMappings))

for _, portMapping := range portMappings {
var hostIP string
Expand All @@ -23,13 +25,15 @@ func portBindingsToPortMap(portMappings meta.PortMappings) nat.PortMap {
protocol = meta.ProtocolTCP
}

portMap[nat.Port(fmt.Sprintf("%d/%s", portMapping.VMPort, protocol.String()))] = []nat.PortBinding{
port := nat.Port(fmt.Sprintf("%d/%s", portMapping.VMPort, protocol.String()))
exposed[port] = struct{}{}
bindings[port] = []nat.PortBinding{
{
HostIP: hostIP,
HostPort: strconv.FormatUint(portMapping.HostPort, 10),
},
}
}

return portMap
return bindings, exposed
}