github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/daemon/stop.go (about)

     1  package daemon
     2  
     3  import (
     4  	"github.com/docker/docker/engine"
     5  )
     6  
     7  func (daemon *Daemon) ContainerStop(job *engine.Job) engine.Status {
     8  	if len(job.Args) != 1 {
     9  		return job.Errorf("Usage: %s CONTAINER\n", job.Name)
    10  	}
    11  	var (
    12  		name = job.Args[0]
    13  		t    = 10
    14  	)
    15  	if job.EnvExists("t") {
    16  		t = job.GetenvInt("t")
    17  	}
    18  	if container := daemon.Get(name); container != nil {
    19  		if !container.IsRunning() {
    20  			return job.Errorf("Container already stopped")
    21  		}
    22  		if err := container.Stop(int(t)); err != nil {
    23  			return job.Errorf("Cannot stop container %s: %s\n", name, err)
    24  		}
    25  		container.LogEvent("stop")
    26  	} else {
    27  		return job.Errorf("No such container: %s\n", name)
    28  	}
    29  	return engine.StatusOK
    30  }