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 bd4675f commit 5558bd1
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion packages/backend/src/containers/containers.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export class ContainersService {
private commandService: CommandService,
) {
if (this.configService.get<string>('SERVER_MODE') !== 'dev') {
this.commandService.executeCommand('docker rm -f $(docker ps -a -q)');
this.initializeContainers();
this.removeUnusedContainers();
}
}

Expand All @@ -44,6 +44,22 @@ export class ContainersService {
}
}

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}`);
}
});
}

private getGitCommand(container: string, command: string): string {
return `${DOCKER_QUIZZER_COMMAND} ${container} ${command}`;
}
Expand Down

0 comments on commit 5558bd1

Please sign in to comment.