Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

Add cleanup of stucked helper containers on servers

Andras Bacsai91e3d33c0b33
diff --git a/app/Console/Commands/Init.php b/app/Console/Commands/Init.php
index e0081d0d58..1bb4aa055c 100644
--- a/app/Console/Commands/Init.php
+++ b/app/Console/Commands/Init.php
@@ -3,9 +3,11 @@
namespace App\Console\Commands;
use App\Enums\ApplicationDeploymentStatus;
+use App\Jobs\CleanupHelperContainersJob;
use App\Models\Application;
use App\Models\ApplicationDeploymentQueue;
use App\Models\InstanceSettings;
+use App\Models\Server;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\ServiceDatabase;
@@ -32,6 +34,16 @@ class Init extends Command
$this->cleanup_ssh();
}
$this->cleanup_in_progress_application_deployments();
+ $this->cleanup_stucked_helper_containers();
+ }
+ private function cleanup_stucked_helper_containers() {
+ $servers = Server::all();
+ foreach ($servers as $server) {
+ if ($server->isFunctional()) {
+ CleanupHelperContainersJob::dispatch($server);
+ }
+ }
+
}
private function alive()
{
diff --git a/app/Jobs/CleanupHelperContainersJob.php b/app/Jobs/CleanupHelperContainersJob.php
new file mode 100644
index 0000000000..5c26ca9304
--- /dev/null
+++ b/app/Jobs/CleanupHelperContainersJob.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace App\Jobs;
+
+use App\Models\Server;
+use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeEncrypted;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Bus\Dispatchable;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
+
+class CleanupHelperContainersJob implements ShouldQueue, ShouldBeUnique, ShouldBeEncrypted
+{
+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+
+ public function __construct(public Server $server)
+ {
+ }
+
+ public function handle(): void
+ {
+ try {
+ ray('Cleaning up helper containers on ' . $this->server->name);
+ $containers = instant_remote_process(['docker container ps --filter "ancestor=ghcr.io/coollabsio/coolify-helper:next" --filter "ancestor=ghcr.io/coollabsio/coolify-helper:latest" --format \'{{json .}}\''], $this->server, false);
+ $containers = format_docker_command_output_to_json($containers);
+ if ($containers->count() > 0) {
+ foreach ($containers as $container) {
+ $containerId = data_get($container,'ID');
+ ray('Removing container ' . $containerId);
+ instant_remote_process(['docker container rm -f ' . $containerId], $this->server, false);
+ }
+ }
+ } catch (\Throwable $e) {
+ send_internal_notification('CleanupHelperContainersJob failed with error: ' . $e->getMessage());
+ ray($e->getMessage());
+ }
+ }
+}
diff --git a/bootstrap/helpers/docker.php b/bootstrap/helpers/docker.php
index 42ea0f9bc7..36f8733b3e 100644
--- a/bootstrap/helpers/docker.php
+++ b/bootstrap/helpers/docker.php
@@ -10,7 +10,6 @@ use Visus\Cuid2\Cuid2;
function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pullRequestId = null): Collection
{
- ray($id, $pullRequestId);
$containers = collect([]);
$containers = instant_remote_process(["docker ps -a --filter='label=coolify.applicationId={$id}' --format '{{json .}}' "], $server);
$containers = format_docker_command_output_to_json($containers);
@@ -26,7 +25,6 @@ function getCurrentApplicationContainerStatus(Server $server, int $id, ?int $pul
return null;
});
$containers = $containers->filter();
- ray($containers);
return $containers;
}