Repository navigation
Add --env-from-file option to run command - #1584
Open
Thanatos9404 wants to merge 1 commit into
Open
Thanatos9404 wants to merge 1 commit into
Thanatos9404 wants to merge 1 commit into
Conversation
`docker compose run` accepts `--env-from-file` to set environment variables of the one-off container from a file. podman-compose rejected the option as an unrecognized argument. The option can be repeated and follows docker compose: the files are read in order, a variable set by an earlier file is not overridden by a later one, a bare `KEY` line without a value is ignored, values given with `-e` take precedence over all files, and the result overrides the service's environment. The files are parsed with dotenv_to_dict(), the helper already used for `env_file`. Relative paths are resolved against the working directory when the arguments are parsed, because podman-compose may later change into the directory where the compose file was found. A missing file is reported in _parse_args(), before any pod or container is created. Fixes: containers#1239 Signed-off-by: Yashvardhan Thanvi <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
docker compose runhas a repeatable--env-from-fileoption that sets environment variables of the one-off container from a file (CLI reference). podman-compose rejected it as an unrecognized argument.Fixes: #1239
Behaviour
This follows docker compose's implementation,
getEnvironment()incmd/compose/run.go:--env-file, where later files win, but it is what docker compose does.KEYline without a value is ignored, as in docker compose.-etake precedence over all files.environment, where-eputs its values, so they override the service'senvironmentandenv_filethe same way-edoes.run --env-from-file=testing.envfrom a subdirectory would read thetesting.envnext to the compose file instead._parse_args(), next to theup --waitchecks, sorunstops withError: Couldn't find env file: <absolute path>and exit code 1 before any pod or container is created. The wording is the one the global--env-fileuses.The files are read with
dotenv_to_dict(), the python-dotenv helper already used forenv_file. One difference:${VAR}in the file is expanded from the file itself and the host environment, as forenv_file, while docker compose looks up the-evalues and the earlier files.--remove-orphans, which the command in the issue also uses, is a separate option and is not part of this PR.Tests
New tests in
tests/unit/test_compose_run_update_container_from_args.py, using the env files intests/integration/env_file_tests/env-files/:-eoverriding a value from a file;environment;KEYlines, which neither replace the service's value nor stop a later file from setting the variable.New
tests/unit/test_compose_run_args.py, through_parse_args()liketest_compose_up_args.py:PodmanComposeErrorwith its absolute path.On
mainthe 7 new tests fail or error: 5 failures, and 2 errors fromunrecognized arguments: --env-from-file.Results on Ubuntu 24.04 (WSL), Python 3.12.3, with
requirements.txtandtest-requirements.txtinstalled:python -m unittest discover tests/unit: 504 tests OK (497 onmain, also OK).ruff format --check,ruff check,mypy .andpylint podman_compose.py(10.00/10): pass.podman runcommand with--dry-run --verbose, with a dummypodmanonPATHthat exits 0 (dry run still callspodman network exists). With the compose file andtesting.envfrom the issue,podman-compose run --env-from-file=testing.env apppasses-e ZZVAR1=foo -e ZZVAR2=bar -e ZZVAR3=baz, the values docker compose prints in the issue.Checklist
feat:prefix)newsfragments/run_env_from_file.featurerunoptions are not part of the compose spec; the docker compose reference is linked above.