diff --git a/bin/docker_command.py b/bin/docker_command.py index 4db69996..8b47f0c1 100644 --- a/bin/docker_command.py +++ b/bin/docker_command.py @@ -41,7 +41,6 @@ def unset_swap(value: str) -> bool: def zero(value: str) -> bool: - """Whether a value is 0 in any unit: a -m 0 leaves memory unlimited, and would override the limit capped adds.""" return size(value) == 0 @@ -85,13 +84,14 @@ def helped(arguments: list[str]) -> bool: def capped(arguments: list[str], share: str) -> list[str]: """docker's arguments, with each limit a container they make lacks added right after the subcommand. - A container that sets neither limit gets share for both. One that sets one limit gets the other at the same size, since docker refuses less swap than memory. A --memory-swap of -1 or 0, and a --memory of 0, is dropped; every other word stays. + A container that sets neither limit gets share for both. One that sets one limit gets the other at the same size, since docker refuses less swap than memory. A --memory-swap of -1 or 0 is dropped; every other word stays. Arguments with a --memory of 0 anywhere come back unchanged. """ - if not unlimited(arguments): + # A -m 0 asks docker for unlimited memory, and would override an added limit; it may also be the container's own, as in grep -m 0, so it is neither dropped nor capped. + if not unlimited(arguments) or option_value(arguments, MEMORY_OPTIONS, zero): return arguments memory = option_value(arguments, MEMORY_OPTIONS, memory_limit) swap = option_value(arguments, SWAP_OPTIONS, swap_limit) - rest = without(without(arguments, SWAP_OPTIONS, unset_swap), MEMORY_OPTIONS, zero) + rest = without(arguments, SWAP_OPTIONS, unset_swap) limits = ([] if memory else ["--memory", swap or share]) + ([] if swap else ["--memory-swap", memory or share]) after = subcommand_at(rest)[-1] + 1 return [*rest[:after], *limits, *rest[after:]] diff --git a/bin/run_heavy/main.py b/bin/run_heavy/main.py index 44176e39..76ae9c43 100644 --- a/bin/run_heavy/main.py +++ b/bin/run_heavy/main.py @@ -16,7 +16,8 @@ first on the command's PATH. That docker adds the limits a container lacks, then - A container that sets neither limit gets a memory limit and a swap limit, both the share. - A container that sets one of them keeps it, and gets the other at the same size. -- A --memory-swap of -1, unlimited swap, or a --memory of 0 is dropped and replaced. +- A --memory-swap of -1, unlimited swap, is dropped and replaced. +- A --memory of 0, unlimited memory, leaves the call as it is. Every other docker call goes through unchanged. A docker compose service, and a docker named by its full path, get no cap: give those containers their own limits. diff --git a/bin/test_docker_command.py b/bin/test_docker_command.py index 1845e291..d87f8568 100644 --- a/bin/test_docker_command.py +++ b/bin/test_docker_command.py @@ -13,7 +13,6 @@ class Capped(unittest.TestCase): (["run", "img", "python", "-m", "pytest"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "python", "-m", "pytest"]), (["run", "img", "grep", "-m", "1", "x"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "grep", "-m", "1", "x"]), (["run", "img", "git", "tag", "-m", "1.2.3"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "git", "tag", "-m", "1.2.3"]), - (["run", "-m", "0", "img"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img"]), (["run", "-m", "6143k", "img"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "-m", "6143k", "img"]), (["run", "img", "tool", "-m", "6"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "tool", "-m", "6"]), (["run", "img", "stress", "--memory", "2"], ["run", "--memory", "6144m", "--memory-swap", "6144m", "img", "stress", "--memory", "2"]), @@ -35,7 +34,7 @@ class Capped(unittest.TestCase): self.assertEqual(capped(given, "6144m"), expected) def test_a_container_with_both_limits_or_another_docker_call_is_left_alone(self): - for given in [["run", "-m", "10g", "--memory-swap", "12g", "img"], ["ps"], ["build", "."], ["run", "--help"], ["compose", "run", "app"], ["exec", "box", "true"]]: + for given in [["run", "-m", "0", "img"], ["run", "img", "grep", "-m", "0", "x"], ["run", "-m", "10g", "--memory-swap", "12g", "img"], ["ps"], ["build", "."], ["run", "--help"], ["compose", "run", "app"], ["exec", "box", "true"]]: with self.subTest(given): self.assertEqual(capped(given, "6144m"), given)