Enable setting networking mode for docker

This patch enables setting networking mode for the docker
driver. This does not handle the `container` mode.
Closes #175
This commit is contained in:
Abhishek Chanda
2015-09-30 19:15:24 -07:00
parent 2351a97776
commit ee2cd3bed0
3 changed files with 48 additions and 0 deletions

View File

@@ -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 {

View File

@@ -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()
}

View File

@@ -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