From 5558bd15c26b668993eb845c4034b1b53dec2c12 Mon Sep 17 00:00:00 2001 From: flydog98 Date: Mon, 18 Dec 2023 23:11:25 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EC=8B=9C=EC=9E=91=20=EC=8B=9C=20?= =?UTF-8?q?=EC=BB=A8=ED=85=8C=EC=9D=B4=EB=84=88=20=EC=82=AD=EC=A0=9C=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=B3=B4=EC=99=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit [#300] --- .../src/containers/containers.service.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/containers/containers.service.ts b/packages/backend/src/containers/containers.service.ts index cc1c9e3..4455d27 100644 --- a/packages/backend/src/containers/containers.service.ts +++ b/packages/backend/src/containers/containers.service.ts @@ -24,8 +24,8 @@ export class ContainersService { private commandService: CommandService, ) { if (this.configService.get('SERVER_MODE') !== 'dev') { - this.commandService.executeCommand('docker rm -f $(docker ps -a -q)'); this.initializeContainers(); + this.removeUnusedContainers(); } } @@ -44,6 +44,22 @@ export class ContainersService { } } + async removeUnusedContainers() { + const { stdoutData: allContainers } = + await this.commandService.executeCommand('docker ps -aq'); + + const usedContainers = new Set(); + 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}`); + } + }); + } + private getGitCommand(container: string, command: string): string { return `${DOCKER_QUIZZER_COMMAND} ${container} ${command}`; }