Skip to content

Commit 69dc74a

Browse files
committed
fix: add container observability for docker-compose in stop/down commands
- List containers before stopping/removing in docker-compose projects - Return actual container IDs in removedContainers/stoppedContainers arrays - Report correct containersFound count for docker-compose configurations - Ensures proper visibility of which containers are affected by operations
1 parent 089bb18 commit 69dc74a

File tree

1 file changed

+44
-11
lines changed

1 file changed

+44
-11
lines changed

src/spec-node/devContainersSpecCLI.ts

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,15 +1627,44 @@ async function stopContainers(params: DockerCLIParameters, options: { all?: bool
16271627
labels.push(`devcontainer.local_folder=${resolvedWorkspaceFolder}`);
16281628
containerIds = await listContainers(params, false, labels);
16291629

1630-
// If no containers found by local folder, try by config file
1631-
if (containerIds.length === 0) {
1632-
const configPath = configFile ? URI.file(path.resolve(process.cwd(), configFile)) : await getDevContainerConfigPathIn(cliHost, workspace.configFolderPath);
1633-
if (configPath) {
1634-
const configFilePath = uriToFsPath(configPath, cliHost.platform);
1635-
labels.length = 0; // Clear labels
1636-
if (idLabel) labels.push(idLabel);
1637-
labels.push(`devcontainer.config_file=${configFilePath}`);
1638-
containerIds = await listContainers(params, false, labels);
1630+
// Check if it's a Docker Compose configuration
1631+
const configPath = configFile ? URI.file(path.resolve(process.cwd(), configFile)) : await getDevContainerConfigPathIn(cliHost, workspace.configFolderPath);
1632+
if (configPath) {
1633+
const configs = await readDevContainerConfigFile(cliHost, workspace, configPath, true, output);
1634+
if (configs) {
1635+
const { config } = configs;
1636+
if ('dockerComposeFile' in config.config) {
1637+
// Handle Docker Compose stop
1638+
const composeFiles = await getDockerComposeFilePaths(cliHost, config.config, cliHost.env, cliHost.cwd);
1639+
const cwdEnvFile = cliHost.path.join(cliHost.cwd, '.env');
1640+
const envFile = Array.isArray(config.config.dockerComposeFile) && config.config.dockerComposeFile.length === 0 && await cliHost.isFile(cwdEnvFile) ? cwdEnvFile : undefined;
1641+
const composeConfig = await readDockerComposeConfig(params, composeFiles, envFile);
1642+
const projectName = await getProjectName(params, workspace, composeFiles, composeConfig);
1643+
1644+
// List containers before stopping them for observability
1645+
const projectLabel = `com.docker.compose.project=${projectName}`;
1646+
const projectContainers = await listContainers(params, false, [projectLabel]);
1647+
1648+
const text = `Running docker-compose stop for project ${projectName}...`;
1649+
const start = output.start(text);
1650+
await dockerComposeCLICommand(params, ...composeFiles.map(f => ['-f', f]).flat(), '-p', projectName, 'stop');
1651+
output.stop(text, start);
1652+
1653+
return {
1654+
outcome: 'success',
1655+
message: undefined,
1656+
stoppedContainers: projectContainers,
1657+
containersFound: projectContainers.length,
1658+
dockerComposeProject: projectName,
1659+
};
1660+
} else if (containerIds.length === 0) {
1661+
// If no containers found by local folder and it's not compose, try by config file
1662+
const configFilePath = uriToFsPath(configPath, cliHost.platform);
1663+
labels.length = 0; // Clear labels
1664+
if (idLabel) labels.push(idLabel);
1665+
labels.push(`devcontainer.config_file=${configFilePath}`);
1666+
containerIds = await listContainers(params, false, labels);
1667+
}
16391668
}
16401669
}
16411670
} else if (idLabel) {
@@ -1710,6 +1739,10 @@ async function downContainers(params: DockerCLIParameters, options: { all?: bool
17101739
const composeConfig = await readDockerComposeConfig(params, composeFiles, envFile);
17111740
const projectName = await getProjectName(params, workspace, composeFiles, composeConfig);
17121741

1742+
// List containers before removing them for observability
1743+
const projectLabel = `com.docker.compose.project=${projectName}`;
1744+
const projectContainers = await listContainers(params, true, [projectLabel]);
1745+
17131746
const text = `Running docker-compose down for project ${projectName}...`;
17141747
const start = output.start(text);
17151748
const args = ['down'];
@@ -1722,8 +1755,8 @@ async function downContainers(params: DockerCLIParameters, options: { all?: bool
17221755
return {
17231756
outcome: 'success',
17241757
message: undefined,
1725-
removedContainers: [],
1726-
containersFound: 0,
1758+
removedContainers: projectContainers,
1759+
containersFound: projectContainers.length,
17271760
dockerComposeProject: projectName,
17281761
};
17291762
} else if (containerIds.length === 0) {

0 commit comments

Comments
 (0)