container.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. package daemon
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. "github.com/docker/docker/api/errors"
  8. containertypes "github.com/docker/docker/api/types/container"
  9. "github.com/docker/docker/api/types/strslice"
  10. "github.com/docker/docker/container"
  11. "github.com/docker/docker/daemon/network"
  12. "github.com/docker/docker/image"
  13. "github.com/docker/docker/opts"
  14. "github.com/docker/docker/pkg/signal"
  15. "github.com/docker/docker/pkg/system"
  16. "github.com/docker/docker/pkg/truncindex"
  17. "github.com/docker/go-connections/nat"
  18. )
  19. // GetContainer looks for a container using the provided information, which could be
  20. // one of the following inputs from the caller:
  21. // - A full container ID, which will exact match a container in daemon's list
  22. // - A container name, which will only exact match via the GetByName() function
  23. // - A partial container ID prefix (e.g. short ID) of any length that is
  24. // unique enough to only return a single container object
  25. // If none of these searches succeed, an error is returned
  26. func (daemon *Daemon) GetContainer(prefixOrName string) (*container.Container, error) {
  27. if len(prefixOrName) == 0 {
  28. return nil, errors.NewBadRequestError(fmt.Errorf("No container name or ID supplied"))
  29. }
  30. if containerByID := daemon.containers.Get(prefixOrName); containerByID != nil {
  31. // prefix is an exact match to a full container ID
  32. return containerByID, nil
  33. }
  34. // GetByName will match only an exact name provided; we ignore errors
  35. if containerByName, _ := daemon.GetByName(prefixOrName); containerByName != nil {
  36. // prefix is an exact match to a full container Name
  37. return containerByName, nil
  38. }
  39. containerID, indexError := daemon.idIndex.Get(prefixOrName)
  40. if indexError != nil {
  41. // When truncindex defines an error type, use that instead
  42. if indexError == truncindex.ErrNotExist {
  43. err := fmt.Errorf("No such container: %s", prefixOrName)
  44. return nil, errors.NewRequestNotFoundError(err)
  45. }
  46. return nil, indexError
  47. }
  48. return daemon.containers.Get(containerID), nil
  49. }
  50. // Exists returns a true if a container of the specified ID or name exists,
  51. // false otherwise.
  52. func (daemon *Daemon) Exists(id string) bool {
  53. c, _ := daemon.GetContainer(id)
  54. return c != nil
  55. }
  56. // IsPaused returns a bool indicating if the specified container is paused.
  57. func (daemon *Daemon) IsPaused(id string) bool {
  58. c, _ := daemon.GetContainer(id)
  59. return c.State.IsPaused()
  60. }
  61. func (daemon *Daemon) containerRoot(id string) string {
  62. return filepath.Join(daemon.repository, id)
  63. }
  64. // Load reads the contents of a container from disk
  65. // This is typically done at startup.
  66. func (daemon *Daemon) load(id string) (*container.Container, error) {
  67. container := daemon.newBaseContainer(id)
  68. if err := container.FromDisk(); err != nil {
  69. return nil, err
  70. }
  71. if container.ID != id {
  72. return container, fmt.Errorf("Container %s is stored at %s", container.ID, id)
  73. }
  74. return container, nil
  75. }
  76. // Register makes a container object usable by the daemon as <container.ID>
  77. func (daemon *Daemon) Register(c *container.Container) {
  78. // Attach to stdout and stderr
  79. if c.Config.OpenStdin {
  80. c.StreamConfig.NewInputPipes()
  81. } else {
  82. c.StreamConfig.NewNopInputPipe()
  83. }
  84. daemon.containers.Add(c.ID, c)
  85. daemon.idIndex.Add(c.ID)
  86. }
  87. func (daemon *Daemon) newContainer(name string, config *containertypes.Config, hostConfig *containertypes.HostConfig, imgID image.ID, managed bool) (*container.Container, error) {
  88. var (
  89. id string
  90. err error
  91. noExplicitName = name == ""
  92. )
  93. id, name, err = daemon.generateIDAndName(name)
  94. if err != nil {
  95. return nil, err
  96. }
  97. if hostConfig.NetworkMode.IsHost() {
  98. if config.Hostname == "" {
  99. config.Hostname, err = os.Hostname()
  100. if err != nil {
  101. return nil, err
  102. }
  103. }
  104. } else {
  105. daemon.generateHostname(id, config)
  106. }
  107. entrypoint, args := daemon.getEntrypointAndArgs(config.Entrypoint, config.Cmd)
  108. base := daemon.newBaseContainer(id)
  109. base.Created = time.Now().UTC()
  110. base.Managed = managed
  111. base.Path = entrypoint
  112. base.Args = args //FIXME: de-duplicate from config
  113. base.Config = config
  114. base.HostConfig = &containertypes.HostConfig{}
  115. base.ImageID = imgID
  116. base.NetworkSettings = &network.Settings{IsAnonymousEndpoint: noExplicitName}
  117. base.Name = name
  118. base.Driver = daemon.GraphDriverName()
  119. return base, err
  120. }
  121. // GetByName returns a container given a name.
  122. func (daemon *Daemon) GetByName(name string) (*container.Container, error) {
  123. if len(name) == 0 {
  124. return nil, fmt.Errorf("No container name supplied")
  125. }
  126. fullName := name
  127. if name[0] != '/' {
  128. fullName = "/" + name
  129. }
  130. id, err := daemon.nameIndex.Get(fullName)
  131. if err != nil {
  132. return nil, fmt.Errorf("Could not find entity for %s", name)
  133. }
  134. e := daemon.containers.Get(id)
  135. if e == nil {
  136. return nil, fmt.Errorf("Could not find container for entity id %s", id)
  137. }
  138. return e, nil
  139. }
  140. // newBaseContainer creates a new container with its initial
  141. // configuration based on the root storage from the daemon.
  142. func (daemon *Daemon) newBaseContainer(id string) *container.Container {
  143. return container.NewBaseContainer(id, daemon.containerRoot(id))
  144. }
  145. func (daemon *Daemon) getEntrypointAndArgs(configEntrypoint strslice.StrSlice, configCmd strslice.StrSlice) (string, []string) {
  146. if len(configEntrypoint) != 0 {
  147. return configEntrypoint[0], append(configEntrypoint[1:], configCmd...)
  148. }
  149. return configCmd[0], configCmd[1:]
  150. }
  151. func (daemon *Daemon) generateHostname(id string, config *containertypes.Config) {
  152. // Generate default hostname
  153. if config.Hostname == "" {
  154. config.Hostname = id[:12]
  155. }
  156. }
  157. func (daemon *Daemon) setSecurityOptions(container *container.Container, hostConfig *containertypes.HostConfig) error {
  158. container.Lock()
  159. defer container.Unlock()
  160. return parseSecurityOpt(container, hostConfig)
  161. }
  162. func (daemon *Daemon) setHostConfig(container *container.Container, hostConfig *containertypes.HostConfig) error {
  163. // Do not lock while creating volumes since this could be calling out to external plugins
  164. // Don't want to block other actions, like `docker ps` because we're waiting on an external plugin
  165. if err := daemon.registerMountPoints(container, hostConfig); err != nil {
  166. return err
  167. }
  168. container.Lock()
  169. defer container.Unlock()
  170. // Register any links from the host config before starting the container
  171. if err := daemon.registerLinks(container, hostConfig); err != nil {
  172. return err
  173. }
  174. container.HostConfig = hostConfig
  175. return container.ToDisk()
  176. }
  177. // verifyContainerSettings performs validation of the hostconfig and config
  178. // structures.
  179. func (daemon *Daemon) verifyContainerSettings(hostConfig *containertypes.HostConfig, config *containertypes.Config, update bool) ([]string, error) {
  180. // First perform verification of settings common across all platforms.
  181. if config != nil {
  182. if config.WorkingDir != "" {
  183. config.WorkingDir = filepath.FromSlash(config.WorkingDir) // Ensure in platform semantics
  184. if !system.IsAbs(config.WorkingDir) {
  185. return nil, fmt.Errorf("the working directory '%s' is invalid, it needs to be an absolute path", config.WorkingDir)
  186. }
  187. }
  188. if len(config.StopSignal) > 0 {
  189. _, err := signal.ParseSignal(config.StopSignal)
  190. if err != nil {
  191. return nil, err
  192. }
  193. }
  194. // Validate if Env contains empty variable or not (e.g., ``, `=foo`)
  195. for _, env := range config.Env {
  196. if _, err := opts.ValidateEnv(env); err != nil {
  197. return nil, err
  198. }
  199. }
  200. // Validate the healthcheck params of Config
  201. if config.Healthcheck != nil {
  202. if config.Healthcheck.Interval != 0 && config.Healthcheck.Interval < time.Second {
  203. return nil, fmt.Errorf("Interval in Healthcheck cannot be less than one second")
  204. }
  205. if config.Healthcheck.Timeout != 0 && config.Healthcheck.Timeout < time.Second {
  206. return nil, fmt.Errorf("Timeout in Healthcheck cannot be less than one second")
  207. }
  208. if config.Healthcheck.Retries < 0 {
  209. return nil, fmt.Errorf("Retries in Healthcheck cannot be negative")
  210. }
  211. }
  212. }
  213. if hostConfig == nil {
  214. return nil, nil
  215. }
  216. if hostConfig.AutoRemove && !hostConfig.RestartPolicy.IsNone() {
  217. return nil, fmt.Errorf("can't create 'AutoRemove' container with restart policy")
  218. }
  219. for port := range hostConfig.PortBindings {
  220. _, portStr := nat.SplitProtoPort(string(port))
  221. if _, err := nat.ParsePort(portStr); err != nil {
  222. return nil, fmt.Errorf("invalid port specification: %q", portStr)
  223. }
  224. for _, pb := range hostConfig.PortBindings[port] {
  225. _, err := nat.NewPort(nat.SplitProtoPort(pb.HostPort))
  226. if err != nil {
  227. return nil, fmt.Errorf("invalid port specification: %q", pb.HostPort)
  228. }
  229. }
  230. }
  231. p := hostConfig.RestartPolicy
  232. switch p.Name {
  233. case "always", "unless-stopped", "no":
  234. if p.MaximumRetryCount != 0 {
  235. return nil, fmt.Errorf("maximum retry count cannot be used with restart policy '%s'", p.Name)
  236. }
  237. case "on-failure":
  238. if p.MaximumRetryCount < 0 {
  239. return nil, fmt.Errorf("maximum retry count cannot be negative")
  240. }
  241. case "":
  242. // do nothing
  243. default:
  244. return nil, fmt.Errorf("invalid restart policy '%s'", p.Name)
  245. }
  246. // Now do platform-specific verification
  247. return verifyPlatformContainerSettings(daemon, hostConfig, config, update)
  248. }