Skip to content

Commit

Permalink
docker compat: use hyphens to join container names
Browse files Browse the repository at this point in the history
docker-compose uses - not _ to join container names to project.
not a huge thing, but for services making assumptions based on this
it can catch off guard, and is easy to make the same.

Signed-off-by: Matt Phillips <[email protected]>
  • Loading branch information
mattp- committed Nov 29, 2024
1 parent 626e278 commit 8832189
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 31 deletions.
12 changes: 6 additions & 6 deletions podman_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def fix_mount_dict(compose, mount_dict, srv_name):
elif external:
vol["name"] = f"{source}"
else:
vol["name"] = f"{compose.project_name}_{source}"
vol["name"] = f"{compose.project_name}-{source}"
return mount_dict


Expand Down Expand Up @@ -351,7 +351,7 @@ def default_network_name_for_project(compose, net, is_ext):

default_net_name_compat = compose.x_podman.get("default_net_name_compat", False)
if default_net_name_compat is True:
return f"{compose.project_name.replace('-', '')}_{net}"
return f"{compose.project_name.replace('-', '')}-{net}"
return f"{compose.project_name}_{net}"


Expand All @@ -369,7 +369,7 @@ def transform(args, project_name, given_containers):
pod_name = None
pods = []
else:
pod_name = f"pod_{project_name}"
pod_name = f"pod-{project_name}"
pod = {"name": pod_name}
pods = [pod]
containers = []
Expand Down Expand Up @@ -1979,7 +1979,7 @@ def _parse_compose_file(self):

container_names_by_service[service_name] = []
for num in range(1, replicas + 1):
name0 = f"{project_name}_{service_name}_{num}"
name0 = f"{project_name}-{service_name}-{num}"
if num == 1:
name = service_desc.get("container_name", name0)
else:
Expand All @@ -1995,7 +1995,7 @@ def _parse_compose_file(self):
x_podman = service_desc.get("x-podman", None)
rootfs_mode = x_podman is not None and x_podman.get("rootfs", None) is not None
if "image" not in cnt and not rootfs_mode:
cnt["image"] = f"{project_name}_{service_name}"
cnt["image"] = f"{project_name}-{service_name}"
labels = norm_as_list(cnt.get("labels", None))
cnt["ports"] = norm_ports(cnt.get("ports", None))
labels.extend(podman_compose_labels)
Expand Down Expand Up @@ -2764,7 +2764,7 @@ async def compose_run(compose, args):

def compose_run_update_container_from_args(compose, cnt, args):
# adjust one-off container options
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
name0 = "{}-{}-tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
cnt["name"] = args.name or name0
if args.entrypoint:
cnt["entrypoint"] = args.entrypoint
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/test_podman_compose_deps.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_deps(self):
"wget -O - http://web:8000/hosts",
])
self.assertIn(b"HTTP request sent, awaiting response... 200 OK", output)
self.assertIn(b"deps_web_1", output)
self.assertIn(b"deps-web-1", output)
finally:
self.run_subprocess_assert_returncode([
podman_compose_path(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_extends_w_empty_service(self):
compose_yaml_path(),
"ps",
])
self.assertIn("extends_w_empty_service_web_1", str(output))
self.assertIn("extends_w_empty_service-web-1", str(output))
finally:
self.run_subprocess_assert_returncode([
podman_compose_path(),
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_podman_compose_extends_w_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ def test_extends_w_file(self): # when file is Dockerfile for building the image
compose_yaml_path(),
"ps",
])
self.assertIn("extends_w_file_web_1", str(output))
self.assertIn("extends_w_file_important_web_1", str(output))
self.assertIn("extends_w_file-web-1", str(output))
self.assertIn("extends_w_file-important_web-1", str(output))
finally:
self.run_subprocess_assert_returncode([
podman_compose_path(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_extends_w_file_subdir(self): # when file is Dockerfile for building th
compose_yaml_path(),
"ps",
])
self.assertIn("extends_w_file_subdir_web_1", str(output))
self.assertIn("extends_w_file_subdir-web-1", str(output))
finally:
self.run_subprocess_assert_returncode([
podman_compose_path(),
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_podman_compose_ipam_default.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SPDX-License-Identifier: GPL-2.0
# SPDt-License-Identifier: GPL-2.0

import json
import os
Expand Down Expand Up @@ -34,7 +34,7 @@ def test_ipam_default(self):
[
"podman",
"inspect",
"ipam_default_testipam_1",
"ipam_default-testipam-1",
],
)
network_info = json.loads(output.decode('utf-8'))[0]
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_podman_compose_multicompose.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ def test_multicompose(self):
os.path.join(os.path.join(test_path(), "multicompose"), "d2/docker-compose.yml"),
"ps",
])
self.assertIn(b"d1_web1_1", output)
self.assertIn(b"d1_web2_1", output)
self.assertIn(b"d1-web1-1", output)
self.assertIn(b"d1-web2-1", output)

output, _ = self.run_subprocess_assert_returncode([
"podman",
"exec",
"-ti",
"d1_web1_1",
"d1-web1-1",
"sh",
"-c",
"set",
Expand All @@ -58,7 +58,7 @@ def test_multicompose(self):
"podman",
"exec",
"-ti",
"d1_web2_1",
"d1-web2-1",
"sh",
"-c",
"set",
Expand All @@ -70,7 +70,7 @@ def test_multicompose(self):
"podman",
"exec",
"-ti",
"d1_web1_1",
"d1-web1-1",
"sh",
"-c",
"cat /var/www/html/index.txt",
Expand All @@ -82,7 +82,7 @@ def test_multicompose(self):
"podman",
"exec",
"-ti",
"d1_web2_1",
"d1-web2-1",
"sh",
"-c",
"cat /var/www/html/index.txt",
Expand Down
8 changes: 4 additions & 4 deletions tests/integration/test_podman_compose_nets_test1.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_nets_test1(self):
compose_yaml_path(),
"ps",
])
self.assertIn(b"nets_test1_web1_1", output)
self.assertIn(b"nets_test1_web2_1", output)
self.assertIn(b"nets_test1-web1-1", output)
self.assertIn(b"nets_test1-web2-1", output)

response = requests.get('http://localhost:8001/index.txt')
self.assertTrue(response.ok)
Expand All @@ -49,7 +49,7 @@ def test_nets_test1(self):
output, _ = self.run_subprocess_assert_returncode([
"podman",
"inspect",
"nets_test1_web1_1",
"nets_test1-web1-1",
])
container_info = json.loads(output.decode('utf-8'))[0]

Expand All @@ -70,7 +70,7 @@ def test_nets_test1(self):
output, _ = self.run_subprocess_assert_returncode([
"podman",
"inspect",
"nets_test1_web2_1",
"nets_test1-web2-1",
])
container_info = json.loads(output.decode('utf-8'))[0]
self.assertEqual(
Expand Down
12 changes: 6 additions & 6 deletions tests/integration/test_podman_compose_nets_test2.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ def test_nets_test2(self):
compose_yaml_path(),
"ps",
])
self.assertIn(b"nets_test2_web1_1", output)
self.assertIn(b"nets_test2_web2_1", output)
self.assertIn(b"nets_test2-web1-1", output)
self.assertIn(b"nets_test2-web2-1", output)

response = requests.get('http://localhost:8001/index.txt')
self.assertTrue(response.ok)
Expand All @@ -49,13 +49,13 @@ def test_nets_test2(self):
output, _ = self.run_subprocess_assert_returncode([
"podman",
"inspect",
"nets_test2_web1_1",
"nets_test2-web1-1",
])
container_info = json.loads(output.decode('utf-8'))[0]

# check if network got specific name from networks top-level element
self.assertEqual(
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack"
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2-mystack"
)

# check if Host port is the same as prodvided by the service port
Expand All @@ -70,12 +70,12 @@ def test_nets_test2(self):
output, _ = self.run_subprocess_assert_returncode([
"podman",
"inspect",
"nets_test2_web2_1",
"nets_test2-web2-1",
])
container_info = json.loads(output.decode('utf-8'))[0]

self.assertEqual(
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack"
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2-mystack"
)

self.assertEqual(
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/test_podman_compose_networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ def test_networks(self):
'"{{.Names}}"',
]
out, _ = self.run_subprocess_assert_returncode(check_cmd)
self.assertIn(b"nets_test_ip_web1_1", out)
self.assertIn(b"nets_test_ip_web2_1", out)
self.assertIn(b"nets_test_ip-web1-1", out)
self.assertIn(b"nets_test_ip-web2-1", out)

expected_wget = {
"172.19.1.10": "test1",
Expand Down

0 comments on commit 8832189

Please sign in to comment.