Skip to content

Commit

Permalink
feat: 컨테이너 생성 완료 후 기존 컨테이너 삭제하도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
flydog98 committed Dec 18, 2023
1 parent 604fa1f commit 55e1b38
Showing 1 changed file with 8 additions and 16 deletions.
24 changes: 8 additions & 16 deletions packages/backend/src/containers/containers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ export class ContainersService {
}

async initializeContainers() {
const { stdoutData: existingContainers } =
await this.commandService.executeCommand('docker ps -aq');
const existingContainersSet = new Set(
existingContainers.split('\n').filter((id) => id),
);

for (let i: number = 1; i < 20; i++) {
const maxContainers =
this.configService.get<number>('CONTAINER_POOL_MAX') || 1;
Expand All @@ -42,22 +48,8 @@ export class ContainersService {
this.availableContainers.set(i, containers);
}

this.removeUnusedContainers();
}

async removeUnusedContainers() {
const { stdoutData: allContainers } =
await this.commandService.executeCommand('docker ps -aq');

const usedContainers = new Set<string>();
this.availableContainers.forEach((containers) => {
containers.forEach((containerId) => usedContainers.add(containerId));
});

allContainers.split('\n').forEach((containerId) => {
if (!usedContainers.has(containerId)) {
this.commandService.executeCommand(`docker rm -f ${containerId}`);
}
existingContainersSet.forEach((containerId) => {
this.commandService.executeCommand(`docker rm -f ${containerId}`);
});
}

Expand Down

0 comments on commit 55e1b38

Please sign in to comment.