Skip to content

Commit

Permalink
Detect running containers that have no environments associated
Browse files Browse the repository at this point in the history
  • Loading branch information
Luc45 committed Mar 6, 2024
1 parent c5eabfd commit 4232882
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/src/Environment/EnvironmentDanglingCleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function __construct(
public function cleanup_dangling(): void {
$this->remove_dangling_environments();
$this->detect_dangling_containers_exited();
$this->detect_dangling_containers_running();
$this->detect_dangling_networks();
$this->detect_dangling_directories();

Expand Down Expand Up @@ -206,6 +207,47 @@ protected function detect_dangling_containers_exited(): void {
}
}

/**
* - Detect running containers that have no environments associated.
*/
protected function detect_dangling_containers_running(): void {
$running_environments = $this->environment_monitor->get();

// List the running containers.
$list_process = new Process( [ 'docker', 'container', 'ls', '--format=json', '--filter=status=running', '--filter=name=qit_env_' ] );
$list_process->run();
$containers_output = $list_process->getOutput();

$lines = explode( "\n", $containers_output );

$running_containers = [];

foreach ( $lines as $line ) {
$c = json_decode( $line, true );
if ( $c === null ) {
continue;
}
if ( empty( $c['Names'] ) ) {
continue;
}
$container_name = $c['Names'];

if ( substr( $container_name, 0, strlen( 'qit_env_' ) ) === 'qit_env_' ) {
$running_containers[] = $container_name;
}
}

foreach ( $running_containers as $container_name ) {
foreach ( $running_environments as $env_info ) {
if ( strpos( $container_name, $env_info->env_id ) !== false ) {
continue 2;
}
}

$this->dangling_containers[] = 'qit_env_' . $container_name;
}
}

/**
* - Checks that all directories in the temp envs directory are in use by a running environment.
* - Mark the directory as orphaned if it's not in use by a running environment.
Expand Down

0 comments on commit 4232882

Please sign in to comment.