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.

fix: add proxy to network with periodic check

Andras Bacsaiebfc0bd1e1a9
diff --git a/app/Actions/Proxy/StartProxy.php b/app/Actions/Proxy/StartProxy.php
index edb84dd014..aafeb2f3e7 100644
--- a/app/Actions/Proxy/StartProxy.php
+++ b/app/Actions/Proxy/StartProxy.php
@@ -14,21 +14,12 @@ class StartProxy
use AsAction;
public function handle(Server $server, bool $async = true): Activity|string
{
+ $commands = collect([]);
$proxyType = $server->proxyType();
if ($proxyType === 'none') {
return 'OK';
}
$proxy_path = get_proxy_path();
- $networks = collect($server->standaloneDockers)->map(function ($docker) {
- return $docker['network'];
- })->unique();
- if ($networks->count() === 0) {
- $networks = collect(['coolify']);
- }
- $create_networks_command = $networks->map(function ($network) {
- return "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1";
- });
-
$configuration = CheckConfiguration::run($server);
if (!$configuration) {
throw new \Exception("Configuration is not synced");
@@ -36,13 +27,12 @@ class StartProxy
$docker_compose_yml_base64 = base64_encode($configuration);
$server->proxy->last_applied_settings = Str::of($docker_compose_yml_base64)->pipe('md5')->value;
$server->save();
- $commands = [
+
+ $commands = $commands->merge([
"command -v lsof >/dev/null || echo '####### Installing lsof...'",
"command -v lsof >/dev/null || apt-get update",
"command -v lsof >/dev/null || apt install -y lsof",
"command -v lsof >/dev/null || command -v fuser >/dev/null || apt install -y psmisc",
- "echo '####### Creating required Docker networks...'",
- ...$create_networks_command,
"cd $proxy_path",
"echo '####### Creating Docker Compose file...'",
"echo '####### Pulling docker image...'",
@@ -60,7 +50,8 @@ class StartProxy
"echo '####### Starting coolify-proxy...'",
'docker compose up -d --remove-orphans || docker-compose up -d --remove-orphans',
"echo '####### Proxy installed successfully...'"
- ];
+ ]);
+ $commands = $commands->merge(connectProxyToNetworks($server));
if (!$async) {
instant_remote_process($commands, $server);
return 'OK';
diff --git a/app/Jobs/ContainerStatusJob.php b/app/Jobs/ContainerStatusJob.php
index 6da47121b6..37fb197624 100644
--- a/app/Jobs/ContainerStatusJob.php
+++ b/app/Jobs/ContainerStatusJob.php
@@ -89,11 +89,14 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted
} else {
$this->server->proxy->status = data_get($foundProxyContainer, 'State.Status');
$this->server->save();
+ $connectProxyToDockerNetworks = connectProxyToNetworks($this->server);
+ instant_remote_process([$connectProxyToDockerNetworks], $this->server, false);
}
$foundApplications = [];
$foundApplicationPreviews = [];
$foundDatabases = [];
$foundServices = [];
+
foreach ($containers as $container) {
$containerStatus = data_get($container, 'State.Status');
$labels = data_get($container, 'Config.Labels');
@@ -179,7 +182,7 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted
$db->update(['status' => 'exited']);
}
}
- }
+ }
$exitedServices = $exitedServices->unique('id');
ray($exitedServices);
// ray($exitedServices);
@@ -202,7 +205,6 @@ class ContainerStatusJob implements ShouldQueue, ShouldBeEncrypted
// $this->server->team->notify(new ContainerStopped($containerName, $this->server, $url));
// }
-
$notRunningApplications = $applications->pluck('id')->diff($foundApplications);
foreach ($notRunningApplications as $applicationId) {
$application = $applications->where('id', $applicationId)->first();
diff --git a/bootstrap/helpers/proxy.php b/bootstrap/helpers/proxy.php
index e3db2be36f..661da1c520 100644
--- a/bootstrap/helpers/proxy.php
+++ b/bootstrap/helpers/proxy.php
@@ -10,7 +10,23 @@ function get_proxy_path()
$proxy_path = "$base_path/proxy";
return $proxy_path;
}
+function connectProxyToNetworks(Server $server) {
+ $networks = collect($server->standaloneDockers)->map(function ($docker) {
+ return $docker['network'];
+ })->unique();
+ if ($networks->count() === 0) {
+ $networks = collect(['coolify']);
+ }
+ $commands = $networks->map(function ($network) {
+ return [
+ "echo '####### Connecting coolify-proxy to $network network...'",
+ "docker network ls --format '{{.Name}}' | grep '^$network$' >/dev/null 2>&1 || docker network create --attachable $network > /dev/null 2>&1",
+ "docker network connect $network coolify-proxy >/dev/null 2>&1 || true",
+ ];
+ });
+ return $commands->flatten();
+}
function generate_default_proxy_configuration(Server $server)
{
$proxy_path = get_proxy_path();
diff --git a/bootstrap/helpers/remoteProcess.php b/bootstrap/helpers/remoteProcess.php
index 7936ca62bf..706be1e1bd 100644
--- a/bootstrap/helpers/remoteProcess.php
+++ b/bootstrap/helpers/remoteProcess.php
@@ -16,7 +16,7 @@ use Illuminate\Support\Str;
use Spatie\Activitylog\Contracts\Activity;
function remote_process(
- array $command,
+ Collection|array $command,
Server $server,
?string $type = null,
?string $type_uuid = null,
@@ -26,6 +26,9 @@ function remote_process(
if (is_null($type)) {
$type = ActivityTypes::INLINE->value;
}
+ if ($command instanceof Collection) {
+ $command = $command->toArray();
+ }
$command_string = implode("\n", $command);
if (auth()->user()) {
$teams = auth()->user()->teams->pluck('id');
@@ -98,8 +101,11 @@ function generateSshCommand(Server $server, string $command, bool $isMux = true)
// ray($ssh_command);
return $ssh_command;
}
-function instant_remote_process(array $command, Server $server, $throwError = true)
+function instant_remote_process(Collection|array $command, Server $server, $throwError = true)
{
+ if ($command instanceof Collection) {
+ $command = $command->toArray();
+ }
$command_string = implode("\n", $command);
$ssh_command = generateSshCommand($server, $command_string);
$process = Process::run($ssh_command);