-m 10 # 10 bytes
-m 10b # 10 bytes
-m 10k # 10240 bytes (10 * 1024)
-m 10m # 10485760 bytes (10 * 1024 * 1024)
-m 10g # 10737418240 bytes (10 * 1024 * 1024 * 1024)
Units are case-insensitive, and 'kb', 'mb' and 'gb' are equivalent to 'k', 'm' and 'g'.
| ... | ... |
@@ -162,7 +162,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, |
| 162 | 162 |
cmd.Var(flAttach, "a", "Attach to stdin, stdout or stderr.") |
| 163 | 163 |
flStdin := cmd.Bool("i", false, "Keep stdin open even if not attached")
|
| 164 | 164 |
flTty := cmd.Bool("t", false, "Allocate a pseudo-tty")
|
| 165 |
- flMemory := cmd.Int64("m", 0, "Memory limit (in bytes)")
|
|
| 165 |
+ flMemoryString := cmd.String("m", "", "Memory limit (format: <number><optional unit>, where unit = b, k, m or g)")
|
|
| 166 | 166 |
flContainerIDFile := cmd.String("cidfile", "", "Write the container ID to the file")
|
| 167 | 167 |
flNetwork := cmd.Bool("n", true, "Enable networking for this container")
|
| 168 | 168 |
flPrivileged := cmd.Bool("privileged", false, "Give extended privileges to this container")
|
| ... | ... |
@@ -170,9 +170,9 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, |
| 170 | 170 |
cmd.Bool("sig-proxy", true, "Proxify all received signal to the process (even in non-tty mode)")
|
| 171 | 171 |
cmd.String("name", "", "Assign a name to the container")
|
| 172 | 172 |
|
| 173 |
- if capabilities != nil && *flMemory > 0 && !capabilities.MemoryLimit {
|
|
| 173 |
+ if capabilities != nil && *flMemoryString != "" && !capabilities.MemoryLimit {
|
|
| 174 | 174 |
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support memory limit capabilities. Limitation discarded.\n") |
| 175 |
- *flMemory = 0 |
|
| 175 |
+ *flMemoryString = "" |
|
| 176 | 176 |
} |
| 177 | 177 |
|
| 178 | 178 |
flCpuShares := cmd.Int64("c", 0, "CPU shares (relative weight)")
|
| ... | ... |
@@ -239,6 +239,18 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, |
| 239 | 239 |
} |
| 240 | 240 |
} |
| 241 | 241 |
|
| 242 |
+ var flMemory int64 |
|
| 243 |
+ |
|
| 244 |
+ if *flMemoryString != "" {
|
|
| 245 |
+ parsedMemory, err := utils.RAMInBytes(*flMemoryString) |
|
| 246 |
+ |
|
| 247 |
+ if err != nil {
|
|
| 248 |
+ return nil, nil, cmd, err |
|
| 249 |
+ } |
|
| 250 |
+ |
|
| 251 |
+ flMemory = parsedMemory |
|
| 252 |
+ } |
|
| 253 |
+ |
|
| 242 | 254 |
var binds []string |
| 243 | 255 |
|
| 244 | 256 |
// add any bind targets to the list of container volumes |
| ... | ... |
@@ -306,7 +318,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, |
| 306 | 306 |
Tty: *flTty, |
| 307 | 307 |
NetworkDisabled: !*flNetwork, |
| 308 | 308 |
OpenStdin: *flStdin, |
| 309 |
- Memory: *flMemory, |
|
| 309 |
+ Memory: flMemory, |
|
| 310 | 310 |
CpuShares: *flCpuShares, |
| 311 | 311 |
AttachStdin: flAttach.Get("stdin"),
|
| 312 | 312 |
AttachStdout: flAttach.Get("stdout"),
|
| ... | ... |
@@ -330,7 +342,7 @@ func ParseRun(args []string, capabilities *Capabilities) (*Config, *HostConfig, |
| 330 | 330 |
Links: flLinks, |
| 331 | 331 |
} |
| 332 | 332 |
|
| 333 |
- if capabilities != nil && *flMemory > 0 && !capabilities.SwapLimit {
|
|
| 333 |
+ if capabilities != nil && flMemory > 0 && !capabilities.SwapLimit {
|
|
| 334 | 334 |
//fmt.Fprintf(stdout, "WARNING: Your kernel does not support swap limit capabilities. Limitation discarded.\n") |
| 335 | 335 |
config.MemorySwap = -1 |
| 336 | 336 |
} |
| ... | ... |
@@ -568,7 +568,7 @@ network communication. |
| 568 | 568 |
-h="": Container host name |
| 569 | 569 |
-i=false: Keep stdin open even if not attached |
| 570 | 570 |
-privileged=false: Give extended privileges to this container |
| 571 |
- -m=0: Memory limit (in bytes) |
|
| 571 |
+ -m="": Memory limit (format: <number><optional unit>, where unit = b, k, m or g) |
|
| 572 | 572 |
-n=true: Enable networking for this container |
| 573 | 573 |
-p=[]: Map a network port to the container |
| 574 | 574 |
-rm=false: Automatically remove the container when it exits (incompatible with -d) |
| ... | ... |
@@ -20,6 +20,7 @@ import ( |
| 20 | 20 |
"strings" |
| 21 | 21 |
"sync" |
| 22 | 22 |
"time" |
| 23 |
+ "regexp" |
|
| 23 | 24 |
) |
| 24 | 25 |
|
| 25 | 26 |
var ( |
| ... | ... |
@@ -176,6 +177,37 @@ func HumanSize(size int64) string {
|
| 176 | 176 |
return fmt.Sprintf("%.4g %s", sizef, units[i])
|
| 177 | 177 |
} |
| 178 | 178 |
|
| 179 |
+// Parses a human-readable string representing an amount of RAM |
|
| 180 |
+// in bytes, kibibytes, mebibytes or gibibytes, and returns the |
|
| 181 |
+// number of bytes, or -1 if the string is unparseable. |
|
| 182 |
+// Units are case-insensitive, and the 'b' suffix is optional. |
|
| 183 |
+func RAMInBytes(size string) (bytes int64, err error) {
|
|
| 184 |
+ re, error := regexp.Compile("^(\\d+)([kKmMgG])?[bB]?$")
|
|
| 185 |
+ if error != nil { return -1, error }
|
|
| 186 |
+ |
|
| 187 |
+ matches := re.FindStringSubmatch(size) |
|
| 188 |
+ |
|
| 189 |
+ if len(matches) != 3 {
|
|
| 190 |
+ return -1, fmt.Errorf("Invalid size: '%s'", size)
|
|
| 191 |
+ } |
|
| 192 |
+ |
|
| 193 |
+ memLimit, error := strconv.ParseInt(matches[1], 10, 0) |
|
| 194 |
+ if error != nil { return -1, error }
|
|
| 195 |
+ |
|
| 196 |
+ unit := strings.ToLower(matches[2]) |
|
| 197 |
+ |
|
| 198 |
+ if unit == "k" {
|
|
| 199 |
+ memLimit *= 1024 |
|
| 200 |
+ } else if unit == "m" {
|
|
| 201 |
+ memLimit *= 1024*1024 |
|
| 202 |
+ } else if unit == "g" {
|
|
| 203 |
+ memLimit *= 1024*1024*1024 |
|
| 204 |
+ } |
|
| 205 |
+ |
|
| 206 |
+ return memLimit, nil |
|
| 207 |
+} |
|
| 208 |
+ |
|
| 209 |
+ |
|
| 179 | 210 |
func Trunc(s string, maxlen int) string {
|
| 180 | 211 |
if len(s) <= maxlen {
|
| 181 | 212 |
return s |
| ... | ... |
@@ -265,6 +265,39 @@ func TestHumanSize(t *testing.T) {
|
| 265 | 265 |
} |
| 266 | 266 |
} |
| 267 | 267 |
|
| 268 |
+func TestRAMInBytes(t *testing.T) {
|
|
| 269 |
+ assertRAMInBytes(t, "32", false, 32) |
|
| 270 |
+ assertRAMInBytes(t, "32b", false, 32) |
|
| 271 |
+ assertRAMInBytes(t, "32B", false, 32) |
|
| 272 |
+ assertRAMInBytes(t, "32k", false, 32*1024) |
|
| 273 |
+ assertRAMInBytes(t, "32K", false, 32*1024) |
|
| 274 |
+ assertRAMInBytes(t, "32kb", false, 32*1024) |
|
| 275 |
+ assertRAMInBytes(t, "32Kb", false, 32*1024) |
|
| 276 |
+ assertRAMInBytes(t, "32Mb", false, 32*1024*1024) |
|
| 277 |
+ assertRAMInBytes(t, "32Gb", false, 32*1024*1024*1024) |
|
| 278 |
+ |
|
| 279 |
+ assertRAMInBytes(t, "", true, -1) |
|
| 280 |
+ assertRAMInBytes(t, "hello", true, -1) |
|
| 281 |
+ assertRAMInBytes(t, "-32", true, -1) |
|
| 282 |
+ assertRAMInBytes(t, " 32 ", true, -1) |
|
| 283 |
+ assertRAMInBytes(t, "32 mb", true, -1) |
|
| 284 |
+ assertRAMInBytes(t, "32m b", true, -1) |
|
| 285 |
+ assertRAMInBytes(t, "32bm", true, -1) |
|
| 286 |
+} |
|
| 287 |
+ |
|
| 288 |
+func assertRAMInBytes(t *testing.T, size string, expectError bool, expectedBytes int64) {
|
|
| 289 |
+ actualBytes, err := RAMInBytes(size) |
|
| 290 |
+ if (err != nil) && !expectError {
|
|
| 291 |
+ t.Errorf("Unexpected error parsing '%s': %s", size, err)
|
|
| 292 |
+ } |
|
| 293 |
+ if (err == nil) && expectError {
|
|
| 294 |
+ t.Errorf("Expected to get an error parsing '%s', but got none (bytes=%d)", size, actualBytes)
|
|
| 295 |
+ } |
|
| 296 |
+ if actualBytes != expectedBytes {
|
|
| 297 |
+ t.Errorf("Expected '%s' to parse as %d bytes, got %d", size, expectedBytes, actualBytes)
|
|
| 298 |
+ } |
|
| 299 |
+} |
|
| 300 |
+ |
|
| 268 | 301 |
func TestParseHost(t *testing.T) {
|
| 269 | 302 |
if addr, err := ParseHost("127.0.0.1", 4243, "0.0.0.0"); err != nil || addr != "tcp://0.0.0.0:4243" {
|
| 270 | 303 |
t.Errorf("0.0.0.0 -> expected tcp://0.0.0.0:4243, got %s", addr)
|