fix(proxy): set Traefik network labels for compose services

Add the Coolify network label when no user-selected Traefik network exists, while preserving custom labels for applications and services.
This commit is contained in:
Andras Bacsai
2026-08-15 18:31:32 +02:00
parent 006b1b2b74
commit 1c25b58dc8
4 changed files with 154 additions and 0 deletions
+19
View File
@@ -358,6 +358,19 @@ function parseDockerVolumeString(string $volumeString): array
];
}
function addTraefikDockerNetworkLabel(Collection $labels, string $network): Collection
{
$hasUserDefinedNetwork = $labels->contains(
fn ($label): bool => is_string($label) && str($label)->before('=')->is('traefik.docker.network')
);
if (! $hasUserDefinedNetwork) {
$labels->push("traefik.docker.network={$network}");
}
return $labels;
}
function applicationParser(Application $resource, int $pull_request_id = 0, ?int $preview_id = null, ?string $commit = null): Collection
{
$uuid = data_get($resource, 'uuid');
@@ -1346,6 +1359,9 @@ function applicationParser(Application $resource, int $pull_request_id = 0, ?int
$redirectDirection = in_array($composeRedirect, ['www', 'non-www', 'both'], true)
? $composeRedirect
: 'both';
if (! $use_network_mode && (! $shouldGenerateLabelsExactly || $server->proxyType() === ProxyTypes::TRAEFIK->value)) {
$serviceLabels = addTraefikDockerNetworkLabel($serviceLabels, $baseNetwork->first());
}
if ($shouldGenerateLabelsExactly) {
switch ($server->proxyType()) {
case ProxyTypes::TRAEFIK->value:
@@ -2620,6 +2636,9 @@ function serviceParser(Service $resource): Collection
$redirectDirection = in_array(data_get($originalResource, 'redirect'), ['www', 'non-www', 'both'], true)
? data_get($originalResource, 'redirect')
: 'both';
if (! $use_network_mode && (! $shouldGenerateLabelsExactly || $server->proxyType() === ProxyTypes::TRAEFIK->value)) {
$serviceLabels = addTraefikDockerNetworkLabel($serviceLabels, $baseNetwork->first());
}
if ($shouldGenerateLabelsExactly) {
switch ($server->proxyType()) {
case ProxyTypes::TRAEFIK->value:
@@ -398,3 +398,59 @@ YAML;
expect(json_decode($plainApplication->docker_compose_domains, true))->toBeNull();
});
test('applicationParser selects the Coolify network for Traefik routed compose services', function () {
$application = Application::factory()->create([
'environment_id' => $this->environment->id,
'destination_id' => $this->destination->id,
'destination_type' => StandaloneDocker::class,
'build_pack' => 'dockercompose',
'docker_compose_raw' => <<<'YAML'
services:
frontend:
image: nginx:latest
networks:
- custom-network
networks:
custom-network: {}
YAML,
'docker_compose_domains' => json_encode([
'frontend' => ['domain' => 'https://example.com'],
]),
]);
$parsedCompose = applicationParser($application);
$labels = collect(data_get($parsedCompose, 'services.frontend.labels'));
expect($labels->values()->all())->toContain("traefik.docker.network={$application->uuid}");
});
test('applicationParser preserves a user-selected Traefik network', function () {
$application = Application::factory()->create([
'environment_id' => $this->environment->id,
'destination_id' => $this->destination->id,
'destination_type' => StandaloneDocker::class,
'build_pack' => 'dockercompose',
'docker_compose_raw' => <<<'YAML'
services:
frontend:
image: nginx:latest
labels:
traefik.docker.network: custom-network
networks:
- custom-network
networks:
custom-network: {}
YAML,
'docker_compose_domains' => json_encode([
'frontend' => ['domain' => 'https://example.com'],
]),
]);
$parsedCompose = applicationParser($application);
$labels = collect(data_get($parsedCompose, 'services.frontend.labels'));
expect($labels->values()->all())
->toContain('traefik.docker.network=custom-network')
->not->toContain("traefik.docker.network={$application->uuid}");
});
@@ -0,0 +1,49 @@
<?php
use App\Models\Environment;
use App\Models\Project;
use App\Models\Server;
use App\Models\Service;
use App\Models\ServiceApplication;
use App\Models\StandaloneDocker;
use App\Models\Team;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Bus;
uses(RefreshDatabase::class);
it('selects the Coolify service network for Traefik routed compose services', function () {
Bus::fake();
$team = Team::factory()->create();
$server = Server::factory()->create(['team_id' => $team->id]);
$destination = StandaloneDocker::where('server_id', $server->id)->firstOrFail();
$project = Project::factory()->create(['team_id' => $team->id]);
$environment = Environment::factory()->create(['project_id' => $project->id]);
$service = Service::factory()->create([
'environment_id' => $environment->id,
'server_id' => $server->id,
'destination_id' => $destination->id,
'destination_type' => $destination->getMorphClass(),
'docker_compose_raw' => <<<'YAML'
services:
app:
image: nginx:latest
networks:
- custom-network
networks:
custom-network: {}
YAML,
]);
ServiceApplication::create([
'name' => 'app',
'service_id' => $service->id,
'fqdn' => 'https://example.com',
]);
$parsedCompose = serviceParser($service);
$labels = collect(data_get($parsedCompose, 'services.app.labels'));
expect($labels->values()->all())->toContain("traefik.docker.network={$service->uuid}");
});
@@ -0,0 +1,30 @@
<?php
it('adds the Coolify network when the user did not select a Traefik network', function () {
$labels = addTraefikDockerNetworkLabel(collect([
'traefik.enable=true',
]), 'app-uuid');
expect($labels->values()->all())->toContain('traefik.docker.network=app-uuid');
});
it('preserves a user-selected Traefik network', function () {
$labels = addTraefikDockerNetworkLabel(collect([
'traefik.enable=true',
'traefik.docker.network=custom-network',
]), 'app-uuid');
expect($labels->values()->all())
->toContain('traefik.docker.network=custom-network')
->not->toContain('traefik.docker.network=app-uuid');
});
it('treats a bare user-provided Traefik network label as authoritative', function () {
$labels = addTraefikDockerNetworkLabel(collect([
'traefik.docker.network',
]), 'app-uuid');
expect($labels->values()->all())
->toContain('traefik.docker.network')
->not->toContain('traefik.docker.network=app-uuid');
});