mirror of
https://github.com/containers/podman-compose.git
synced 2025-06-07 21:46:41 +02:00
Merge pull request #956 from Genzer/fix/loading-.env-breaking-since-1.1.0
Load .env from Compose file's directory and cwd
This commit is contained in:
commit
16cbcf4152
@ -1798,8 +1798,14 @@ class PodmanCompose:
|
|||||||
# env-file is relative to the CWD
|
# env-file is relative to the CWD
|
||||||
dotenv_dict = {}
|
dotenv_dict = {}
|
||||||
if args.env_file:
|
if args.env_file:
|
||||||
|
# Load .env from the Compose file's directory to preserve
|
||||||
|
# behavior prior to 1.1.0 and to match with Docker Compose (v2).
|
||||||
|
if ".env" == args.env_file:
|
||||||
|
project_dotenv_file = os.path.realpath(os.path.join(dirname, ".env"))
|
||||||
|
if os.path.exists(project_dotenv_file):
|
||||||
|
dotenv_dict.update(dotenv_to_dict(project_dotenv_file))
|
||||||
dotenv_path = os.path.realpath(args.env_file)
|
dotenv_path = os.path.realpath(args.env_file)
|
||||||
dotenv_dict = dotenv_to_dict(dotenv_path)
|
dotenv_dict.update(dotenv_to_dict(dotenv_path))
|
||||||
|
|
||||||
# TODO: remove next line
|
# TODO: remove next line
|
||||||
os.chdir(dirname)
|
os.chdir(dirname)
|
||||||
|
2
tests/env-file-tests/.env
Normal file
2
tests/env-file-tests/.env
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ZZVAR1='This value is overwritten by env-file-tests/.env'
|
||||||
|
ZZVAR3='This value is loaded from env-file-tests/.env'
|
4
tests/env-file-tests/.gitignore
vendored
Normal file
4
tests/env-file-tests/.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
# This overrides the repository root .gitignore (ignoring all .env).
|
||||||
|
# The .env files in this directory are important for the test cases.
|
||||||
|
!.env
|
||||||
|
!project/.env
|
@ -25,3 +25,13 @@ based on environment variable precedent this command should give podman-rocks-32
|
|||||||
```
|
```
|
||||||
ZZVAR1=podman-rocks-321 podman-compose -f $(pwd)/project/container-compose.yaml --env-file $(pwd)/env-files/project-1.env up
|
ZZVAR1=podman-rocks-321 podman-compose -f $(pwd)/project/container-compose.yaml --env-file $(pwd)/env-files/project-1.env up
|
||||||
```
|
```
|
||||||
|
|
||||||
|
_The below test should print three environment variables_
|
||||||
|
|
||||||
|
```
|
||||||
|
podman-compose -f $(pwd)/project/container-compose.load-.env-in-project.yaml run --rm app
|
||||||
|
|
||||||
|
ZZVAR1=This value is overwritten by env-file-tests/.env
|
||||||
|
ZZVAR2=This value is loaded from .env in project/ directory
|
||||||
|
ZZVAR3=This value is loaded from env-file-tests/.env
|
||||||
|
```
|
||||||
|
2
tests/env-file-tests/project/.env
Normal file
2
tests/env-file-tests/project/.env
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
ZZVAR1='This value is loaded but should be overwritten'
|
||||||
|
ZZVAR2='This value is loaded from .env in project/ directory'
|
@ -0,0 +1,11 @@
|
|||||||
|
services:
|
||||||
|
app:
|
||||||
|
image: busybox
|
||||||
|
command: ["/bin/busybox", "sh", "-c", "env | grep ZZ"]
|
||||||
|
tmpfs:
|
||||||
|
- /run
|
||||||
|
- /tmp
|
||||||
|
environment:
|
||||||
|
ZZVAR1: $ZZVAR1
|
||||||
|
ZZVAR2: $ZZVAR2
|
||||||
|
ZZVAR3: $ZZVAR3
|
Loading…
x
Reference in New Issue
Block a user