github.com/rentongzhang/docker@v1.8.2-rc1/api/client/restart.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"strconv"
     7  
     8  	Cli "github.com/docker/docker/cli"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  )
    11  
    12  // CmdRestart restarts one or more running containers.
    13  //
    14  // Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
    15  func (cli *DockerCli) CmdRestart(args ...string) error {
    16  	cmd := Cli.Subcmd("restart", []string{"CONTAINER [CONTAINER...]"}, "Restart a running container", true)
    17  	nSeconds := cmd.Int([]string{"t", "-time"}, 10, "Seconds to wait for stop before killing the container")
    18  	cmd.Require(flag.Min, 1)
    19  
    20  	cmd.ParseFlags(args, true)
    21  
    22  	v := url.Values{}
    23  	v.Set("t", strconv.Itoa(*nSeconds))
    24  
    25  	var errNames []string
    26  	for _, name := range cmd.Args() {
    27  		_, _, err := readBody(cli.call("POST", "/containers/"+name+"/restart?"+v.Encode(), nil, nil))
    28  		if err != nil {
    29  			fmt.Fprintf(cli.err, "%s\n", err)
    30  			errNames = append(errNames, name)
    31  		} else {
    32  			fmt.Fprintf(cli.out, "%s\n", name)
    33  		}
    34  	}
    35  	if len(errNames) > 0 {
    36  		return fmt.Errorf("Error: failed to restart containers: %v", errNames)
    37  	}
    38  	return nil
    39  }