Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker driver: allow to configure working directory #1513

Merged
merged 2 commits into from
Aug 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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