Skip to content

Commit

Permalink
Bind container port to host port (#46)
Browse files Browse the repository at this point in the history
* Bind container port to host port
  • Loading branch information
shubham149 authored Feb 9, 2022
1 parent 02d1535 commit 984164d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ type (
MemLimit int64 `json:"mem_limit,omitempty"`
Network string `json:"network,omitempty"`
Networks []string `json:"networks,omitempty"`
PortBindings map[string]string `json:"port_bindings,omitempty"` // Host port to container port mapping
Privileged bool `json:"privileged,omitempty"`
Pull spec.PullPolicy `json:"pull,omitempty"`
ShmSize int64 `json:"shm_size,omitempty"`
Expand Down
25 changes: 25 additions & 0 deletions engine/docker/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/mount"
"github.com/docker/docker/api/types/network"
"github.com/docker/go-connections/nat"
)

// returns a container configuration.
Expand Down Expand Up @@ -46,6 +47,13 @@ func toConfig(pipelineConfig *spec.PipelineConfig, step *spec.Step) *container.C
if len(step.Volumes) != 0 {
config.Volumes = toVolumeSet(pipelineConfig, step)
}
if len(step.PortBindings) != 0 {
exposedPorts := make(nat.PortSet)
for _, ctrPort := range step.PortBindings {
exposedPorts[nat.Port(ctrPort)] = struct{}{}
}
config.ExposedPorts = exposedPorts
}
return config
}

Expand Down Expand Up @@ -91,6 +99,23 @@ func toHostConfig(pipelineConfig *spec.PipelineConfig, step *spec.Step) *contain
config.Binds = toVolumeSlice(pipelineConfig, step)
config.Mounts = toVolumeMounts(pipelineConfig, step)
}

if len(step.PortBindings) != 0 {
portBinding := make(nat.PortMap)
for hostPort, ctrPort := range step.PortBindings {
p := nat.Port(ctrPort)
if _, ok := portBinding[p]; ok {
portBinding[p] = append(portBinding[p], nat.PortBinding{HostPort: hostPort})
} else {
portBinding[p] = []nat.PortBinding{
{
HostPort: hostPort,
},
}
}
}
config.PortBindings = portBinding
}
return config
}

Expand Down
1 change: 1 addition & 0 deletions engine/spec/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type (
Name string `json:"name,omitempty"`
Network string `json:"network,omitempty"`
Networks []string `json:"networks,omitempty"`
PortBindings map[string]string `json:"port_bindings,omitempty"` // Host port to container port mapping.
Privileged bool `json:"privileged,omitempty"`
Pull PullPolicy `json:"pull,omitempty"`
Secrets []*Secret `json:"secrets,omitempty"`
Expand Down
1 change: 1 addition & 0 deletions pipeline/runtime/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func toStep(r *api.StartStepRequest) *spec.Step {
Name: r.Name,
Network: r.Network,
Networks: r.Networks,
PortBindings: r.PortBindings,
Privileged: r.Privileged,
Pull: r.Pull,
ShmSize: r.ShmSize,
Expand Down

0 comments on commit 984164d

Please sign in to comment.