diff --git a/api/api.go b/api/api.go index b240c875..81e10b31 100644 --- a/api/api.go +++ b/api/api.go @@ -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"` diff --git a/engine/docker/convert.go b/engine/docker/convert.go index 2290f8a1..2ebd8722 100644 --- a/engine/docker/convert.go +++ b/engine/docker/convert.go @@ -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. @@ -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 } @@ -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 } diff --git a/engine/spec/spec.go b/engine/spec/spec.go index ad57fa57..22abdd6f 100644 --- a/engine/spec/spec.go +++ b/engine/spec/spec.go @@ -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"` diff --git a/pipeline/runtime/convert.go b/pipeline/runtime/convert.go index 8e3e6a88..ffb26ddb 100644 --- a/pipeline/runtime/convert.go +++ b/pipeline/runtime/convert.go @@ -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,