Skip to content

Commit

Permalink
Merge pull request #184 from achanda/docker_net
Browse files Browse the repository at this point in the history
Enable setting networking mode for docker
  • Loading branch information
dadgar committed Oct 2, 2015
2 parents 43b2a7d + ee2cd3b commit 96b6109
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
17 changes: 17 additions & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ func createContainer(ctx *ExecContext, task *structs.Task, logger *log.Logger) d
logger.Printf("[DEBUG] driver.docker: using %d bytes memory for %s", hostConfig.Memory, task.Config["image"])
logger.Printf("[DEBUG] driver.docker: using %d cpu shares for %s", hostConfig.CPUShares, task.Config["image"])

mode, ok := task.Config["network_mode"]
if !ok || mode == "" {
// docker default
logger.Printf("[WARN] driver.docker: no mode specified for networking, defaulting to bridge")
mode = "bridge"
}

// Ignore the container mode for now
switch mode {
case "default", "bridge", "none", "host":
logger.Printf("[DEBUG] driver.docker: using %s as network mode", mode)
default:
logger.Printf("[WARN] invalid setting for network mode %s, defaulting to bridge mode on docker0", mode)
mode = "bridge"
}
hostConfig.NetworkMode = mode

// Setup port mapping (equivalent to -p on docker CLI). Ports must already be
// exposed in the container.
if len(task.Resources.Networks) == 0 {
Expand Down
26 changes: 26 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,29 @@ func TestDocker_StartNVersions(t *testing.T) {

t.Log("==> Test complete!")
}

func TestDockerHostNet(t *testing.T) {
task := &structs.Task{
Config: map[string]string{
"image": "redis",
"network_mode": "host",
},
Resources: &structs.Resources{
MemoryMB: 256,
CPU: 512,
},
}
driverCtx := testDriverContext(task.Name)
ctx := testDriverExecContext(task, driverCtx)
defer ctx.AllocDir.Destroy()
d := NewDockerDriver(driverCtx)

handle, err := d.Start(ctx, task)
if err != nil {
t.Fatalf("err: %v", err)
}
if handle == nil {
t.Fatalf("missing handle")
}
defer handle.Kill()
}
5 changes: 5 additions & 0 deletions website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ The `docker` driver supports the following configuration in the job specificatio

* `command` - (Optional) The command to run when starting the container.

* `network_mode` - (Optional) The network mode to be used for the container.
Valid options are `net`, `bridge`, `host` or `none`. If nothing is
specified, the container will start in `bridge` mode. The `container`
network mode is not supported right now.

### Port Mapping

Nomad uses port binding to expose services running in containers using the port
Expand Down

0 comments on commit 96b6109

Please sign in to comment.