Skip to content

Multiple env files as arguments or environment variable #813

@zarmhast

Description

@zarmhast

Describe the bug

podman-compose supports only a single --env-file argument at a time and ignores the COMPOSE_ENV_FILES environment variable.

To Reproduce

#.env.default
SUBJECT=World
GREETING=Hello
#.env.override
SUBJECT=Podman
# docker-compose.yml
services:
  dummy:
    image: busybox
    command: ["sh", "-c", "echo $GREETING $SUBJECT"]

Both commands should give the same result:

podman-compose --env-file .env.default --env-file .env.override up
COMPOSE_ENV_FILES=.env.default,.env.override podman-compose up

Expected behavior

Hello Podman

Actual behavior
The argument invocation results in:

Podman

The env variable invocation is empty

Output

$ podman-compose version
using podman version: 4.8.1
podman-compose version  1.0.6
podman --version 
podman version 4.8.1

Environment:

  • OS: Linux / WSL / Mac
  • podman version: 4.8.1
  • podman compose version: 1.0.6

Additional context
A suggested fix could be this patch:

Index: podman_compose.py
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/podman_compose.py b/podman_compose.py
--- a/podman_compose.py	(revision bce40c2db30fb0ffb9264b5f51535c26f48fe983)
+++ b/podman_compose.py	(date 1702365137091)
@@ -1478,11 +1478,11 @@
             sys.exit(1)
 
     def get_podman_args(self, cmd):
-        xargs = []
+        xargs: list[str] = []
         for args in self.global_args.podman_args:
             xargs.extend(shlex.split(args))
         cmd_norm = cmd if cmd != "create" else "run"
-        cmd_args = self.global_args.__dict__.get(f"podman_{cmd_norm}_args", None) or []
+        cmd_args = getattr(self.global_args, f"podman_{cmd_norm}_args", __default=None) or []
         for args in cmd_args:
             xargs.extend(shlex.split(args))
         return xargs
@@ -1565,9 +1565,9 @@
 
         # env-file is relative to the CWD
         dotenv_dict = {}
-        if args.env_file:
-            dotenv_path = os.path.realpath(args.env_file)
-            dotenv_dict = dotenv_to_dict(dotenv_path)
+        for env_file in args.env_file or []:
+            dotenv_path = os.path.realpath(env_file)
+            dotenv_dict.update(dotenv_to_dict(dotenv_path))
 
         # TODO: remove next line
         os.chdir(dirname)
@@ -1811,8 +1811,8 @@
             for cmd_parser in cmd._parse_args:  # pylint: disable=protected-access
                 cmd_parser(subparser)
         self.global_args = parser.parse_args()
-        if self.global_args.version:
-            self.global_args.command = "version"
+        if len(self.global_args.env_file) == 0:
+            self.global_args.env_file = [fn.strip() for fn in os.environ.get("COMPOSE_ENV_FILES", ".env").split(",")]
         if not self.global_args.command or self.global_args.command == "help":
             parser.print_help()
             sys.exit(-1)
@@ -1820,7 +1820,13 @@
 
     @staticmethod
     def _init_global_parser(parser):
-        parser.add_argument("-v", "--version", help="show version", action="store_true")
+        parser.add_argument(
+            "-v", "--version",
+            help="show version",
+            action="store_const",
+            const="version",
+            dest="command",
+        )
         parser.add_argument(
             "--in-pod",
             help="pod creation",
@@ -1837,10 +1843,11 @@
         )
         parser.add_argument(
             "--env-file",
-            help="Specify an alternate environment file",
+            help="Specify an alternate environment file (defaults to .env)",
             metavar="env_file",
+            action="append",
             type=str,
-            default=".env",
+            default=[],
         )
         parser.add_argument(
             "-f",

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions