fix(deploy): keep build helper containers out of --add-host (#11781)

Co-authored-by: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com>
This commit is contained in:
Brandon Bridges
2026-10-07 14:27:11 +02:00
committed by GitHub
co-authored by Andras Bacsai
parent a6655fe992
commit c728089804
2 changed files with 74 additions and 22 deletions
+38 -22
View File
@@ -307,6 +307,43 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
}
}
/**
* Build the --add-host flags for the containers on the destination network.
*
* Build helper containers are named after their deployment uuid and only live for the
* duration of that deployment. BuildKit keys every RUN layer on the --add-host set,
* so letting one in gives the next build a different set and forces a full rebuild.
*/
private function addHostFlags(Collection $containers): string
{
$containers = $containers->sort()->values();
$buildHelperNames = ApplicationDeploymentQueue::query()
->whereIn('deployment_uuid', $containers->pluck('Name')->filter())
->pluck('deployment_uuid');
$ips = collect([]);
foreach ($containers as $container) {
$containerName = data_get($container, 'Name');
if ($containerName === 'coolify-proxy') {
continue;
}
if (isGeneratedContainerName($containerName)) {
continue;
}
if ($buildHelperNames->contains($containerName)) {
continue;
}
$containerIp = data_get($container, 'IPv4Address');
if ($containerName && $containerIp) {
$ips->put($containerName, str($containerIp)->before('/')->value());
}
}
return $ips->map(function ($ip, $name) {
return "--add-host $name:$ip";
})->implode(' ');
}
public function handle(): void
{
// Check if deployment was cancelled before we even started
@@ -347,28 +384,7 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
if (! is_null($allContainers)) {
$allContainers = format_docker_command_output_to_json($allContainers);
$ips = collect([]);
if (count($allContainers) > 0) {
$allContainers = $allContainers[0];
$allContainers = collect($allContainers)->sort()->values();
foreach ($allContainers as $container) {
$containerName = data_get($container, 'Name');
if ($containerName === 'coolify-proxy') {
continue;
}
if (isGeneratedContainerName($containerName)) {
continue;
}
$containerIp = data_get($container, 'IPv4Address');
if ($containerName && $containerIp) {
$containerIp = str($containerIp)->before('/');
$ips->put($containerName, $containerIp->value());
}
}
}
$this->addHosts = $ips->map(function ($ip, $name) {
return "--add-host $name:$ip";
})->implode(' ');
$this->addHosts = count($allContainers) > 0 ? $this->addHostFlags(collect($allContainers[0])) : '';
}
if ($this->application->dockerfile_target_build) {
@@ -0,0 +1,36 @@
<?php
use App\Jobs\ApplicationDeploymentJob;
use App\Models\ApplicationDeploymentQueue;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
function addHostFlagsFor(array $containers): string
{
$job = (new ReflectionClass(ApplicationDeploymentJob::class))->newInstanceWithoutConstructor();
return (new ReflectionMethod(ApplicationDeploymentJob::class, 'addHostFlags'))->invoke($job, collect($containers));
}
it('keeps build helper containers of other deployments out of --add-host', function () {
ApplicationDeploymentQueue::create(['application_id' => '1', 'deployment_uuid' => 'k0s8g4wc4c8gkkso0gk4cgww']);
$flags = addHostFlagsFor([
'a' => ['Name' => 'redis-db', 'IPv4Address' => '10.0.1.5/24'],
'b' => ['Name' => 'k0s8g4wc4c8gkkso0gk4cgww', 'IPv4Address' => '10.0.1.9/24'],
'c' => ['Name' => 'coolify-proxy', 'IPv4Address' => '10.0.1.2/24'],
'd' => ['Name' => 'app-uuid-20260908T141530', 'IPv4Address' => '10.0.1.7/24'],
]);
expect($flags)->toBe('--add-host redis-db:10.0.1.5');
});
it('keeps the --add-host set stable whether or not a build helper is running', function () {
ApplicationDeploymentQueue::create(['application_id' => '1', 'deployment_uuid' => 'helper-deployment-uuid']);
$database = ['Name' => 'postgres-main', 'IPv4Address' => '10.0.1.3/24'];
expect(addHostFlagsFor([$database, ['Name' => 'helper-deployment-uuid', 'IPv4Address' => '10.0.1.4/24']]))
->toBe(addHostFlagsFor([$database]))
->toBe('--add-host postgres-main:10.0.1.3');
});