diff --git a/client/driver/docker.go b/client/driver/docker.go index 6995d2b25f6..beca0c5095f 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -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 { diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 074aa5984db..916327e6067 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -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() +} diff --git a/website/source/docs/drivers/docker.html.md b/website/source/docs/drivers/docker.html.md index 22a2a2c24c8..ba890e59f64 100644 --- a/website/source/docs/drivers/docker.html.md +++ b/website/source/docs/drivers/docker.html.md @@ -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