config_unix.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // +build !windows
  2. package runconfig
  3. import (
  4. "github.com/docker/docker/api/types/container"
  5. networktypes "github.com/docker/docker/api/types/network"
  6. )
  7. // ContainerConfigWrapper is a Config wrapper that holds the container Config (portable)
  8. // and the corresponding HostConfig (non-portable).
  9. type ContainerConfigWrapper struct {
  10. *container.Config
  11. InnerHostConfig *container.HostConfig `json:"HostConfig,omitempty"`
  12. Cpuset string `json:",omitempty"` // Deprecated. Exported for backwards compatibility.
  13. NetworkingConfig *networktypes.NetworkingConfig `json:"NetworkingConfig,omitempty"`
  14. *container.HostConfig // Deprecated. Exported to read attributes from json that are not in the inner host config structure.
  15. }
  16. // getHostConfig gets the HostConfig of the Config.
  17. // It's mostly there to handle Deprecated fields of the ContainerConfigWrapper
  18. func (w *ContainerConfigWrapper) getHostConfig() *container.HostConfig {
  19. hc := w.HostConfig
  20. if hc == nil && w.InnerHostConfig != nil {
  21. hc = w.InnerHostConfig
  22. } else if w.InnerHostConfig != nil {
  23. if hc.Memory != 0 && w.InnerHostConfig.Memory == 0 {
  24. w.InnerHostConfig.Memory = hc.Memory
  25. }
  26. if hc.MemorySwap != 0 && w.InnerHostConfig.MemorySwap == 0 {
  27. w.InnerHostConfig.MemorySwap = hc.MemorySwap
  28. }
  29. if hc.CPUShares != 0 && w.InnerHostConfig.CPUShares == 0 {
  30. w.InnerHostConfig.CPUShares = hc.CPUShares
  31. }
  32. if hc.CpusetCpus != "" && w.InnerHostConfig.CpusetCpus == "" {
  33. w.InnerHostConfig.CpusetCpus = hc.CpusetCpus
  34. }
  35. if hc.VolumeDriver != "" && w.InnerHostConfig.VolumeDriver == "" {
  36. w.InnerHostConfig.VolumeDriver = hc.VolumeDriver
  37. }
  38. hc = w.InnerHostConfig
  39. }
  40. if hc != nil {
  41. if w.Cpuset != "" && hc.CpusetCpus == "" {
  42. hc.CpusetCpus = w.Cpuset
  43. }
  44. }
  45. // Make sure NetworkMode has an acceptable value. We do this to ensure
  46. // backwards compatible API behavior.
  47. SetDefaultNetModeIfBlank(hc)
  48. return hc
  49. }