Skip to content

Commit 8832189

Browse files
committed
docker compat: use hyphens to join container names
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]>
1 parent 626e278 commit 8832189

10 files changed

+31
-31
lines changed

podman_compose.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def fix_mount_dict(compose, mount_dict, srv_name):
223223
elif external:
224224
vol["name"] = f"{source}"
225225
else:
226-
vol["name"] = f"{compose.project_name}_{source}"
226+
vol["name"] = f"{compose.project_name}-{source}"
227227
return mount_dict
228228

229229

@@ -351,7 +351,7 @@ def default_network_name_for_project(compose, net, is_ext):
351351

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

357357

@@ -369,7 +369,7 @@ def transform(args, project_name, given_containers):
369369
pod_name = None
370370
pods = []
371371
else:
372-
pod_name = f"pod_{project_name}"
372+
pod_name = f"pod-{project_name}"
373373
pod = {"name": pod_name}
374374
pods = [pod]
375375
containers = []
@@ -1979,7 +1979,7 @@ def _parse_compose_file(self):
19791979

19801980
container_names_by_service[service_name] = []
19811981
for num in range(1, replicas + 1):
1982-
name0 = f"{project_name}_{service_name}_{num}"
1982+
name0 = f"{project_name}-{service_name}-{num}"
19831983
if num == 1:
19841984
name = service_desc.get("container_name", name0)
19851985
else:
@@ -1995,7 +1995,7 @@ def _parse_compose_file(self):
19951995
x_podman = service_desc.get("x-podman", None)
19961996
rootfs_mode = x_podman is not None and x_podman.get("rootfs", None) is not None
19971997
if "image" not in cnt and not rootfs_mode:
1998-
cnt["image"] = f"{project_name}_{service_name}"
1998+
cnt["image"] = f"{project_name}-{service_name}"
19991999
labels = norm_as_list(cnt.get("labels", None))
20002000
cnt["ports"] = norm_ports(cnt.get("ports", None))
20012001
labels.extend(podman_compose_labels)
@@ -2764,7 +2764,7 @@ async def compose_run(compose, args):
27642764

27652765
def compose_run_update_container_from_args(compose, cnt, args):
27662766
# adjust one-off container options
2767-
name0 = "{}_{}_tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
2767+
name0 = "{}-{}-tmp{}".format(compose.project_name, args.service, random.randrange(0, 65536))
27682768
cnt["name"] = args.name or name0
27692769
if args.entrypoint:
27702770
cnt["entrypoint"] = args.entrypoint

tests/integration/test_podman_compose_deps.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_deps(self):
2626
"wget -O - http://web:8000/hosts",
2727
])
2828
self.assertIn(b"HTTP request sent, awaiting response... 200 OK", output)
29-
self.assertIn(b"deps_web_1", output)
29+
self.assertIn(b"deps-web-1", output)
3030
finally:
3131
self.run_subprocess_assert_returncode([
3232
podman_compose_path(),

tests/integration/test_podman_compose_extends_w_empty_service.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_extends_w_empty_service(self):
2929
compose_yaml_path(),
3030
"ps",
3131
])
32-
self.assertIn("extends_w_empty_service_web_1", str(output))
32+
self.assertIn("extends_w_empty_service-web-1", str(output))
3333
finally:
3434
self.run_subprocess_assert_returncode([
3535
podman_compose_path(),

tests/integration/test_podman_compose_extends_w_file.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ def test_extends_w_file(self): # when file is Dockerfile for building the image
2929
compose_yaml_path(),
3030
"ps",
3131
])
32-
self.assertIn("extends_w_file_web_1", str(output))
33-
self.assertIn("extends_w_file_important_web_1", str(output))
32+
self.assertIn("extends_w_file-web-1", str(output))
33+
self.assertIn("extends_w_file-important_web-1", str(output))
3434
finally:
3535
self.run_subprocess_assert_returncode([
3636
podman_compose_path(),

tests/integration/test_podman_compose_extends_w_file_subdir.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def test_extends_w_file_subdir(self): # when file is Dockerfile for building th
2929
compose_yaml_path(),
3030
"ps",
3131
])
32-
self.assertIn("extends_w_file_subdir_web_1", str(output))
32+
self.assertIn("extends_w_file_subdir-web-1", str(output))
3333
finally:
3434
self.run_subprocess_assert_returncode([
3535
podman_compose_path(),

tests/integration/test_podman_compose_ipam_default.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# SPDX-License-Identifier: GPL-2.0
1+
# SPDt-License-Identifier: GPL-2.0
22

33
import json
44
import os
@@ -34,7 +34,7 @@ def test_ipam_default(self):
3434
[
3535
"podman",
3636
"inspect",
37-
"ipam_default_testipam_1",
37+
"ipam_default-testipam-1",
3838
],
3939
)
4040
network_info = json.loads(output.decode('utf-8'))[0]

tests/integration/test_podman_compose_multicompose.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ def test_multicompose(self):
3838
os.path.join(os.path.join(test_path(), "multicompose"), "d2/docker-compose.yml"),
3939
"ps",
4040
])
41-
self.assertIn(b"d1_web1_1", output)
42-
self.assertIn(b"d1_web2_1", output)
41+
self.assertIn(b"d1-web1-1", output)
42+
self.assertIn(b"d1-web2-1", output)
4343

4444
output, _ = self.run_subprocess_assert_returncode([
4545
"podman",
4646
"exec",
4747
"-ti",
48-
"d1_web1_1",
48+
"d1-web1-1",
4949
"sh",
5050
"-c",
5151
"set",
@@ -58,7 +58,7 @@ def test_multicompose(self):
5858
"podman",
5959
"exec",
6060
"-ti",
61-
"d1_web2_1",
61+
"d1-web2-1",
6262
"sh",
6363
"-c",
6464
"set",
@@ -70,7 +70,7 @@ def test_multicompose(self):
7070
"podman",
7171
"exec",
7272
"-ti",
73-
"d1_web1_1",
73+
"d1-web1-1",
7474
"sh",
7575
"-c",
7676
"cat /var/www/html/index.txt",
@@ -82,7 +82,7 @@ def test_multicompose(self):
8282
"podman",
8383
"exec",
8484
"-ti",
85-
"d1_web2_1",
85+
"d1-web2-1",
8686
"sh",
8787
"-c",
8888
"cat /var/www/html/index.txt",

tests/integration/test_podman_compose_nets_test1.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def test_nets_test1(self):
3434
compose_yaml_path(),
3535
"ps",
3636
])
37-
self.assertIn(b"nets_test1_web1_1", output)
38-
self.assertIn(b"nets_test1_web2_1", output)
37+
self.assertIn(b"nets_test1-web1-1", output)
38+
self.assertIn(b"nets_test1-web2-1", output)
3939

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

@@ -70,7 +70,7 @@ def test_nets_test1(self):
7070
output, _ = self.run_subprocess_assert_returncode([
7171
"podman",
7272
"inspect",
73-
"nets_test1_web2_1",
73+
"nets_test1-web2-1",
7474
])
7575
container_info = json.loads(output.decode('utf-8'))[0]
7676
self.assertEqual(

tests/integration/test_podman_compose_nets_test2.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ def test_nets_test2(self):
3434
compose_yaml_path(),
3535
"ps",
3636
])
37-
self.assertIn(b"nets_test2_web1_1", output)
38-
self.assertIn(b"nets_test2_web2_1", output)
37+
self.assertIn(b"nets_test2-web1-1", output)
38+
self.assertIn(b"nets_test2-web2-1", output)
3939

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

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

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

7777
self.assertEqual(
78-
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2_mystack"
78+
list(container_info["NetworkSettings"]["Networks"].keys())[0], "nets_test2-mystack"
7979
)
8080

8181
self.assertEqual(

tests/integration/test_podman_compose_networks.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ def test_networks(self):
6363
'"{{.Names}}"',
6464
]
6565
out, _ = self.run_subprocess_assert_returncode(check_cmd)
66-
self.assertIn(b"nets_test_ip_web1_1", out)
67-
self.assertIn(b"nets_test_ip_web2_1", out)
66+
self.assertIn(b"nets_test_ip-web1-1", out)
67+
self.assertIn(b"nets_test_ip-web2-1", out)
6868

6969
expected_wget = {
7070
"172.19.1.10": "test1",

0 commit comments

Comments
 (0)