diff --git a/client/driver/docker.go b/client/driver/docker.go index 3b63989173f..8e71ceedddb 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -96,6 +96,7 @@ type DockerDriverConfig struct { TTY bool `mapstructure:"tty"` // Allocate a Pseudo-TTY Interactive bool `mapstructure:"interactive"` // Keep STDIN open even if not attached ShmSize int64 `mapstructure:"shm_size"` // Size of /dev/shm of the container in bytes + WorkDir string `mapstructure:"work_dir"` // Working directory inside the container } // Validate validates a docker driver config @@ -223,6 +224,9 @@ func (d *DockerDriver) Validate(config map[string]interface{}) error { "shm_size": &fields.FieldSchema{ Type: fields.TypeInt, }, + "work_dir": &fields.FieldSchema{ + Type: fields.TypeString, + }, }, } @@ -386,6 +390,10 @@ func (d *DockerDriver) createContainer(ctx *ExecContext, task *structs.Task, OpenStdin: driverConfig.Interactive, } + if driverConfig.WorkDir != "" { + config.WorkingDir = driverConfig.WorkDir + } + memLimit := int64(task.Resources.MemoryMB) * 1024 * 1024 hostConfig := &docker.HostConfig{ // Convert MB to bytes. This is an absolute value. diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 3229c0fd385..6dcae2a7178 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -605,6 +605,24 @@ func TestDockerDNS(t *testing.T) { } } +func TestDockerWorkDir(t *testing.T) { + t.Parallel() + task, _, _ := dockerTask() + task.Config["work_dir"] = "/some/path" + + client, handle, cleanup := dockerSetup(t, task) + defer cleanup() + + container, err := client.InspectContainer(handle.(*DockerHandle).ContainerID()) + if err != nil { + t.Fatalf("err: %v", err) + } + + if want, got := "/some/path", container.Config.WorkingDir; want != got { + t.Errorf("Wrong working directory for docker job. Expect: %d, got: %d", want, got) + } +} + func inSlice(needle string, haystack []string) bool { for _, h := range haystack { if h == needle { diff --git a/website/source/docs/drivers/docker.html.md b/website/source/docs/drivers/docker.html.md index d17c9af3d1b..fc5f0660be5 100644 --- a/website/source/docs/drivers/docker.html.md +++ b/website/source/docs/drivers/docker.html.md @@ -107,6 +107,8 @@ The following options are available for use in the job specification. * `shm_size` - (Optional) The size (bytes) of /dev/shm for the container. +* `work_dir` - (Optional) The working directory inside the container. + ### Container Name Nomad creates a container after pulling an image. Containers are named