Fix project name evaluation order

The COMPOSE_PROJECT_NAME environment variable must override the
top-level name: attribute in the Compose file.

The precedence order is defined in the docker compose documentation
https://docs.docker.com/compose/how-tos/project-name/#set-a-project-name

Signed-off-by: Ruben Jenster <r.jenster@drachenfels.de>
This commit is contained in:
Ruben Jenster 2025-03-12 19:04:38 +01:00 committed by Ruben Jenster
parent 1aa750bacf
commit 65b455f081
2 changed files with 14 additions and 8 deletions

View File

@ -0,0 +1 @@
- Fixed project name evaluation order to match the order defined in the [compose spec](https://docs.docker.com/compose/how-tos/project-name/#set-a-project-name).

View File

@ -1981,15 +1981,20 @@ class PodmanCompose:
content = normalize(content)
# log(filename, json.dumps(content, indent = 2))
# See also https://docs.docker.com/compose/how-tos/project-name/#set-a-project-name
# **project_name** is initialized to the argument of the `-p` command line flag.
if not project_name:
project_name = content.get("name")
if project_name is None:
# More strict then actually needed for simplicity:
# podman requires [a-zA-Z0-9][a-zA-Z0-9_.-]*
project_name = self.environ.get("COMPOSE_PROJECT_NAME", dir_basename.lower())
project_name = norm_re.sub("", project_name)
if not project_name:
raise RuntimeError(f"Project name [{dir_basename}] normalized to empty")
project_name = self.environ.get("COMPOSE_PROJECT_NAME")
if not project_name:
project_name = content.get("name")
if not project_name:
project_name = dir_basename.lower()
# More strict then actually needed for simplicity:
# podman requires [a-zA-Z0-9][a-zA-Z0-9_.-]*
project_name_normalized = norm_re.sub("", project_name)
if not project_name_normalized:
raise RuntimeError(f"Project name [{project_name}] normalized to empty")
project_name = project_name_normalized
self.project_name = project_name
self.environ.update({"COMPOSE_PROJECT_NAME": self.project_name})