start.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "github.com/docker/docker/engine"
  7. "github.com/docker/docker/runconfig"
  8. )
  9. func (daemon *Daemon) ContainerStart(job *engine.Job) engine.Status {
  10. if len(job.Args) < 1 {
  11. return job.Errorf("Usage: %s container_id", job.Name)
  12. }
  13. var (
  14. name = job.Args[0]
  15. container = daemon.Get(name)
  16. )
  17. if container == nil {
  18. return job.Errorf("No such container: %s", name)
  19. }
  20. if container.IsRunning() {
  21. return job.Errorf("Container already started")
  22. }
  23. // If no environment was set, then no hostconfig was passed.
  24. if len(job.Environ()) > 0 {
  25. hostConfig := runconfig.ContainerHostConfigFromJob(job)
  26. if err := daemon.setHostConfig(container, hostConfig); err != nil {
  27. return job.Error(err)
  28. }
  29. }
  30. if err := container.Start(); err != nil {
  31. container.LogEvent("die")
  32. return job.Errorf("Cannot start container %s: %s", name, err)
  33. }
  34. return engine.StatusOK
  35. }
  36. func (daemon *Daemon) setHostConfig(container *Container, hostConfig *runconfig.HostConfig) error {
  37. // Validate the HostConfig binds. Make sure that:
  38. // the source exists
  39. for _, bind := range hostConfig.Binds {
  40. splitBind := strings.Split(bind, ":")
  41. source := splitBind[0]
  42. // ensure the source exists on the host
  43. _, err := os.Stat(source)
  44. if err != nil && os.IsNotExist(err) {
  45. err = os.MkdirAll(source, 0755)
  46. if err != nil {
  47. return fmt.Errorf("Could not create local directory '%s' for bind mount: %s!", source, err.Error())
  48. }
  49. }
  50. }
  51. // Register any links from the host config before starting the container
  52. if err := daemon.RegisterLinks(container, hostConfig); err != nil {
  53. return err
  54. }
  55. container.SetHostConfig(hostConfig)
  56. container.ToDisk()
  57. return nil
  58. }