Browse code

Set minimum memory limit to 6M, to account for higher startup memory use

For some time, we defined a minimum limit for `--memory` limits to account for
overhead during startup, and to supply a reasonable functional container.

Changes in the runtime (runc) introduced a higher memory footprint during container
startup, which now lead to obscure error-messages that are unfriendly for users:

run --rm --memory=4m alpine echo success
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "process_linux.go:449: container init caused \"process_linux.go:415: setting cgroup config for procHooks process caused \\\"failed to write \\\\\\\"4194304\\\\\\\" to \\\\\\\"/sys/fs/cgroup/memory/docker/1254c8d63f85442e599b17dff895f4543c897755ee3bd9b56d5d3d17724b38d7/memory.limit_in_bytes\\\\\\\": write /sys/fs/cgroup/memory/docker/1254c8d63f85442e599b17dff895f4543c897755ee3bd9b56d5d3d17724b38d7/memory.limit_in_bytes: device or resource busy\\\"\"": unknown.
ERRO[0000] error waiting for container: context canceled

Containers that fail to start because of this limit, will not be marked as OOMKilled,
which makes it harder for users to find the cause of the failure.

Note that _after_ this memory is only required during startup of the container. After
the container was started, the container may not consume this memory, and limits
could (manually) be lowered, for example, an alpine container running only a shell
can run with 512k of memory;

echo 524288 > /sys/fs/cgroup/memory/docker/acdd326419f0898be63b0463cfc81cd17fb34d2dae6f8aa3768ee6a075ca5c86/memory.limit_in_bytes

However, restarting the container will reset that manual limit to the container's
configuration. While `docker container update` would allow for the updated limit to
be persisted, (re)starting the container after updating produces the same error message
again, so we cannot use different limits for `docker run` / `docker create` and `docker update`.

This patch raises the minimum memory limnit to 6M, so that a better error-message is
produced if a user tries to create a container with a memory-limit that is too low:

docker create --memory=4m alpine echo success
docker: Error response from daemon: Minimum memory limit allowed is 6MB.

Possibly, this constraint could be handled by runc, so that different runtimes
could set a best-matching limit (other runtimes may require less overhead).

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>

Sebastiaan van Stijn authored on 2020/07/01 19:04:23
Showing 5 changed files
... ...
@@ -68,8 +68,8 @@ const (
68 68
 	linuxMinCPUShares = 2
69 69
 	linuxMaxCPUShares = 262144
70 70
 	platformSupported = true
71
-	// It's not kernel limit, we want this 4M limit to supply a reasonable functional container
72
-	linuxMinMemory = 4194304
71
+	// It's not kernel limit, we want this 6M limit to account for overhead during startup, and to supply a reasonable functional container
72
+	linuxMinMemory = 6291456
73 73
 	// constants for remapped root settings
74 74
 	defaultIDSpecifier = "default"
75 75
 	defaultRemappedID  = "dockremap"
... ...
@@ -433,7 +433,7 @@ func verifyPlatformContainerResources(resources *containertypes.Resources, sysIn
433 433
 
434 434
 	// memory subsystem checks and adjustments
435 435
 	if resources.Memory != 0 && resources.Memory < linuxMinMemory {
436
-		return warnings, fmt.Errorf("Minimum memory limit allowed is 4MB")
436
+		return warnings, fmt.Errorf("Minimum memory limit allowed is 6MB")
437 437
 	}
438 438
 	if resources.Memory > 0 && !sysInfo.MemoryLimit {
439 439
 		warnings = append(warnings, "Your kernel does not support memory limit capabilities or the cgroup is not mounted. Limitation discarded.")
... ...
@@ -871,7 +871,7 @@ func (s *DockerSuite) TestCreateWithTooLowMemoryLimit(c *testing.T) {
871 871
 	} else {
872 872
 		assert.Assert(c, res.StatusCode != http.StatusOK)
873 873
 	}
874
-	assert.Assert(c, strings.Contains(string(b), "Minimum memory limit allowed is 4MB"))
874
+	assert.Assert(c, strings.Contains(string(b), "Minimum memory limit allowed is 6MB"))
875 875
 }
876 876
 
877 877
 func (s *DockerSuite) TestContainerAPIRename(c *testing.T) {
... ...
@@ -2826,13 +2826,11 @@ func (s *DockerSuite) TestRunPIDHostWithChildIsKillable(c *testing.T) {
2826 2826
 }
2827 2827
 
2828 2828
 func (s *DockerSuite) TestRunWithTooSmallMemoryLimit(c *testing.T) {
2829
-	// TODO Windows. This may be possible to enable once Windows supports
2830
-	// memory limits on containers
2829
+	// TODO Windows. This may be possible to enable once Windows supports memory limits on containers
2831 2830
 	testRequires(c, DaemonIsLinux)
2832
-	// this memory limit is 1 byte less than the min, which is 4MB
2833
-	// https://github.com/docker/docker/blob/v1.5.0/daemon/create.go#L22
2834
-	out, _, err := dockerCmdWithError("run", "-m", "4194303", "busybox")
2835
-	if err == nil || !strings.Contains(out, "Minimum memory limit allowed is 4MB") {
2831
+	// this memory limit is 1 byte less than the min (daemon.linuxMinMemory), which is 6MB (6291456 bytes)
2832
+	out, _, err := dockerCmdWithError("create", "-m", "6291455", "busybox")
2833
+	if err == nil || !strings.Contains(out, "Minimum memory limit allowed is 6MB") {
2836 2834
 		c.Fatalf("expected run to fail when using too low a memory limit: %q", out)
2837 2835
 	}
2838 2836
 }
... ...
@@ -108,7 +108,7 @@ func (s *DockerSuite) TestUpdateContainerInvalidValue(c *testing.T) {
108 108
 	dockerCmd(c, "run", "-d", "--name", name, "-m", "300M", "busybox", "true")
109 109
 	out, _, err := dockerCmdWithError("update", "-m", "2M", name)
110 110
 	assert.ErrorContains(c, err, "")
111
-	expected := "Minimum memory limit allowed is 4MB"
111
+	expected := "Minimum memory limit allowed is 6MB"
112 112
 	assert.Assert(c, strings.Contains(out, expected))
113 113
 }
114 114
 
... ...
@@ -175,7 +175,7 @@ func (s *DockerSuite) TestDeprecatedStartWithTooLowMemoryLimit(c *testing.T) {
175 175
 	} else {
176 176
 		assert.Equal(c, res.StatusCode, http.StatusBadRequest)
177 177
 	}
178
-	assert.Assert(c, is.Contains(string(b), "Minimum memory limit allowed is 4MB"))
178
+	assert.Assert(c, is.Contains(string(b), "Minimum memory limit allowed is 6MB"))
179 179
 }
180 180
 
181 181
 // #14640