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.
PHP coolifydatabasesdeploymentdockerdocker-composeinertiajslaravelmariadbmysqlnextjsnodejsphppostgresredisself-hostedself-hostingserverstatic-sitesveltesvelte5
Add Docker container management methods and update Livewire component
Andras Bacsai7d6bd10ccafb
diff --git a/app/Livewire/Server/Resources.php b/app/Livewire/Server/Resources.phpindex c3ef7f692f..99d988271f 100644--- a/app/Livewire/Server/Resources.php+++ b/app/Livewire/Server/Resources.php@@ -4,6 +4,7 @@ namespace App\Livewire\Server;use App\Models\Server;use Illuminate\Foundation\Auth\Access\AuthorizesRequests;+use Illuminate\Support\Collection;use Livewire\Component;class Resources extends Component@@ -11,6 +12,7 @@ class Resources extends Componentuse AuthorizesRequests;public ?Server $server = null;public $parameters = [];+ public Collection $unmanagedContainers;public function getListeners(){$teamId = auth()->user()->currentTeam()->id;@@ -19,11 +21,31 @@ class Resources extends Component];}+ public function startUnmanaged($id) {+ $this->server->startUnmanaged($id);+ $this->dispatch('success', 'Container started.');+ $this->loadUnmanagedContainers();+ }+ public function restartUnmanaged($id) {+ $this->server->restartUnmanaged($id);+ $this->dispatch('success', 'Container restarted.');+ $this->loadUnmanagedContainers();+ }+ public function stopUnmanaged($id) {+ $this->server->stopUnmanaged($id);+ $this->dispatch('success', 'Container stopped.');+ $this->loadUnmanagedContainers();+ }public function refreshStatus() {$this->server->refresh();+ $this->loadUnmanagedContainers();$this->dispatch('success', 'Resource statuses refreshed.');}+ public function loadUnmanagedContainers() {+ $this->unmanagedContainers = $this->server->loadUnmanagedContainers();+ }public function mount() {+ $this->unmanagedContainers = collect();$this->parameters = get_route_parameters();try {$this->server = Server::ownedByCurrentTeam()->whereUuid(request()->server_uuid)->first();@@ -33,6 +55,7 @@ class Resources extends Component} catch (\Throwable $e) {return handleError($e, $this);}+ $this->loadUnmanagedContainers();}public function render(){diff --git a/app/Models/Server.php b/app/Models/Server.phpindex ce34486edf..24799fe6ee 100644--- a/app/Models/Server.php+++ b/app/Models/Server.php@@ -225,6 +225,29 @@ class Server extends BaseModel$services = $this->services();return $applications->concat($databases)->concat($services->get());}+ public function stopUnmanaged($id) {+ return instant_remote_process(["docker stop -t 0 $id"], $this);+ }+ public function restartUnmanaged($id) {+ return instant_remote_process(["docker restart $id"], $this);+ }+ public function startUnmanaged($id) {+ return instant_remote_process(["docker start $id"], $this);+ }+ public function loadUnmanagedContainers()+ {+ $containers = instant_remote_process(["docker ps -a --format '{{json .}}' "], $this);+ $containers = format_docker_command_output_to_json($containers);+ $containers = $containers->map(function ($container) {+ $labels = data_get($container, 'Labels');+ if (!str($labels)->contains("coolify.managed")) {+ return $container;+ }+ return null;+ });+ $containers = $containers->filter();+ return collect($containers);+ }public function hasDefinedResources(){$applications = $this->applications()->count() > 0;@@ -246,7 +269,7 @@ class Server extends BaseModel$mariadbs = data_get($standaloneDocker, 'mariadbs', collect([]));return $postgresqls->concat($redis)->concat($mongodbs)->concat($mysqls)->concat($mariadbs);})->filter(function ($item) {- return data_get($item,'name') === 'coolify-db';+ return data_get($item, 'name') === 'coolify-db';})->flatten();}public function applications()diff --git a/resources/views/livewire/server/resources.blade.php b/resources/views/livewire/server/resources.blade.phpindex 964adc4b69..113ce4e632 100644--- a/resources/views/livewire/server/resources.blade.php+++ b/resources/views/livewire/server/resources.blade.php@@ -1,55 +1,143 @@<div><x-server.navbar :server="$server" :parameters="$parameters" />- <div class="flex flex-col">- <div class="flex gap-2">- <h2>Resources</h2>- <x-forms.button wire:click="refreshStatus">Refresh</x-forms.button>++ <div x-data="{ activeTab: window.location.hash ? window.location.hash.substring(1) : 'managed' }" class="flex h-full">+ <div class="flex flex-col gap-4 xl:w-32">+ <a :class="activeTab === 'managed' && 'text-white'"+ @click.prevent="activeTab = 'managed'; window.location.hash = 'managed'" href="#">Managed</a>+ <a :class="activeTab === 'unmanaged' && 'text-white'"+ @click.prevent="activeTab = 'unmanaged'; window.location.hash = 'unmanaged'" href="#">Unmanaged</a></div>- <div class="pb-4 title">Here you can find all resources for this server.</div>- </div>- <div class="flex flex-col">- <div class="flex flex-col">- <div class="overflow-x-auto">- <div class="inline-block min-w-full">- <div class="overflow-hidden">- <table class="min-w-full divide-y divide-coolgray-400">- <thead>- <tr class="text-neutral-500">- <th class="px-5 py-3 text-xs font-medium text-left uppercase">Project</th>- <th class="px-5 py-3 text-xs font-medium text-left uppercase">Environment</th>- <th class="px-5 py-3 text-xs font-medium text-left uppercase">Name</th>- <th class="px-5 py-3 text-xs font-medium text-left uppercase">Type</th>- <th class="px-5 py-3 text-xs font-medium text-left uppercase">Status</th>- </tr>- </thead>- <tbody class="divide-y divide-coolgray-400">- @forelse ($server->definedResources()->sortBy('name',SORT_NATURAL) as $resource)- <tr class="text-white bg-coolblack hover:bg-coolgray-100">- <td class="px-5 py-4 text-sm whitespace-nowrap">- {{ data_get($resource->project(), 'name') }}- </td>- <td class="px-5 py-4 text-sm whitespace-nowrap">- {{ data_get($resource, 'environment.name') }}- </td>- <td class="px-5 py-4 text-sm whitespace-nowrap"><a class=""- href="{{ $resource->link() }}">{{ $resource->name }} </a></td>- <td class="px-5 py-4 text-sm whitespace-nowrap">- {{ str($resource->type())->headline() }}</td>- <td class="px-5 py-4 text-sm font-medium whitespace-nowrap">- @if ($resource->type() === 'service')- <x-status.services :service="$resource" :showRefreshButton="false" />- @else- <x-status.index :status="$resource->status" :showRefreshButton="false" />- @endif- </td>- </tr>- @empty- @endforelse- </tbody>- </table>+ <div class="w-full pl-8">+ <div x-cloak x-show="activeTab === 'managed'" class="h-full">+ <div class="flex flex-col">+ <div class="flex gap-2">+ <h2>Resources</h2>+ <x-forms.button wire:click="refreshStatus">Refresh</x-forms.button>+ </div>+ <div class="pb-4 title">Here you can find all resources that are managed by Coolify.</div>+ </div>+ <div class="flex flex-col">+ <div class="flex flex-col">+ <div class="overflow-x-auto">+ <div class="inline-block min-w-full">+ <div class="overflow-hidden">+ <table class="min-w-full divide-y divide-coolgray-400">+ <thead>+ <tr class="text-neutral-500">+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Project+ </th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">+ Environment</th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Name</th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Type</th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Status+ </th>+ </tr>+ </thead>+ <tbody class="divide-y divide-coolgray-400">+ @forelse ($server->definedResources()->sortBy('name',SORT_NATURAL) as $resource)+ <tr class="text-white bg-coolblack hover:bg-coolgray-100">+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ {{ data_get($resource->project(), 'name') }}+ </td>+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ {{ data_get($resource, 'environment.name') }}+ </td>+ <td class="px-5 py-4 text-sm whitespace-nowrap"><a class=""+ href="{{ $resource->link() }}">{{ $resource->name }} </a>+ </td>+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ {{ str($resource->type())->headline() }}</td>+ <td class="px-5 py-4 text-sm font-medium whitespace-nowrap">+ @if ($resource->type() === 'service')+ <x-status.services :service="$resource" :showRefreshButton="false" />+ @else+ <x-status.index :status="$resource->status" :showRefreshButton="false" />+ @endif+ </td>+ </tr>+ @empty+ @endforelse+ </tbody>+ </table>+ </div>+ </div>+ </div></div></div></div>+ <div x-cloak x-show="activeTab === 'unmanaged'" class="h-full">+ <div class="flex flex-col">+ <div class="flex gap-2">+ <h2>Resources</h2>+ <x-forms.button wire:click="refreshStatus">Refresh</x-forms.button>+ </div>+ <div class="pb-4 title">Here you can find all other containers running on the server.</div>+ </div>+ @if ($unmanagedContainers->count() > 0)+ <div class="flex flex-col">+ <div class="flex flex-col">+ <div class="overflow-x-auto">+ <div class="inline-block min-w-full">+ <div class="overflow-hidden">+ <table class="min-w-full divide-y divide-coolgray-400">+ <thead>+ <tr class="text-neutral-500">+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Name+ </th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Image+ </th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Status+ </th>+ <th class="px-5 py-3 text-xs font-medium text-left uppercase">Action+ </th>+ </tr>+ </thead>+ <tbody class="divide-y divide-coolgray-400">+ @forelse ($unmanagedContainers->sortBy('name',SORT_NATURAL) as $resource)+ <tr class="text-white bg-coolblack hover:bg-coolgray-100">+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ {{ data_get($resource, 'Names') }}+ </td>+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ {{ data_get($resource, 'Image') }}+ </td>+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ {{ data_get($resource, 'State') }}+ </td>+ <td class="px-5 py-4 text-sm whitespace-nowrap">+ @if (data_get($resource, 'State') === 'running')+ <x-forms.button+ wire:click="restartUnmanaged('{{ data_get($resource, 'ID') }}')"+ wire:key="{{ data_get($resource, 'ID') }}">Restart</x-forms.button>+ <x-forms.button isError+ wire:click="stopUnmanaged('{{ data_get($resource, 'ID') }}')"+ wire:key="{{ data_get($resource, 'ID') }}">Stop</x-forms.button>+ @elseif (data_get($resource, 'State') === 'exited')+ <x-forms.button+ wire:click="startUnmanaged('{{ data_get($resource, 'ID') }}')"+ wire:key="{{ data_get($resource, 'ID') }}">Start</x-forms.button>+ @elseif (data_get($resource, 'State') === 'restarting')+ <x-forms.button+ wire:click="stopUnmanaged('{{ data_get($resource, 'ID') }}')"+ wire:key="{{ data_get($resource, 'ID') }}">Stop</x-forms.button>+ @endif+ </td>+ </tr>+ @empty+ @endforelse+ </tbody>+ </table>+ </div>+ </div>+ </div>+ </div>+ </div>+ @endif+ </div></div></div>++</div>