From c7280898046395d712d3892c2d88d31f9b5a831d Mon Sep 17 00:00:00 2001 From: Brandon Bridges Date: Wed, 7 Oct 2026 13:27:11 +0100 Subject: [PATCH] fix(deploy): keep build helper containers out of --add-host (#11781) Co-authored-by: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> --- app/Jobs/ApplicationDeploymentJob.php | 60 ++++++++++++------- .../ApplicationDeploymentAddHostsTest.php | 36 +++++++++++ 2 files changed, 74 insertions(+), 22 deletions(-) create mode 100644 tests/Feature/ApplicationDeploymentAddHostsTest.php diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 3a24d8bd50..4c8b6ca3e9 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -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) { diff --git a/tests/Feature/ApplicationDeploymentAddHostsTest.php b/tests/Feature/ApplicationDeploymentAddHostsTest.php new file mode 100644 index 0000000000..f57ac02e35 --- /dev/null +++ b/tests/Feature/ApplicationDeploymentAddHostsTest.php @@ -0,0 +1,36 @@ +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'); +});