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

Add support to pass env-file to docker compose run #9169

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions cmd/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,28 @@ import (

type runOptions struct {
*composeOptions
Service string
Command []string
environment []string
Detach bool
Remove bool
noTty bool
tty bool
interactive bool
user string
workdir string
entrypoint string
entrypointCmd []string
labels []string
volumes []string
publish []string
useAliases bool
servicePorts bool
name string
noDeps bool
ignoreOrphans bool
quietPull bool
Service string
Command []string
environment []string
Detach bool
Remove bool
noTty bool
tty bool
interactive bool
user string
workdir string
entrypoint string
entrypointCmd []string
labels []string
volumes []string
publish []string
useAliases bool
servicePorts bool
name string
noDeps bool
ignoreOrphans bool
quietPull bool
ServiceEnvFiles []string
}

func (opts runOptions) apply(project *types.Project) error {
Expand Down Expand Up @@ -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")
flags.StringArrayVarP(&opts.labels, "label", "l", []string{}, "Add or override a label")
flags.BoolVar(&opts.Remove, "rm", false, "Automatically remove the container when it exits")
flags.BoolVarP(&opts.noTty, "no-TTY", "T", !streams.Out().IsTerminal(), "Disable pseudo-TTY allocation (default: auto-detected).")
Expand Down Expand Up @@ -233,6 +235,7 @@ func runRun(ctx context.Context, backend api.Service, project *types.Project, op
NoDeps: opts.noDeps,
Index: 0,
QuietPull: opts.quietPull,
ServiceEnvFiles: opts.ServiceEnvFiles,
}

for i, service := range project.Services {
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,8 @@ type RunOptions struct {
// QuietPull makes the pulling process quiet
QuietPull bool
// used by exec
Index int
Index int
ServiceEnvFiles []string
}

// EventsOptions group options of the Events API
Expand Down
4 changes: 4 additions & 0 deletions pkg/compose/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Copy link
Contributor

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

Copy link
Contributor Author

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?

Copy link
Contributor

@ndeloof ndeloof May 10, 2023

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)
}

}
}
58 changes: 58 additions & 0 deletions pkg/compose/run_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package compose

import (
"testing"

"github.com/compose-spec/compose-go/types"
"github.com/docker/compose/v2/pkg/api"
"gotest.tools/v3/assert"
)

func TestServiceEnvFiles(t *testing.T) {

t.Run("Verify service.EnvFile shouldn't modify", func(t *testing.T) {
fooService := types.ServiceConfig{
Name: "foo",
EnvFile: []string{},
}

project := types.Project{
Name: "test-project",
Services: types.Services{
fooService,
},
}

opts := api.RunOptions{
ServiceEnvFiles: nil,
}

applyRunOptions(&project, &fooService, opts)

assert.Assert(t, len(fooService.EnvFile) == 0)
})

t.Run("Verify appends ServiceEnvFiles", func(t *testing.T) {
fooService := &types.ServiceConfig{
Name: "foo",
EnvFile: []string{"./existing.env"},
}

project := types.Project{
Name: "test-project",
Services: types.Services{
*fooService,
},
}

opts := api.RunOptions{
ServiceEnvFiles: []string{"./file.env"},
}

applyRunOptions(&project, fooService, opts)

assert.Assert(t, len(fooService.EnvFile) == 2)
assert.Assert(t, fooService.EnvFile[0] == "./existing.env")
assert.Assert(t, fooService.EnvFile[1] == "./file.env")
})
}