diff --git a/packages/renderer/src/lib/container/container-utils.spec.ts b/packages/renderer/src/lib/container/container-utils.spec.ts index 6cdae35b4de..dd4213fc745 100644 --- a/packages/renderer/src/lib/container/container-utils.spec.ts +++ b/packages/renderer/src/lib/container/container-utils.spec.ts @@ -277,6 +277,37 @@ test('check parsing of container info without names', async () => { expect(name).toBe(''); }); +test('check that if a container is part of compose, it will use the Names field for output WITHOUT the project name even if there is a name for service', async () => { + const containerInfo = { + Id: 'container1', + Image: 'registry.k8s.io/pause:3.7', + Labels: { + 'com.docker.compose.project': 'compose', + 'com.docker.compose.service': 'compose_container', + }, + Names: ['/compose-compose_container-1'], + State: 'RUNNING', + } as unknown as ContainerInfo; + const name = containerUtils.getName(containerInfo); + expect(name).toBe('compose_container-1'); +}); + +test('test that if a container is part of compose, and that container_name has been specified, that means that the Names[0] should be used without the project name', async () => { + const containerInfo = { + Id: 'container1', + Image: 'registry.k8s.io/pause:3.7', + Labels: { + 'com.docker.compose.project': 'compose', + 'com.docker.compose.service': 'container_name', + }, + // If container_name was specified in the compose file, this should be used (without the project name). + Names: ['/container_name'], + State: 'RUNNING', + } as unknown as ContainerInfo; + const name = containerUtils.getName(containerInfo); + expect(name).toBe('container_name'); +}); + test('check parsing of container info without labels', async () => { const context = new ContextUI(); const containerInfo = { diff --git a/packages/renderer/src/lib/container/container-utils.ts b/packages/renderer/src/lib/container/container-utils.ts index 8cdc8bc531e..3fa29597af7 100644 --- a/packages/renderer/src/lib/container/container-utils.ts +++ b/packages/renderer/src/lib/container/container-utils.ts @@ -32,17 +32,25 @@ import { ContainerGroupInfoTypeUI } from './ContainerInfoUI'; export class ContainerUtils { getName(containerInfo: ContainerInfo) { - // part of a compose ? - const composeService = containerInfo.Labels?.['com.docker.compose.service']; - if (composeService) { - const composeContainerNumber = containerInfo.Labels?.['com.docker.compose.container-number']; - if (composeContainerNumber) { - return `${composeService}-${composeContainerNumber}`; - } - } + // If the container has no name, return an empty string. if (containerInfo.Names.length === 0) { return ''; } + + // Safely determine if this is a compose project or not by checking the project label. + const composeProject = containerInfo.Labels?.['com.docker.compose.project']; + + /* + When deploying with compose, the container name will be -- under Names[0]. + This is added to the container name to make it unique. + HOWEVER, if you specify container_name in the compose file, the container name will be whatever is is set to and + will not have either the project or service number. + Thus the easier way to show the correct name is to get the containerInfo.Labels?.['com.docker.compose.project'] label + remove it from the Names[0] and return the result. + */ + if (composeProject) { + return containerInfo.Names[0].replace(/^\//, '').replace(`${composeProject}-`, ''); + } return containerInfo.Names[0].replace(/^\//, ''); }