Skip to content

Commit

Permalink
Merge pull request #1513 from mlafeldt/workdir
Browse files Browse the repository at this point in the history
Docker driver: allow to configure working directory
  • Loading branch information
dadgar committed Aug 3, 2016
2 parents 3b77948 + 27d787e commit 91a5304
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
8 changes: 8 additions & 0 deletions client/driver/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
},
}

Expand Down Expand Up @@ -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.
Expand Down
18 changes: 18 additions & 0 deletions client/driver/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions website/source/docs/drivers/docker.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 91a5304

Please sign in to comment.