From 92394785fa3e55b19402fc762c030d28b36b6cfc Mon Sep 17 00:00:00 2001 From: Lei Jitang Date: Mon, 8 Aug 2016 18:36:03 +0800 Subject: [PATCH] Fix update memory without memoryswap The memory should always be smaller than memoryswap, we should error out with message that user know how to do rather than just an invalid argument error if user update the memory limit bigger than already set memory swap. Signed-off-by: Lei Jitang --- container/container_unix.go | 5 +++++ integration-cli/docker_cli_update_unix_test.go | 14 ++++++++++++++ man/docker-update.1.md | 6 ++++++ 3 files changed, 25 insertions(+) diff --git a/container/container_unix.go b/container/container_unix.go index 2727b818f5..9219ca1569 100644 --- a/container/container_unix.go +++ b/container/container_unix.go @@ -283,6 +283,11 @@ func (container *Container) UpdateContainer(hostConfig *containertypes.HostConfi cResources.CpusetMems = resources.CpusetMems } if resources.Memory != 0 { + // if memory limit smaller than already set memoryswap limit and doesn't + // update the memoryswap limit, then error out. + if resources.Memory > cResources.MemorySwap && resources.MemorySwap == 0 { + return fmt.Errorf("Memory limit should be smaller than already set memoryswap limit, update the memoryswap at the same time") + } cResources.Memory = resources.Memory } if resources.MemorySwap != 0 { diff --git a/integration-cli/docker_cli_update_unix_test.go b/integration-cli/docker_cli_update_unix_test.go index ee1b2a3137..21fa9991bb 100644 --- a/integration-cli/docker_cli_update_unix_test.go +++ b/integration-cli/docker_cli_update_unix_test.go @@ -236,3 +236,17 @@ func (s *DockerSuite) TestUpdateStats(c *check.C) { c.Assert(preMemLimit, checker.Equals, curMemLimit) } + +func (s *DockerSuite) TestUpdateMemoryWithSwapMemory(c *check.C) { + testRequires(c, DaemonIsLinux) + testRequires(c, memoryLimitSupport) + testRequires(c, swapMemorySupport) + + name := "test-update-container" + dockerCmd(c, "run", "-d", "--name", name, "--memory", "300M", "busybox", "top") + out, _, err := dockerCmdWithError("update", "--memory", "800M", name) + c.Assert(err, checker.NotNil) + c.Assert(out, checker.Contains, "Memory limit should be smaller than already set memoryswap limit") + + dockerCmd(c, "update", "--memory", "800M", "--memory-swap", "1000M", name) +} diff --git a/man/docker-update.1.md b/man/docker-update.1.md index 68eb0ba4b6..ad86297a07 100644 --- a/man/docker-update.1.md +++ b/man/docker-update.1.md @@ -67,6 +67,12 @@ a running container with kernel memory initialized. **-m**, **--memory**="" Memory limit (format: , where unit = b, k, m or g) + Note that the memory should be smaller than the already set swap memory limit. + If you want update a memory limit bigger than the already set swap memory limit, + you should update swap memory limit at the same time. If you don't set swap memory + limit on docker create/run but only memory limit, the swap memory is double + the memory limit. + **--memory-reservation**="" Memory soft limit (format: [], where unit = b, k, m or g)