diff --git a/app/Actions/Service/StartService.php b/app/Actions/Service/StartService.php index 10aa885bf..25a0614d9 100644 --- a/app/Actions/Service/StartService.php +++ b/app/Actions/Service/StartService.php @@ -10,6 +10,7 @@ class StartService use AsAction; public function handle(Service $service) { + $network = $service->destination->network; $service->saveComposeConfigs(); $commands[] = "cd " . $service->workdir(); $commands[] = "echo '####### Saved configuration files to {$service->workdir()}.'"; @@ -21,6 +22,7 @@ public function handle(Service $service) $commands[] = "echo '####### Starting containers.'"; $commands[] = "docker compose up -d --remove-orphans --force-recreate"; $commands[] = "docker network connect $service->uuid coolify-proxy 2>/dev/null || true"; + $commands[] = "docker network connect $network --alias $service->name-$service->uuid $service->name-$service->uuid 2>/dev/null || true"; $activity = remote_process($commands, $service->server); return $activity; } diff --git a/app/Models/Service.php b/app/Models/Service.php index 05be5fef4..c370e62b1 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -63,6 +63,10 @@ public function databases() { return $this->hasMany(ServiceDatabase::class); } + public function destination() + { + return $this->morphTo(); + } public function environment() { return $this->belongsTo(Environment::class); @@ -124,9 +128,16 @@ public function parse(bool $isNew = false): Collection $topLevelNetworks = collect(data_get($yaml, 'networks', [])); $dockerComposeVersion = data_get($yaml, 'version') ?? '3.8'; $services = data_get($yaml, 'services'); - $definedNetwork = $this->uuid; $generatedServiceFQDNS = collect([]); + if (is_null($this->destination)) { + $destination = $this->server->destinations()->first(); + if ($destination) { + $this->destination()->associate($destination); + $this->save(); + } + } + $definedNetwork = collect([$this->uuid]); $services = collect($services)->map(function ($service, $serviceName) use ($topLevelVolumes, $topLevelNetworks, $definedNetwork, $isNew, $generatedServiceFQDNS) { $serviceVolumes = collect(data_get($service, 'volumes', [])); @@ -237,13 +248,19 @@ public function parse(bool $isNew = false): Collection return $value == $definedNetwork; }); if (!$definedNetworkExists) { - $topLevelNetworks->put($definedNetwork, [ - 'name' => $definedNetwork, - 'external' => true - ]); + foreach ($definedNetwork as $network) { + $topLevelNetworks->put($network, [ + 'name' => $network, + 'external' => true + ]); + } } $networks = $serviceNetworks->toArray(); - $networks = array_merge($networks, [$definedNetwork]); + foreach ($definedNetwork as $key => $network) { + $networks = array_merge($networks, [ + $network => null + ]); + } data_set($service, 'networks', $networks); // Collect/create/update volumes @@ -308,7 +325,7 @@ public function parse(bool $isNew = false): Collection $target = Str::of($volume)->after(':')->beforeLast(':'); $source = $name; $volume = "$source:$target"; - } else if(is_array($volume)) { + } else if (is_array($volume)) { data_set($volume, 'source', $name); } $topLevelVolumes->put($name, null); diff --git a/database/migrations/2023_09_23_111816_add_destination_to_services_table.php b/database/migrations/2023_09_23_111816_add_destination_to_services_table.php new file mode 100644 index 000000000..2ec71bfdb --- /dev/null +++ b/database/migrations/2023_09_23_111816_add_destination_to_services_table.php @@ -0,0 +1,28 @@ +nullableMorphs('destination'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('services', function (Blueprint $table) { + $table->dropMorphs('destination'); + }); + } +};