Ensure that network_mode and networks are not present at the same time

See
https://docs.docker.com/reference/compose-file/services/#network_mode

Signed-off-by: Povilas Kanapickas <povilas@radix.lt>
This commit is contained in:
Povilas Kanapickas 2024-12-18 19:59:23 +02:00
parent a023dc145b
commit 2891be01d7
3 changed files with 17 additions and 0 deletions

View File

@ -0,0 +1,2 @@
Improved error detection by rejecting service definitions that contain both "network_mode" and
"networks" keys, which is not allowed.

View File

@ -880,6 +880,12 @@ async def assert_cnt_nets(compose, cnt):
def get_net_args_from_network_mode(compose, cnt):
net_args = []
net = cnt.get("network_mode")
service_name = cnt["service_name"]
if "networks" in cnt:
raise ValueError(
f"networks and network_mode must not be present in the same service [{service_name}]"
)
is_bridge = False
if net == "none":

View File

@ -59,6 +59,15 @@ class TestGetNetArgs(unittest.TestCase):
args = get_net_args(compose, container)
self.assertListEqual(expected_args, args)
def test_network_mode_and_networks_unsupported(self):
compose = get_networked_compose()
container = get_minimal_container()
container["networks"] = {"net0": {}}
container["network_mode"] = "none"
with self.assertRaises(ValueError):
get_net_args(compose, container)
def test_alias(self):
compose = get_networked_compose()
container = get_minimal_container()