Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed some issues with expose, port mapping, and environment variables #476

Merged
merged 4 commits into from
Nov 20, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
29 changes: 22 additions & 7 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,19 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
exposedPorts := map[docker.Port]struct{}{}

for _, port := range network.ReservedPorts {
// By default we will map the allocated port 1:1 to the container
containerPortInt := port.Value

// If the user has mapped a port using port_map we'll change it here
if len(driverConfig.PortMap) == 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it makes sense to do > 0, and choose the first block?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the first block is always 0. If we're worried about multiple port_map entries we should normalize this instead.

mapped, ok := driverConfig.PortMap[0][port.Label]
if ok {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this up with the previous line?

containerPortInt = mapped
}
}

hostPortStr := strconv.Itoa(port.Value)
containerPort := docker.Port(hostPortStr)
containerPort := docker.Port(strconv.Itoa(containerPortInt))

publishedPorts[containerPort+"/tcp"] = []docker.PortBinding{docker.PortBinding{HostIP: network.IP, HostPort: hostPortStr}}
publishedPorts[containerPort+"/udp"] = []docker.PortBinding{docker.PortBinding{HostIP: network.IP, HostPort: hostPortStr}}
Expand All @@ -261,7 +272,6 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
d.logger.Printf("[DEBUG] driver.docker: exposed port %d", port.Value)
}

containerToHostPortMap := make(map[string]int)
for _, port := range network.DynamicPorts {
// By default we will map the allocated port 1:1 to the container
containerPortInt := port.Value
Expand All @@ -275,7 +285,6 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri
}

hostPortStr := strconv.Itoa(port.Value)
// containerPort := docker.Port(hostPortStr)
containerPort := docker.Port(strconv.Itoa(containerPortInt))

publishedPorts[containerPort+"/tcp"] = []docker.PortBinding{docker.PortBinding{HostIP: network.IP, HostPort: hostPortStr}}
Expand All @@ -284,12 +293,18 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, dri

exposedPorts[containerPort+"/tcp"] = struct{}{}
exposedPorts[containerPort+"/udp"] = struct{}{}
d.logger.Printf("[DEBUG] driver.docker: exposed port %s", hostPortStr)

containerToHostPortMap[string(containerPort)] = port.Value
d.logger.Printf("[DEBUG] driver.docker: exposed port %s", containerPort)
}

env.SetPorts(containerToHostPortMap)
// This was set above in a call to TaskEnvironmentVariables but if we
// have mapped any ports we will need to override them.
//
// TODO refactor the implementation in TaskEnvironmentVariables to match
// the 0.2 ports world view. Docker seems to be the only place where
// this is actually needed, but this is kinda hacky.
if len(driverConfig.PortMap) == 1 {
env.SetPorts(network.MapLabelToValues(driverConfig.PortMap[0]))
}
hostConfig.PortBindings = publishedPorts
config.ExposedPorts = exposedPorts
}
Expand Down
Loading