hostconfig.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. package container // import "github.com/docker/docker/api/types/container"
  2. import (
  3. "strings"
  4. "github.com/docker/docker/api/types/blkiodev"
  5. "github.com/docker/docker/api/types/mount"
  6. "github.com/docker/docker/api/types/strslice"
  7. "github.com/docker/go-connections/nat"
  8. units "github.com/docker/go-units"
  9. )
  10. // CgroupnsMode represents the cgroup namespace mode of the container
  11. type CgroupnsMode string
  12. // cgroup namespace modes for containers
  13. const (
  14. CgroupnsModeEmpty CgroupnsMode = ""
  15. CgroupnsModePrivate CgroupnsMode = "private"
  16. CgroupnsModeHost CgroupnsMode = "host"
  17. )
  18. // IsPrivate indicates whether the container uses its own private cgroup namespace
  19. func (c CgroupnsMode) IsPrivate() bool {
  20. return c == CgroupnsModePrivate
  21. }
  22. // IsHost indicates whether the container shares the host's cgroup namespace
  23. func (c CgroupnsMode) IsHost() bool {
  24. return c == CgroupnsModeHost
  25. }
  26. // IsEmpty indicates whether the container cgroup namespace mode is unset
  27. func (c CgroupnsMode) IsEmpty() bool {
  28. return c == CgroupnsModeEmpty
  29. }
  30. // Valid indicates whether the cgroup namespace mode is valid
  31. func (c CgroupnsMode) Valid() bool {
  32. return c.IsEmpty() || c.IsPrivate() || c.IsHost()
  33. }
  34. // Isolation represents the isolation technology of a container. The supported
  35. // values are platform specific
  36. type Isolation string
  37. // Isolation modes for containers
  38. const (
  39. IsolationEmpty Isolation = "" // IsolationEmpty is unspecified (same behavior as default)
  40. IsolationDefault Isolation = "default" // IsolationDefault is the default isolation mode on current daemon
  41. IsolationProcess Isolation = "process" // IsolationProcess is process isolation mode
  42. IsolationHyperV Isolation = "hyperv" // IsolationHyperV is HyperV isolation mode
  43. )
  44. // IsDefault indicates the default isolation technology of a container. On Linux this
  45. // is the native driver. On Windows, this is a Windows Server Container.
  46. func (i Isolation) IsDefault() bool {
  47. // TODO consider making isolation-mode strict (case-sensitive)
  48. v := Isolation(strings.ToLower(string(i)))
  49. return v == IsolationDefault || v == IsolationEmpty
  50. }
  51. // IsHyperV indicates the use of a Hyper-V partition for isolation
  52. func (i Isolation) IsHyperV() bool {
  53. // TODO consider making isolation-mode strict (case-sensitive)
  54. return Isolation(strings.ToLower(string(i))) == IsolationHyperV
  55. }
  56. // IsProcess indicates the use of process isolation
  57. func (i Isolation) IsProcess() bool {
  58. // TODO consider making isolation-mode strict (case-sensitive)
  59. return Isolation(strings.ToLower(string(i))) == IsolationProcess
  60. }
  61. // IpcMode represents the container ipc stack.
  62. type IpcMode string
  63. // IpcMode constants
  64. const (
  65. IPCModeNone IpcMode = "none"
  66. IPCModeHost IpcMode = "host"
  67. IPCModeContainer IpcMode = "container"
  68. IPCModePrivate IpcMode = "private"
  69. IPCModeShareable IpcMode = "shareable"
  70. )
  71. // IsPrivate indicates whether the container uses its own private ipc namespace which can not be shared.
  72. func (n IpcMode) IsPrivate() bool {
  73. return n == IPCModePrivate
  74. }
  75. // IsHost indicates whether the container shares the host's ipc namespace.
  76. func (n IpcMode) IsHost() bool {
  77. return n == IPCModeHost
  78. }
  79. // IsShareable indicates whether the container's ipc namespace can be shared with another container.
  80. func (n IpcMode) IsShareable() bool {
  81. return n == IPCModeShareable
  82. }
  83. // IsContainer indicates whether the container uses another container's ipc namespace.
  84. func (n IpcMode) IsContainer() bool {
  85. _, ok := containerID(string(n))
  86. return ok
  87. }
  88. // IsNone indicates whether container IpcMode is set to "none".
  89. func (n IpcMode) IsNone() bool {
  90. return n == IPCModeNone
  91. }
  92. // IsEmpty indicates whether container IpcMode is empty
  93. func (n IpcMode) IsEmpty() bool {
  94. return n == ""
  95. }
  96. // Valid indicates whether the ipc mode is valid.
  97. func (n IpcMode) Valid() bool {
  98. // TODO(thaJeztah): align with PidMode, and consider container-mode without a container name/ID to be invalid.
  99. return n.IsEmpty() || n.IsNone() || n.IsPrivate() || n.IsHost() || n.IsShareable() || n.IsContainer()
  100. }
  101. // Container returns the name of the container ipc stack is going to be used.
  102. func (n IpcMode) Container() (idOrName string) {
  103. idOrName, _ = containerID(string(n))
  104. return idOrName
  105. }
  106. // NetworkMode represents the container network stack.
  107. type NetworkMode string
  108. // IsNone indicates whether container isn't using a network stack.
  109. func (n NetworkMode) IsNone() bool {
  110. return n == "none"
  111. }
  112. // IsDefault indicates whether container uses the default network stack.
  113. func (n NetworkMode) IsDefault() bool {
  114. return n == "default"
  115. }
  116. // IsPrivate indicates whether container uses its private network stack.
  117. func (n NetworkMode) IsPrivate() bool {
  118. return !(n.IsHost() || n.IsContainer())
  119. }
  120. // IsContainer indicates whether container uses a container network stack.
  121. func (n NetworkMode) IsContainer() bool {
  122. _, ok := containerID(string(n))
  123. return ok
  124. }
  125. // ConnectedContainer is the id of the container which network this container is connected to.
  126. func (n NetworkMode) ConnectedContainer() (idOrName string) {
  127. idOrName, _ = containerID(string(n))
  128. return idOrName
  129. }
  130. // UserDefined indicates user-created network
  131. func (n NetworkMode) UserDefined() string {
  132. if n.IsUserDefined() {
  133. return string(n)
  134. }
  135. return ""
  136. }
  137. // UsernsMode represents userns mode in the container.
  138. type UsernsMode string
  139. // IsHost indicates whether the container uses the host's userns.
  140. func (n UsernsMode) IsHost() bool {
  141. return n == "host"
  142. }
  143. // IsPrivate indicates whether the container uses the a private userns.
  144. func (n UsernsMode) IsPrivate() bool {
  145. return !n.IsHost()
  146. }
  147. // Valid indicates whether the userns is valid.
  148. func (n UsernsMode) Valid() bool {
  149. return n == "" || n.IsHost()
  150. }
  151. // CgroupSpec represents the cgroup to use for the container.
  152. type CgroupSpec string
  153. // IsContainer indicates whether the container is using another container cgroup
  154. func (c CgroupSpec) IsContainer() bool {
  155. _, ok := containerID(string(c))
  156. return ok
  157. }
  158. // Valid indicates whether the cgroup spec is valid.
  159. func (c CgroupSpec) Valid() bool {
  160. // TODO(thaJeztah): align with PidMode, and consider container-mode without a container name/ID to be invalid.
  161. return c == "" || c.IsContainer()
  162. }
  163. // Container returns the ID or name of the container whose cgroup will be used.
  164. func (c CgroupSpec) Container() (idOrName string) {
  165. idOrName, _ = containerID(string(c))
  166. return idOrName
  167. }
  168. // UTSMode represents the UTS namespace of the container.
  169. type UTSMode string
  170. // IsPrivate indicates whether the container uses its private UTS namespace.
  171. func (n UTSMode) IsPrivate() bool {
  172. return !n.IsHost()
  173. }
  174. // IsHost indicates whether the container uses the host's UTS namespace.
  175. func (n UTSMode) IsHost() bool {
  176. return n == "host"
  177. }
  178. // Valid indicates whether the UTS namespace is valid.
  179. func (n UTSMode) Valid() bool {
  180. return n == "" || n.IsHost()
  181. }
  182. // PidMode represents the pid namespace of the container.
  183. type PidMode string
  184. // IsPrivate indicates whether the container uses its own new pid namespace.
  185. func (n PidMode) IsPrivate() bool {
  186. return !(n.IsHost() || n.IsContainer())
  187. }
  188. // IsHost indicates whether the container uses the host's pid namespace.
  189. func (n PidMode) IsHost() bool {
  190. return n == "host"
  191. }
  192. // IsContainer indicates whether the container uses a container's pid namespace.
  193. func (n PidMode) IsContainer() bool {
  194. _, ok := containerID(string(n))
  195. return ok
  196. }
  197. // Valid indicates whether the pid namespace is valid.
  198. func (n PidMode) Valid() bool {
  199. return n == "" || n.IsHost() || validContainer(string(n))
  200. }
  201. // Container returns the name of the container whose pid namespace is going to be used.
  202. func (n PidMode) Container() (idOrName string) {
  203. idOrName, _ = containerID(string(n))
  204. return idOrName
  205. }
  206. // DeviceRequest represents a request for devices from a device driver.
  207. // Used by GPU device drivers.
  208. type DeviceRequest struct {
  209. Driver string // Name of device driver
  210. Count int // Number of devices to request (-1 = All)
  211. DeviceIDs []string // List of device IDs as recognizable by the device driver
  212. Capabilities [][]string // An OR list of AND lists of device capabilities (e.g. "gpu")
  213. Options map[string]string // Options to pass onto the device driver
  214. }
  215. // DeviceMapping represents the device mapping between the host and the container.
  216. type DeviceMapping struct {
  217. PathOnHost string
  218. PathInContainer string
  219. CgroupPermissions string
  220. }
  221. // RestartPolicy represents the restart policies of the container.
  222. type RestartPolicy struct {
  223. Name string
  224. MaximumRetryCount int
  225. }
  226. // IsNone indicates whether the container has the "no" restart policy.
  227. // This means the container will not automatically restart when exiting.
  228. func (rp *RestartPolicy) IsNone() bool {
  229. return rp.Name == "no" || rp.Name == ""
  230. }
  231. // IsAlways indicates whether the container has the "always" restart policy.
  232. // This means the container will automatically restart regardless of the exit status.
  233. func (rp *RestartPolicy) IsAlways() bool {
  234. return rp.Name == "always"
  235. }
  236. // IsOnFailure indicates whether the container has the "on-failure" restart policy.
  237. // This means the container will automatically restart of exiting with a non-zero exit status.
  238. func (rp *RestartPolicy) IsOnFailure() bool {
  239. return rp.Name == "on-failure"
  240. }
  241. // IsUnlessStopped indicates whether the container has the
  242. // "unless-stopped" restart policy. This means the container will
  243. // automatically restart unless user has put it to stopped state.
  244. func (rp *RestartPolicy) IsUnlessStopped() bool {
  245. return rp.Name == "unless-stopped"
  246. }
  247. // IsSame compares two RestartPolicy to see if they are the same
  248. func (rp *RestartPolicy) IsSame(tp *RestartPolicy) bool {
  249. return rp.Name == tp.Name && rp.MaximumRetryCount == tp.MaximumRetryCount
  250. }
  251. // LogMode is a type to define the available modes for logging
  252. // These modes affect how logs are handled when log messages start piling up.
  253. type LogMode string
  254. // Available logging modes
  255. const (
  256. LogModeUnset LogMode = ""
  257. LogModeBlocking LogMode = "blocking"
  258. LogModeNonBlock LogMode = "non-blocking"
  259. )
  260. // LogConfig represents the logging configuration of the container.
  261. type LogConfig struct {
  262. Type string
  263. Config map[string]string
  264. }
  265. // Resources contains container's resources (cgroups config, ulimits...)
  266. type Resources struct {
  267. // Applicable to all platforms
  268. CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers)
  269. Memory int64 // Memory limit (in bytes)
  270. NanoCPUs int64 `json:"NanoCpus"` // CPU quota in units of 10<sup>-9</sup> CPUs.
  271. // Applicable to UNIX platforms
  272. CgroupParent string // Parent cgroup.
  273. BlkioWeight uint16 // Block IO weight (relative weight vs. other containers)
  274. BlkioWeightDevice []*blkiodev.WeightDevice
  275. BlkioDeviceReadBps []*blkiodev.ThrottleDevice
  276. BlkioDeviceWriteBps []*blkiodev.ThrottleDevice
  277. BlkioDeviceReadIOps []*blkiodev.ThrottleDevice
  278. BlkioDeviceWriteIOps []*blkiodev.ThrottleDevice
  279. CPUPeriod int64 `json:"CpuPeriod"` // CPU CFS (Completely Fair Scheduler) period
  280. CPUQuota int64 `json:"CpuQuota"` // CPU CFS (Completely Fair Scheduler) quota
  281. CPURealtimePeriod int64 `json:"CpuRealtimePeriod"` // CPU real-time period
  282. CPURealtimeRuntime int64 `json:"CpuRealtimeRuntime"` // CPU real-time runtime
  283. CpusetCpus string // CpusetCpus 0-2, 0,1
  284. CpusetMems string // CpusetMems 0-2, 0,1
  285. Devices []DeviceMapping // List of devices to map inside the container
  286. DeviceCgroupRules []string // List of rule to be added to the device cgroup
  287. DeviceRequests []DeviceRequest // List of device requests for device drivers
  288. // KernelMemory specifies the kernel memory limit (in bytes) for the container.
  289. // Deprecated: kernel 5.4 deprecated kmem.limit_in_bytes.
  290. KernelMemory int64 `json:",omitempty"`
  291. KernelMemoryTCP int64 `json:",omitempty"` // Hard limit for kernel TCP buffer memory (in bytes)
  292. MemoryReservation int64 // Memory soft limit (in bytes)
  293. MemorySwap int64 // Total memory usage (memory + swap); set `-1` to enable unlimited swap
  294. MemorySwappiness *int64 // Tuning container memory swappiness behaviour
  295. OomKillDisable *bool // Whether to disable OOM Killer or not
  296. PidsLimit *int64 // Setting PIDs limit for a container; Set `0` or `-1` for unlimited, or `null` to not change.
  297. Ulimits []*units.Ulimit // List of ulimits to be set in the container
  298. // Applicable to Windows
  299. CPUCount int64 `json:"CpuCount"` // CPU count
  300. CPUPercent int64 `json:"CpuPercent"` // CPU percent
  301. IOMaximumIOps uint64 // Maximum IOps for the container system drive
  302. IOMaximumBandwidth uint64 // Maximum IO in bytes per second for the container system drive
  303. }
  304. // UpdateConfig holds the mutable attributes of a Container.
  305. // Those attributes can be updated at runtime.
  306. type UpdateConfig struct {
  307. // Contains container's resources (cgroups, ulimits)
  308. Resources
  309. RestartPolicy RestartPolicy
  310. }
  311. // HostConfig the non-portable Config structure of a container.
  312. // Here, "non-portable" means "dependent of the host we are running on".
  313. // Portable information *should* appear in Config.
  314. type HostConfig struct {
  315. // Applicable to all platforms
  316. Binds []string // List of volume bindings for this container
  317. ContainerIDFile string // File (path) where the containerId is written
  318. LogConfig LogConfig // Configuration of the logs for this container
  319. NetworkMode NetworkMode // Network mode to use for the container
  320. PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
  321. RestartPolicy RestartPolicy // Restart policy to be used for the container
  322. AutoRemove bool // Automatically remove container when it exits
  323. VolumeDriver string // Name of the volume driver used to mount volumes
  324. VolumesFrom []string // List of volumes to take from other container
  325. ConsoleSize [2]uint // Initial console size (height,width)
  326. Annotations map[string]string `json:",omitempty"` // Arbitrary non-identifying metadata attached to container and provided to the runtime
  327. // Applicable to UNIX platforms
  328. CapAdd strslice.StrSlice // List of kernel capabilities to add to the container
  329. CapDrop strslice.StrSlice // List of kernel capabilities to remove from the container
  330. CgroupnsMode CgroupnsMode // Cgroup namespace mode to use for the container
  331. DNS []string `json:"Dns"` // List of DNS server to lookup
  332. DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for
  333. DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for
  334. ExtraHosts []string // List of extra hosts
  335. GroupAdd []string // List of additional groups that the container process will run as
  336. IpcMode IpcMode // IPC namespace to use for the container
  337. Cgroup CgroupSpec // Cgroup to use for the container
  338. Links []string // List of links (in the name:alias form)
  339. OomScoreAdj int // Container preference for OOM-killing
  340. PidMode PidMode // PID namespace to use for the container
  341. Privileged bool // Is the container in privileged mode
  342. PublishAllPorts bool // Should docker publish all exposed port for the container
  343. ReadonlyRootfs bool // Is the container root filesystem in read-only
  344. SecurityOpt []string // List of string values to customize labels for MLS systems, such as SELinux.
  345. StorageOpt map[string]string `json:",omitempty"` // Storage driver options per container.
  346. Tmpfs map[string]string `json:",omitempty"` // List of tmpfs (mounts) used for the container
  347. UTSMode UTSMode // UTS namespace to use for the container
  348. UsernsMode UsernsMode // The user namespace to use for the container
  349. ShmSize int64 // Total shm memory usage
  350. Sysctls map[string]string `json:",omitempty"` // List of Namespaced sysctls used for the container
  351. Runtime string `json:",omitempty"` // Runtime to use with this container
  352. // Applicable to Windows
  353. Isolation Isolation // Isolation technology of the container (e.g. default, hyperv)
  354. // Contains container's resources (cgroups, ulimits)
  355. Resources
  356. // Mounts specs used by the container
  357. Mounts []mount.Mount `json:",omitempty"`
  358. // MaskedPaths is the list of paths to be masked inside the container (this overrides the default set of paths)
  359. MaskedPaths []string
  360. // ReadonlyPaths is the list of paths to be set as read-only inside the container (this overrides the default set of paths)
  361. ReadonlyPaths []string
  362. // Run a custom init inside the container, if null, use the daemon's configured settings
  363. Init *bool `json:",omitempty"`
  364. }
  365. // containerID splits "container:<ID|name>" values. It returns the container
  366. // ID or name, and whether an ID/name was found. It returns an empty string and
  367. // a "false" if the value does not have a "container:" prefix. Further validation
  368. // of the returned, including checking if the value is empty, should be handled
  369. // by the caller.
  370. func containerID(val string) (idOrName string, ok bool) {
  371. k, v, hasSep := strings.Cut(val, ":")
  372. if !hasSep || k != "container" {
  373. return "", false
  374. }
  375. return v, true
  376. }
  377. // validContainer checks if the given value is a "container:" mode with
  378. // a non-empty name/ID.
  379. func validContainer(val string) bool {
  380. id, ok := containerID(val)
  381. return ok && id != ""
  382. }