Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/Actions/Server/InstallDocker.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ public function handle(Server $server)
]);
if ($server->isSwarm()) {
$command = $command->merge([
'docker network create --attachable --driver overlay coolify-overlay >/dev/null 2>&1 || true',
dockerNetworkCreateCommand('coolify-overlay', isSwarm: true, quiet: true).' || true',
]);
} else {
$command = $command->merge([
'docker network create --attachable coolify >/dev/null 2>&1 || true',
dockerNetworkCreateCommand('coolify', quiet: true).' || true',
]);
$command = $command->merge([
"echo 'Done!'",
Expand Down
2 changes: 1 addition & 1 deletion app/Actions/Service/StartService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function handle(Service $service, bool $pullLatestImages = false, bool $s
}
if ($service->networks()->count() > 0) {
$commands[] = "echo 'Creating Docker network.'";
$commands[] = "docker network inspect $service->uuid >/dev/null 2>&1 || docker network create --attachable $service->uuid";
$commands[] = dockerNetworkCreateCommand($service->uuid);
}
$commands[] = 'echo Starting service.';
$commands[] = "docker compose --project-directory {$workdir} -f {$workdir}/docker-compose.yml --project-name {$service->uuid} up -d --remove-orphans --force-recreate --build";
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ApplicationDeploymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ private function deploy_docker_compose_buildpack()
// TODO
} else {
$this->execute_remote_command([
"docker network inspect '{$networkId}' >/dev/null 2>&1 || docker network create --attachable '{$networkId}' >/dev/null || true",
dockerNetworkCreateCommand($networkId, quiet: true).' || true',
'hidden' => true,
'ignore_errors' => true,
], [
Expand Down
4 changes: 2 additions & 2 deletions app/Models/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1406,9 +1406,9 @@ public function validateCoolifyNetwork($isSwarm = false, $isBuildServer = false)
return;
}
if ($isSwarm) {
return instant_remote_process(['docker network create --attachable --driver overlay coolify-overlay >/dev/null 2>&1 || true'], $this, false);
return instant_remote_process([dockerNetworkCreateCommand('coolify-overlay', isSwarm: true, quiet: true).' || true'], $this, false);
} else {
return instant_remote_process(['docker network create coolify --attachable >/dev/null 2>&1 || true'], $this, false);
return instant_remote_process([dockerNetworkCreateCommand('coolify', quiet: true).' || true'], $this, false);
}
}

Expand Down
34 changes: 26 additions & 8 deletions bootstrap/helpers/proxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use App\Enums\ProxyTypes;
use App\Models\Application;
use App\Models\Server;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Yaml\Yaml;

Expand All @@ -21,6 +22,19 @@ function isDockerPredefinedNetwork(string $network): bool
return in_array($network, ['default', 'host'], true);
}

function dockerNetworkCreateCommand(string $network, bool $isSwarm = false, bool $quiet = false): string
{
$safe = escapeshellarg($network);
$output = $quiet ? ' >/dev/null 2>&1' : '';
$inspect = "docker network inspect {$safe} >/dev/null 2>&1";

if ($isSwarm) {
return "{$inspect} || docker network create --driver overlay --attachable {$safe}{$output}";
}

return "{$inspect} || docker network create --attachable --ipv6 {$safe}{$output} || {$inspect} || docker network create --attachable {$safe}{$output}";
}

function collectProxyDockerNetworksByServer(Server $server)
{
if (! $server->isFunctional()) {
Expand Down Expand Up @@ -110,17 +124,19 @@ function connectProxyToNetworks(Server $server)
if ($server->isSwarm()) {
$commands = $networks->map(function ($network) {
$safe = escapeshellarg($network);

return [
"docker network ls --format '{{.Name}}' | grep '^{$network}$' >/dev/null || docker network create --driver overlay --attachable {$safe} >/dev/null",
dockerNetworkCreateCommand($network, isSwarm: true, quiet: true),
"docker network connect {$safe} coolify-proxy >/dev/null 2>&1 || true",
"echo 'Successfully connected coolify-proxy to {$safe} network.'",
];
});
} else {
$commands = $networks->map(function ($network) {
$safe = escapeshellarg($network);

return [
"docker network ls --format '{{.Name}}' | grep '^{$network}$' >/dev/null || docker network create --attachable {$safe} >/dev/null",
dockerNetworkCreateCommand($network, quiet: true),
"docker network connect {$safe} coolify-proxy >/dev/null 2>&1 || true",
"echo 'Successfully connected coolify-proxy to {$safe} network.'",
];
Expand All @@ -135,7 +151,7 @@ function connectProxyToNetworks(Server $server)
* This must be called BEFORE docker compose up since the compose file declares networks as external.
*
* @param Server $server The server to ensure networks on
* @return \Illuminate\Support\Collection Commands to create networks if they don't exist
* @return Collection Commands to create networks if they don't exist
*/
function ensureProxyNetworksExist(Server $server)
{
Expand All @@ -144,17 +160,19 @@ function ensureProxyNetworksExist(Server $server)
if ($server->isSwarm()) {
$commands = $networks->map(function ($network) {
$safe = escapeshellarg($network);

return [
"echo 'Ensuring network {$safe} exists...'",
"docker network ls --format '{{.Name}}' | grep -q '^{$network}$' || docker network create --driver overlay --attachable {$safe}",
dockerNetworkCreateCommand($network, isSwarm: true),
];
});
} else {
$commands = $networks->map(function ($network) {
$safe = escapeshellarg($network);

return [
"echo 'Ensuring network {$safe} exists...'",
"docker network ls --format '{{.Name}}' | grep -q '^{$network}$' || docker network create --attachable {$safe}",
dockerNetworkCreateCommand($network),
];
});
}
Expand Down Expand Up @@ -211,7 +229,7 @@ function extractCustomProxyCommands(Server $server, string $existing_config): ar
$custom_commands[] = $command;
}
}
} catch (\Exception $e) {
} catch (Exception $e) {
// If we can't parse the config, return empty array
// Silently fail to avoid breaking the proxy regeneration
}
Expand Down Expand Up @@ -432,7 +450,7 @@ function getExactTraefikVersionFromContainer(Server $server): ?string
Log::debug("getExactTraefikVersionFromContainer: Server '{$server->name}' (ID: {$server->id}) - Could not detect exact version");

return null;
} catch (\Exception $e) {
} catch (Exception $e) {
Log::error("getExactTraefikVersionFromContainer: Server '{$server->name}' (ID: {$server->id}) - Error: ".$e->getMessage());

return null;
Expand Down Expand Up @@ -479,7 +497,7 @@ function getTraefikVersionFromDockerCompose(Server $server): ?string
Log::debug("getTraefikVersionFromDockerCompose: Server '{$server->name}' (ID: {$server->id}) - Image format doesn't match expected pattern: {$image}");

return null;
} catch (\Exception $e) {
} catch (Exception $e) {
Log::error("getTraefikVersionFromDockerCompose: Server '{$server->name}' (ID: {$server->id}) - Error: ".$e->getMessage());

return null;
Expand Down
17 changes: 17 additions & 0 deletions tests/Unit/ProxyHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,20 @@
// only filters 'default' and 'host', so we maintain consistency
expect(isDockerPredefinedNetwork('none'))->toBeFalse();
});

it('creates standalone docker networks with ipv6 fallback', function () {
$command = dockerNetworkCreateCommand('coolify');
$safe = escapeshellarg('coolify');

expect($command)->toContain("docker network inspect {$safe}")
->and($command)->toContain("docker network create --attachable --ipv6 {$safe}")
->and($command)->toContain("docker network create --attachable {$safe}");
});

it('keeps swarm docker network creation on overlay without ipv6 fallback', function () {
$command = dockerNetworkCreateCommand('coolify-overlay', isSwarm: true);
$safe = escapeshellarg('coolify-overlay');

expect($command)->toContain("docker network create --driver overlay --attachable {$safe}")
->and($command)->not->toContain('--ipv6');
});