start.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package daemon
  2. import (
  3. "runtime"
  4. "github.com/docker/docker/context"
  5. derr "github.com/docker/docker/errors"
  6. "github.com/docker/docker/runconfig"
  7. "github.com/docker/docker/utils"
  8. )
  9. // ContainerStart starts a container.
  10. func (daemon *Daemon) ContainerStart(ctx context.Context, name string, hostConfig *runconfig.HostConfig) error {
  11. container, err := daemon.Get(ctx, name)
  12. if err != nil {
  13. return err
  14. }
  15. if container.isPaused() {
  16. return derr.ErrorCodeStartPaused
  17. }
  18. if container.IsRunning() {
  19. return derr.ErrorCodeAlreadyStarted
  20. }
  21. // Windows does not have the backwards compatibility issue here.
  22. if runtime.GOOS != "windows" {
  23. // This is kept for backward compatibility - hostconfig should be passed when
  24. // creating a container, not during start.
  25. if hostConfig != nil {
  26. if err := daemon.setHostConfig(ctx, container, hostConfig); err != nil {
  27. return err
  28. }
  29. }
  30. } else {
  31. if hostConfig != nil {
  32. return derr.ErrorCodeHostConfigStart
  33. }
  34. }
  35. // check if hostConfig is in line with the current system settings.
  36. // It may happen cgroups are umounted or the like.
  37. if _, err = daemon.verifyContainerSettings(ctx, container.hostConfig, nil); err != nil {
  38. return err
  39. }
  40. if err := container.Start(ctx); err != nil {
  41. return derr.ErrorCodeCantStart.WithArgs(name, utils.GetErrorMessage(err))
  42. }
  43. return nil
  44. }