diff --git a/integration/commands_test.go b/integration/commands_test.go index f1c5870755..7de7a227ea 100644 --- a/integration/commands_test.go +++ b/integration/commands_test.go @@ -252,6 +252,25 @@ func TestRunWorkdirExists(t *testing.T) { } } +// TestRunWorkdirExistsAndIsFile checks that if 'docker run -w' with existing file can be detected +func TestRunWorkdirExistsAndIsFile(t *testing.T) { + + cli := api.NewDockerCli(nil, nil, ioutil.Discard, testDaemonProto, testDaemonAddr, nil) + defer cleanup(globalEngine, t) + + c := make(chan struct{}) + go func() { + defer close(c) + if err := cli.CmdRun("-w", "/bin/cat", unitTestImageID, "pwd"); err == nil { + t.Fatal("should have failed to run when using /bin/cat as working dir.") + } + }() + + setTimeout(t, "CmdRun timed out", 5*time.Second, func() { + <-c + }) +} + func TestRunExit(t *testing.T) { stdin, stdinPipe := io.Pipe() stdout, stdoutPipe := io.Pipe() diff --git a/runtime/container.go b/runtime/container.go index 53d0aa666e..4cf307d823 100644 --- a/runtime/container.go +++ b/runtime/container.go @@ -536,8 +536,18 @@ func (container *Container) Start() (err error) { if container.Config.WorkingDir != "" { container.Config.WorkingDir = path.Clean(container.Config.WorkingDir) - if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil { - return nil + + pthInfo, err := os.Stat(path.Join(container.basefs, container.Config.WorkingDir)) + if err != nil { + if !os.IsNotExist(err) { + return err + } + if err := os.MkdirAll(path.Join(container.basefs, container.Config.WorkingDir), 0755); err != nil { + return err + } + } + if pthInfo != nil && !pthInfo.IsDir() { + return fmt.Errorf("Cannot mkdir: %s is not a directory", container.Config.WorkingDir) } } @@ -959,10 +969,11 @@ func (container *Container) ExportRw() (archive.Archive, error) { return nil, err } return utils.NewReadCloserWrapper(archive, func() error { - err := archive.Close() - container.Unmount() - return err - }), nil + err := archive.Close() + container.Unmount() + return err + }), + nil } func (container *Container) Export() (archive.Archive, error) { @@ -976,10 +987,11 @@ func (container *Container) Export() (archive.Archive, error) { return nil, err } return utils.NewReadCloserWrapper(archive, func() error { - err := archive.Close() - container.Unmount() - return err - }), nil + err := archive.Close() + container.Unmount() + return err + }), + nil } func (container *Container) WaitTimeout(timeout time.Duration) error { @@ -1128,10 +1140,11 @@ func (container *Container) Copy(resource string) (io.ReadCloser, error) { return nil, err } return utils.NewReadCloserWrapper(archive, func() error { - err := archive.Close() - container.Unmount() - return err - }), nil + err := archive.Close() + container.Unmount() + return err + }), + nil } // Returns true if the container exposes a certain port