From 86c925fbd3f754c1c7de882a5fd154d24d0e6a05 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Fri, 12 Aug 2022 20:13:34 +0200 Subject: [PATCH 1/2] Reset the DockerCli and APIClient after loading the environment file This forces a re-evaluation of the environment variables. Signed-off-by: Ulysses Souza --- cmd/compose/compose.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 10d110884..21c5695a8 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -32,6 +32,8 @@ import ( dockercli "github.com/docker/cli/cli" "github.com/docker/cli/cli-plugins/manager" "github.com/docker/cli/cli/command" + "github.com/docker/cli/cli/flags" + "github.com/docker/docker/client" "github.com/morikuni/aec" "github.com/pkg/errors" "github.com/sirupsen/logrus" @@ -291,6 +293,18 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { if err != nil { return err } + + // Reset DockerCli and APIClient to get possible `DOCKER_HOST` and/or `DOCKER_CONTEXT` loaded from environment file + err = dockerCli.Apply(func(cli *command.DockerCli) error { + return cli.Initialize(flags.NewClientOptions(), + command.WithInitializeClient(func(_ *command.DockerCli) (client.APIClient, error) { + return nil, nil + })) + }) + if err != nil { + return err + } + parent := cmd.Root() if parent != nil { parentPrerun := parent.PersistentPreRunE From 0b4cb85c8406fba91cc11743239d4256abda2b42 Mon Sep 17 00:00:00 2001 From: Ulysses Souza Date: Fri, 12 Aug 2022 20:16:00 +0200 Subject: [PATCH 2/2] Code formatting Just moving it up to make clearer Signed-off-by: Ulysses Souza --- cmd/compose/compose.go | 2 +- pkg/api/proxy.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/compose/compose.go b/cmd/compose/compose.go index 21c5695a8..bad59078c 100644 --- a/cmd/compose/compose.go +++ b/cmd/compose/compose.go @@ -294,7 +294,7 @@ func RootCommand(dockerCli command.Cli, backend api.Service) *cobra.Command { return err } - // Reset DockerCli and APIClient to get possible `DOCKER_HOST` and/or `DOCKER_CONTEXT` loaded from environment file + // Reset DockerCli and APIClient to get possible `DOCKER_HOST` and/or `DOCKER_CONTEXT` loaded from environment file. err = dockerCli.Apply(func(cli *command.DockerCli) error { return cli.Initialize(flags.NewClientOptions(), command.WithInitializeClient(func(_ *command.DockerCli) (client.APIClient, error) { diff --git a/pkg/api/proxy.go b/pkg/api/proxy.go index 5dcfa0e7f..d023714bf 100644 --- a/pkg/api/proxy.go +++ b/pkg/api/proxy.go @@ -22,6 +22,8 @@ import ( "github.com/compose-spec/compose-go/types" ) +var _ Service = &ServiceProxy{} + // ServiceProxy implements Service by delegating to implementation functions. This allows lazy init and per-method overrides type ServiceProxy struct { BuildFn func(ctx context.Context, project *types.Project, options BuildOptions) error @@ -59,8 +61,6 @@ func NewServiceProxy() *ServiceProxy { // Interceptor allow to customize the compose types.Project before the actual Service method is executed type Interceptor func(ctx context.Context, project *types.Project) -var _ Service = &ServiceProxy{} - // WithService configure proxy to use specified Service as delegate func (s *ServiceProxy) WithService(service Service) *ServiceProxy { s.BuildFn = service.Build