github.com/fcwu/docker@v1.4.2-0.20150115145920-2a69ca89f0df/daemon/start.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/docker/docker/engine" 9 "github.com/docker/docker/runconfig" 10 ) 11 12 func (daemon *Daemon) ContainerStart(job *engine.Job) engine.Status { 13 if len(job.Args) < 1 { 14 return job.Errorf("Usage: %s container_id", job.Name) 15 } 16 var ( 17 name = job.Args[0] 18 container = daemon.Get(name) 19 ) 20 21 if container == nil { 22 return job.Errorf("No such container: %s", name) 23 } 24 25 if container.IsRunning() { 26 return job.Errorf("Container already started") 27 } 28 29 // If no environment was set, then no hostconfig was passed. 30 // This is kept for backward compatibility - hostconfig should be passed when 31 // creating a container, not during start. 32 if len(job.Environ()) > 0 { 33 hostConfig := runconfig.ContainerHostConfigFromJob(job) 34 if err := daemon.setHostConfig(container, hostConfig); err != nil { 35 return job.Error(err) 36 } 37 } 38 if err := container.Start(); err != nil { 39 container.LogEvent("die") 40 return job.Errorf("Cannot start container %s: %s", name, err) 41 } 42 43 return engine.StatusOK 44 } 45 46 func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.HostConfig) error { 47 container.Lock() 48 defer container.Unlock() 49 if err := parseSecurityOpt(container, hostConfig); err != nil { 50 return err 51 } 52 // Validate the HostConfig binds. Make sure that: 53 // the source exists 54 for _, bind := range hostConfig.Binds { 55 splitBind := strings.Split(bind, ":") 56 source := splitBind[0] 57 58 // ensure the source exists on the host 59 _, err := os.Stat(source) 60 if err != nil && os.IsNotExist(err) { 61 err = os.MkdirAll(source, 0755) 62 if err != nil { 63 return fmt.Errorf("Could not create local directory '%s' for bind mount: %s!", source, err.Error()) 64 } 65 } 66 } 67 // Register any links from the host config before starting the container 68 if err := daemon.RegisterLinks(container, hostConfig); err != nil { 69 return err 70 } 71 container.hostConfig = hostConfig 72 container.toDisk() 73 74 return nil 75 }