diff --git a/src/commands/containers/composeGroup.ts b/src/commands/containers/composeGroup.ts index 7947979dcb..a50d848848 100644 --- a/src/commands/containers/composeGroup.ts +++ b/src/commands/containers/composeGroup.ts @@ -27,13 +27,16 @@ export async function composeGroupDown(context: IActionContext, node: ContainerG async function composeGroup(context: IActionContext, composeCommand: 'logs' | 'restart' | 'down', node: ContainerGroupTreeItem, additionalArguments?: string): Promise { const workingDirectory = getComposeWorkingDirectory(node); const filesArgument = getComposeFiles(node)?.map(f => isWindows() ? `-f "${f}"` : `-f '${f}'`)?.join(' '); + const projectName = getProjectName(node); - if (!workingDirectory || !filesArgument) { + if (!workingDirectory || !filesArgument || !projectName) { context.errorHandling.suppressReportIssue = true; throw new Error(localize('vscode-docker.commands.containers.composeGroup.noCompose', 'Unable to determine compose project info for container group \'{0}\'.', node.label)); } - const terminalCommand = `docker-compose ${filesArgument} ${composeCommand} ${additionalArguments || ''}`; + const projectNameArgument = isWindows() ? `-p "${projectName}"` : `-p '${projectName}'`; + + const terminalCommand = `docker-compose ${filesArgument} ${projectNameArgument} ${composeCommand} ${additionalArguments || ''}`; await executeAsTask(context, await rewriteComposeCommandIfNeeded(terminalCommand), 'Docker Compose', { addDockerEnv: true, cwd: workingDirectory, }); } @@ -52,3 +55,9 @@ function getComposeFiles(node: ContainerGroupTreeItem): string[] | undefined { // (In short, the working dir may not be the same as the cwd when the docker-compose up command was called, BUT the files are relative to that cwd) return container?.labels?.['com.docker.compose.project.config_files']?.split(',')?.map(f => path.parse(f).base); } + +function getProjectName(node: ContainerGroupTreeItem): string | undefined { + // Find a container with the `com.docker.compose.project` label, which gives the project name + const container = (node.ChildTreeItems as ContainerTreeItem[]).find(c => c.labels?.['com.docker.compose.project']); + return container?.labels?.['com.docker.compose.project']; +}