From b23b0f7c8db78d5f1a4a563e0cb0ce2fc05e9477 Mon Sep 17 00:00:00 2001 From: Oscar Zhou <100548325+oscarzhou-portainer@users.noreply.github.com> Date: Thu, 30 May 2024 08:46:48 +1200 Subject: [PATCH] fix(compose): add project directory option to compose command [EE-7093] (#11859) --- .../compose/internal/composeplugin/composeplugin.go | 8 ++++++++ pkg/libstack/libstack.go | 5 +++++ 2 files changed, 13 insertions(+) diff --git a/pkg/libstack/compose/internal/composeplugin/composeplugin.go b/pkg/libstack/compose/internal/composeplugin/composeplugin.go index ca98426c0..26d09d4a8 100644 --- a/pkg/libstack/compose/internal/composeplugin/composeplugin.go +++ b/pkg/libstack/compose/internal/composeplugin/composeplugin.go @@ -126,6 +126,10 @@ func (wrapper *PluginWrapper) command(command composeCommand, options libstack.O command.WithHost(options.Host) } + if options.ProjectDir != "" { + command.WithProjectDirectory(options.ProjectDir) + } + var stderr bytes.Buffer args := []string{} @@ -237,6 +241,10 @@ func (command *composeCommand) WithEnvFilePath(envFilePath string) { command.globalArgs = append(command.globalArgs, "--env-file", envFilePath) } +func (command *composeCommand) WithProjectDirectory(projectDir string) { + command.globalArgs = append(command.globalArgs, "--project-directory", projectDir) +} + func (command *composeCommand) ToArgs() []string { return append(command.globalArgs, command.subCommandAndArgs...) } diff --git a/pkg/libstack/libstack.go b/pkg/libstack/libstack.go index 9ff08012a..6d323cc87 100644 --- a/pkg/libstack/libstack.go +++ b/pkg/libstack/libstack.go @@ -35,6 +35,7 @@ type WaitResult struct { } type Options struct { + // WorkingDir is the working directory for the command execution WorkingDir string Host string ProjectName string @@ -42,6 +43,10 @@ type Options struct { EnvFilePath string // Env is a list of environment variables to pass to the command, example: "FOO=bar" Env []string + // ProjectDir is the working directory for containers created by docker compose file. + // By default, it is an empty string, which means it corresponds to the path of the compose file itself. + // This is particularly helpful when mounting a relative path. + ProjectDir string } type DeployOptions struct {