Fix guards for docker port mapping and change dummy dynamic ports to real ports (0 is not a valid port)
This commit is contained in:
parent
cb9d7f7677
commit
9a7adb9eb3
|
@ -245,7 +245,7 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
|
||||||
// Setup port mapping and exposed ports
|
// Setup port mapping and exposed ports
|
||||||
if len(task.Resources.Networks) == 0 {
|
if len(task.Resources.Networks) == 0 {
|
||||||
d.logger.Println("[DEBUG] driver.docker: No network interfaces are available")
|
d.logger.Println("[DEBUG] driver.docker: No network interfaces are available")
|
||||||
if len(driverConfig.PortMap[0]) > 0 {
|
if len(driverConfig.PortMap) == 1 && len(driverConfig.PortMap[0]) > 0 {
|
||||||
return c, fmt.Errorf("Trying to map ports but no network interface is available")
|
return c, fmt.Errorf("Trying to map ports but no network interface is available")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -269,9 +269,15 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
|
||||||
|
|
||||||
containerToHostPortMap := make(map[string]int)
|
containerToHostPortMap := make(map[string]int)
|
||||||
for _, port := range network.DynamicPorts {
|
for _, port := range network.DynamicPorts {
|
||||||
containerPort, ok := driverConfig.PortMap[0][port.Label]
|
// By default we will map the allocated port 1:1 to the container
|
||||||
if !ok {
|
containerPort := port.Value
|
||||||
containerPort = port.Value
|
|
||||||
|
// If the user has mapped a port using port_map we'll change it here
|
||||||
|
if len(driverConfig.PortMap) == 1 {
|
||||||
|
mapped, ok := driverConfig.PortMap[0][port.Label]
|
||||||
|
if ok {
|
||||||
|
containerPort = mapped
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
containerPortStr := docker.Port(strconv.Itoa(containerPort))
|
containerPortStr := docker.Port(strconv.Itoa(containerPort))
|
||||||
|
@ -318,7 +324,7 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
|
||||||
|
|
||||||
config.Env = env.List()
|
config.Env = env.List()
|
||||||
return docker.CreateContainerOptions{
|
return docker.CreateContainerOptions{
|
||||||
Name: fmt.Sprintf("%s-%s", task.Name, ctx.AllocID),
|
// Name: fmt.Sprintf("%s-%s", task.Name, ctx.AllocID),
|
||||||
Config: config,
|
Config: config,
|
||||||
HostConfig: hostConfig,
|
HostConfig: hostConfig,
|
||||||
}, nil
|
}, nil
|
||||||
|
@ -392,7 +398,7 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle
|
||||||
// Now that we have the image we can get the image id
|
// Now that we have the image we can get the image id
|
||||||
dockerImage, err = client.InspectImage(image)
|
dockerImage, err = client.InspectImage(image)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logger.Printf("[ERR] driver.docker: failed getting image id for %s\n", image)
|
d.logger.Printf("[ERR] driver.docker: failed getting image id for %s: %s\n", image, err)
|
||||||
return nil, fmt.Errorf("Failed to determine image id for `%s`: %s", image, err)
|
return nil, fmt.Errorf("Failed to determine image id for `%s`: %s", image, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -407,15 +413,15 @@ func (d *DockerDriver) Start(ctx *ExecContext, task *structs.Task) (DriverHandle
|
||||||
container, err := client.CreateContainer(config)
|
container, err := client.CreateContainer(config)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logger.Printf("[ERR] driver.docker: failed to create container from image %s: %s\n", image, err)
|
d.logger.Printf("[ERR] driver.docker: failed to create container from image %s: %s\n", image, err)
|
||||||
return nil, fmt.Errorf("Failed to create container from image %s", image)
|
return nil, fmt.Errorf("Failed to create container from image %s: %s", image, err)
|
||||||
}
|
}
|
||||||
d.logger.Printf("[INFO] driver.docker: created container %s\n", container.ID)
|
d.logger.Printf("[INFO] driver.docker: created container %s\n", container.ID)
|
||||||
|
|
||||||
// Start the container
|
// Start the container
|
||||||
err = client.StartContainer(container.ID, container.HostConfig)
|
err = client.StartContainer(container.ID, container.HostConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logger.Printf("[ERR] driver.docker: starting container %s\n", container.ID)
|
d.logger.Printf("[ERR] driver.docker: failed to start container %s: %s\n", container.ID, err)
|
||||||
return nil, fmt.Errorf("Failed to start container %s", container.ID)
|
return nil, fmt.Errorf("Failed to start container %s: %s", container.ID, err)
|
||||||
}
|
}
|
||||||
d.logger.Printf("[INFO] driver.docker: started container %s\n", container.ID)
|
d.logger.Printf("[INFO] driver.docker: started container %s\n", container.ID)
|
||||||
|
|
||||||
|
|
|
@ -293,7 +293,7 @@ func taskTemplate() *structs.Task {
|
||||||
&structs.NetworkResource{
|
&structs.NetworkResource{
|
||||||
IP: "127.0.0.1",
|
IP: "127.0.0.1",
|
||||||
ReservedPorts: []structs.Port{{"main", 11110}},
|
ReservedPorts: []structs.Port{{"main", 11110}},
|
||||||
DynamicPorts: []structs.Port{{"REDIS", 0}},
|
DynamicPorts: []structs.Port{{"REDIS", 43330}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -307,12 +307,15 @@ func TestDocker_StartN(t *testing.T) {
|
||||||
|
|
||||||
task1 := taskTemplate()
|
task1 := taskTemplate()
|
||||||
task1.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 11110}
|
task1.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 11110}
|
||||||
|
task1.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43331}
|
||||||
|
|
||||||
task2 := taskTemplate()
|
task2 := taskTemplate()
|
||||||
task2.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 22222}
|
task2.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 22222}
|
||||||
|
task2.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43332}
|
||||||
|
|
||||||
task3 := taskTemplate()
|
task3 := taskTemplate()
|
||||||
task3.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 33333}
|
task3.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 33333}
|
||||||
|
task3.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43333}
|
||||||
|
|
||||||
taskList := []*structs.Task{task1, task2, task3}
|
taskList := []*structs.Task{task1, task2, task3}
|
||||||
|
|
||||||
|
@ -359,14 +362,17 @@ func TestDocker_StartNVersions(t *testing.T) {
|
||||||
task1 := taskTemplate()
|
task1 := taskTemplate()
|
||||||
task1.Config["image"] = "redis"
|
task1.Config["image"] = "redis"
|
||||||
task1.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 11110}
|
task1.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 11110}
|
||||||
|
task1.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43331}
|
||||||
|
|
||||||
task2 := taskTemplate()
|
task2 := taskTemplate()
|
||||||
task2.Config["image"] = "redis:latest"
|
task2.Config["image"] = "redis:latest"
|
||||||
task2.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 22222}
|
task2.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 22222}
|
||||||
|
task2.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43332}
|
||||||
|
|
||||||
task3 := taskTemplate()
|
task3 := taskTemplate()
|
||||||
task3.Config["image"] = "redis:3.0"
|
task3.Config["image"] = "redis:3.0"
|
||||||
task3.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 33333}
|
task3.Resources.Networks[0].ReservedPorts[0] = structs.Port{"main", 33333}
|
||||||
|
task3.Resources.Networks[0].DynamicPorts[0] = structs.Port{"REDIS", 43333}
|
||||||
|
|
||||||
taskList := []*structs.Task{task1, task2, task3}
|
taskList := []*structs.Task{task1, task2, task3}
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,7 @@ var basicResources = &structs.Resources{
|
||||||
&structs.NetworkResource{
|
&structs.NetworkResource{
|
||||||
IP: "0.0.0.0",
|
IP: "0.0.0.0",
|
||||||
ReservedPorts: []structs.Port{{"main", 12345}},
|
ReservedPorts: []structs.Port{{"main", 12345}},
|
||||||
DynamicPorts: []structs.Port{{"HTTP", 0}},
|
DynamicPorts: []structs.Port{{"HTTP", 43330}},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue