Add support to pass env-file to docker compose run
What I did
This pull request adds support to pass in --env-file to docker compose run command.
Behavior of the option:
If you pass env-file from CLI it overrides the env-files provided in docker-compose.yaml config.
Related issue #8379
(not mandatory) A picture of a cute animal, if possible in relation with what you did

command documentation also need to be updated to reflect this new flag: https://github.com/docker/compose/blob/v2/docs/reference/docker_compose_run.yaml
@ndeloof Looks like there is bug in the implementation. It isn't working as expected. I think when we parse the compose config files at that time only the env_file mentioned in it gets parsed. because when I see the service.Environment it was already populated with the key=value mentioned in the env_file. So my understanding that just replacing the service.EnvFile will work is not true.
I'll just put back this PR in draft as it is not ready for merge.
The main issue I see adding support for compose run --env-file ... to set container environment from a file (which would make sense to align with docker run ...) is that this will introduce confusion with the top-level --env-file used to override .env file used to interpolate variables. i.e.
docker compose --env-file xx run is a distinct thing vs docker compose run --env-file xx
I'm not sure how we could address this UX issue. Any thoughts @glours @ulyssessouza @thaJeztah ?
Same for me... The UX doesn't help... And specially because the new one is an array and the root command one is a single file.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Project coverage is 73.89%. Comparing base (
d47f0f3) to head (bfb8c20). Report is 1239 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #9169 +/- ##
=======================================
Coverage 73.89% 73.89%
=======================================
Files 2 2
Lines 272 272
=======================================
Hits 201 201
Misses 60 60
Partials 11 11
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
🚀 New features to boost your workflow:
- ❄ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
This issue has been automatically marked as not stale anymore due to the recent activity.
This issue has been automatically marked as not stale anymore due to the recent activity.
This issue has been automatically marked as not stale anymore due to the recent activity.
I cherry-picked your code into https://github.com/docker/compose/pull/12626