-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Add support to pass env-file to docker compose run #9169
Add support to pass env-file to docker compose run #9169
Conversation
25c370a
to
5a1f2bd
Compare
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 I'll just put back this PR in draft as it is not ready for merge. |
The main issue I see adding support for |
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. |
cmd/compose/run.go
Outdated
@@ -141,6 +142,7 @@ func runCommand(p *projectOptions, backend api.Service) *cobra.Command { | |||
flags := cmd.Flags() | |||
flags.BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID") | |||
flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables") | |||
flags.StringArrayVar(&opts.EnvFiles, "env-file", []string{}, "Read in a file of environment variables") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we use --service-env-file
to avoid ambiguity with upper-level compose command --env-file flag
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we want to retain a parallel between docker run
and docker compose run
, then I'm wondering if we should look at doing the reverse, and think of a new name for the flag for setting the .env
(we could keep the old flag, but hidden)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
renamed the option to service-env-file
a877dc5
to
5b7b2a3
Compare
Signed-off-by: Vedant Koditkar <vedant.koditkar@outlook.com>
Signed-off-by: Vedant Koditkar <vedant.koditkar@outlook.com>
5b7b2a3
to
bfb8c20
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but path to service env file passed by command line flag could be relative to working directory
@@ -127,4 +127,8 @@ func applyRunOptions(project *types.Project, service *types.ServiceConfig, opts | |||
for k, v := range opts.Labels { | |||
service.Labels = service.Labels.Add(k, v) | |||
} | |||
|
|||
if opts.ServiceEnvFiles != nil { | |||
service.EnvFile = append(service.EnvFile, opts.ServiceEnvFiles...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
need to make service envfile an absolute path
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any sample code which I can refer in the repository?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can just use:
if !filepath.IsAbs(file) {
file, err = filepath.Abs(file)
}
Codecov ReportAll modified and coverable lines are covered by tests ✅
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 ☔ View full report in Codecov by Sentry. |
@@ -156,6 +157,7 @@ func runCommand(p *ProjectOptions, streams api.Streams, backend api.Service) *co | |||
flags := cmd.Flags() | |||
flags.BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID") | |||
flags.StringArrayVarP(&opts.environment, "env", "e", []string{}, "Set environment variables") | |||
flags.StringArrayVar(&opts.ServiceEnvFiles, "service-env-file", []string{}, "Read in a file of environment variables") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: rename flag --env-from-file
, wdyt ?
What I did
This pull request adds support to pass in
--env-file
todocker 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