@@ -331,14 +331,17 @@ func ResourceLimitsFunc(h *container.HostConfig, resources *ContainerResources)
331331 if resources == nil {
332332 return
333333 }
334- h .Memory = int64 (resources .MemoryMb ) * 1024 * 1024 // Memory in Mb
335- h .MemoryReservation = int64 (resources .MemoryMb ) * 1024 * 1024 // Total memory that can be reserved (soft) in Mb
336- // https://docs.docker.com/engine/containers/resource_constraints/ if memories are equal we don't have swap, read the docs
337- h .MemorySwap = h .Memory // No swap for simplicity
338-
339- // Set CPU limits using CPUQuota and CPUPeriod
340- // we don't use runtime.NumCPU or docker API to get CPUs because h.CPUShares is relative to amount of containers you run
341- // CPUPeriod and CPUQuota are absolute and easier to control
342- h .CPUPeriod = 100000 // Default period (100ms)
343- h .CPUQuota = int64 (resources .CPUs * 100000 ) // Quota in microseconds (e.g., 0.5 CPUs = 50000)
334+ if resources .MemoryMb != 0 {
335+ h .Memory = int64 (resources .MemoryMb ) * 1024 * 1024 // Memory in Mb
336+ h .MemoryReservation = int64 (resources .MemoryMb ) * 1024 * 1024 // Total memory that can be reserved (soft) in Mb
337+ // https://docs.docker.com/engine/containers/resource_constraints/ if both values are equal swap is off, read the docs
338+ h .MemorySwap = h .Memory
339+ }
340+ if resources .CPUs != 0 {
341+ // Set CPU limits using CPUQuota and CPUPeriod
342+ // we don't use runtime.NumCPU or docker API to get CPUs because h.CPUShares is relative to amount of containers you run
343+ // CPUPeriod and CPUQuota are absolute and easier to control
344+ h .CPUPeriod = 100000 // Default period (100ms)
345+ h .CPUQuota = int64 (resources .CPUs * 100000 ) // Quota in microseconds (e.g., 0.5 CPUs = 50000)
346+ }
344347}
0 commit comments