diff --git a/container.go b/container.go index 57ec531737..7c1caf4d57 100644 --- a/container.go +++ b/container.go @@ -415,11 +415,13 @@ func (container *Container) Restart() error { return nil } -func (container *Container) Wait() { +// Wait blocks until the container stops running, then returns its exit code. +func (container *Container) Wait() int { for container.State.Running { container.State.wait() } + return container.State.ExitCode } func (container *Container) WaitTimeout(timeout time.Duration) error { diff --git a/server/server.go b/server/server.go index 0e57c3da42..ee2c997634 100644 --- a/server/server.go +++ b/server/server.go @@ -53,6 +53,7 @@ func (srv *Server) Help() string { {"diff", "Inspect changes on a container's filesystem"}, {"commit", "Save the state of a container"}, {"attach", "Attach to the standard inputs and outputs of a running container"}, + {"wait", "Block until a container exits, then print its exit code"}, {"info", "Display system-wide information"}, {"tar", "Stream the contents of a container as a tar archive"}, {"web", "Generate a web UI"}, @@ -63,6 +64,27 @@ func (srv *Server) Help() string { return help } +// 'docker wait': block until a container stops +func (srv *Server) CmdWait(stdin io.ReadCloser, stdout io.Writer, args ...string) error { + cmd := rcli.Subcmd(stdout, "wait", "[OPTIONS] NAME", "Block until a container stops, then print its exit code.") + if err := cmd.Parse(args); err != nil { + cmd.Usage() + return nil + } + if cmd.NArg() < 1 { + cmd.Usage() + return nil + } + for _, name := range cmd.Args() { + if container := srv.containers.Get(name); container != nil { + fmt.Fprintln(stdout, container.Wait()) + } else { + return errors.New("No such container: " + name) + } + } + return nil +} + // 'docker info': display system-wide information. func (srv *Server) CmdInfo(stdin io.ReadCloser, stdout io.Writer, args ...string) error { fmt.Fprintf(stdout, "containers: %d\nversion: %s\nimages: %d\n",