diff --git a/client/driver/docker.go b/client/driver/docker.go index 987b740d4a8..bf0ef89a538 100644 --- a/client/driver/docker.go +++ b/client/driver/docker.go @@ -405,7 +405,10 @@ func (d *DockerDriver) containerBinds(driverConfig *DockerDriverConfig, alloc *a binds := []string{allocDirBind, taskLocalBind, secretDirBind} volumesEnabled := d.config.ReadBoolDefault(dockerVolumesConfigOption, dockerVolumesConfigDefault) - for _, userbind := range driverConfig.Volumes { + + // Expand environment variables in volume paths + expandedVols := d.taskEnv.ParseAndReplace(driverConfig.Volumes) + for _, userbind := range expandedVols { parts := strings.Split(userbind, ":") if len(parts) < 2 { return nil, fmt.Errorf("invalid docker volume: %q", userbind) diff --git a/client/driver/docker_test.go b/client/driver/docker_test.go index 4c02905d45f..8a868f22fb7 100644 --- a/client/driver/docker_test.go +++ b/client/driver/docker_test.go @@ -950,16 +950,18 @@ func setupDockerVolumes(t *testing.T, cfg *config.Config, hostpath string) (*str randfn := fmt.Sprintf("test-%d", rand.Int()) hostfile := filepath.Join(hostpath, randfn) - containerFile := filepath.Join("/mnt/vol", randfn) + containerPath := "/mnt/vol" + containerFile := filepath.Join(containerPath, randfn) task := &structs.Task{ Name: "ls", + Env: map[string]string{"VOL_PATH": containerPath}, Config: map[string]interface{}{ "image": "busybox", "load": []string{"busybox.tar"}, "command": "touch", "args": []string{containerFile}, - "volumes": []string{fmt.Sprintf("%s:/mnt/vol", hostpath)}, + "volumes": []string{fmt.Sprintf("%s:${VOL_PATH}", hostpath)}, }, LogConfig: &structs.LogConfig{ MaxFiles: 10,