Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple env files as arguments or environment variable #813

Open
zarmhast opened this issue Dec 12, 2023 · 1 comment
Open

Multiple env files as arguments or environment variable #813

zarmhast opened this issue Dec 12, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@zarmhast
Copy link

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",
@zarmhast zarmhast added the bug Something isn't working label Dec 12, 2023
@AttilaTheFun
Copy link

I've encountered this issue as well -- if you pass multiple env files to podman-compose, only the values from the last one are used. I believe this is supposed to be supported according to the spec.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants