Merge pull request #15446 from cpuguy83/better_err_on_exec_err

Return better errors from exec
This commit is contained in:
Jessie Frazelle 2015-08-12 11:13:29 -07:00
commit d3198fa8c4
2 changed files with 18 additions and 21 deletions

View File

@ -64,10 +64,9 @@ func (s *Server) postContainerExecStart(version version.Version, w http.Response
return err return err
} }
var ( var (
execName = vars["name"] execName = vars["name"]
stdin io.ReadCloser stdin, inStream io.ReadCloser
stdout io.Writer stdout, stderr, outStream io.Writer
stderr io.Writer
) )
execStartCheck := &types.ExecStartCheck{} execStartCheck := &types.ExecStartCheck{}
@ -76,15 +75,14 @@ func (s *Server) postContainerExecStart(version version.Version, w http.Response
} }
if !execStartCheck.Detach { if !execStartCheck.Detach {
var err error
// Setting up the streaming http interface. // Setting up the streaming http interface.
inStream, outStream, err := hijackServer(w) inStream, outStream, err = hijackServer(w)
if err != nil { if err != nil {
return err return err
} }
defer closeStreams(inStream, outStream) defer closeStreams(inStream, outStream)
var errStream io.Writer
if _, ok := r.Header["Upgrade"]; ok { if _, ok := r.Header["Upgrade"]; ok {
fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n") fmt.Fprintf(outStream, "HTTP/1.1 101 UPGRADED\r\nContent-Type: application/vnd.docker.raw-stream\r\nConnection: Upgrade\r\nUpgrade: tcp\r\n\r\n")
} else { } else {
@ -92,22 +90,16 @@ func (s *Server) postContainerExecStart(version version.Version, w http.Response
} }
if !execStartCheck.Tty { if !execStartCheck.Tty {
errStream = stdcopy.NewStdWriter(outStream, stdcopy.Stderr) stderr = stdcopy.NewStdWriter(outStream, stdcopy.Stderr)
outStream = stdcopy.NewStdWriter(outStream, stdcopy.Stdout) stdout = stdcopy.NewStdWriter(outStream, stdcopy.Stdout)
} }
stdin = inStream stdin = inStream
stdout = outStream
stderr = errStream
} }
// Now run the user process in container. // Now run the user process in container.
if err := s.daemon.ContainerExecStart(execName, stdin, stdout, stderr); err != nil { if err := s.daemon.ContainerExecStart(execName, stdin, stdout, stderr); err != nil {
logrus.Errorf("Error starting exec command in container %s: %s", execName, err) fmt.Fprintf(outStream, "Error running exec in container: %v\n", err)
return err
} }
w.WriteHeader(http.StatusNoContent)
return nil return nil
} }

View File

@ -175,7 +175,6 @@ func (d *Daemon) ContainerExecCreate(config *runconfig.ExecConfig) (string, erro
} }
func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error { func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout io.Writer, stderr io.Writer) error {
var ( var (
cStdin io.ReadCloser cStdin io.ReadCloser
cStdout, cStderr io.Writer cStdout, cStderr io.Writer
@ -246,12 +245,18 @@ func (d *Daemon) ContainerExecStart(execName string, stdin io.ReadCloser, stdout
if err != nil { if err != nil {
return fmt.Errorf("attach failed with error: %s", err) return fmt.Errorf("attach failed with error: %s", err)
} }
break return nil
case err := <-execErr: case err := <-execErr:
if err == nil {
return nil
}
// Maybe the container stopped while we were trying to exec
if !container.IsRunning() {
return fmt.Errorf("container stopped while running exec")
}
return err return err
} }
return nil
} }
func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) { func (d *Daemon) Exec(c *Container, execConfig *execConfig, pipes *execdriver.Pipes, startCallback execdriver.StartCallback) (int, error) {