diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php index 1b0ccb218..327fea25e 100644 --- a/app/Jobs/ApplicationDeploymentJob.php +++ b/app/Jobs/ApplicationDeploymentJob.php @@ -3427,24 +3427,19 @@ private function generate_compose_file() if ($this->pull_request_id === 0) { $custom_compose = convertDockerRunToCompose($this->application->custom_docker_run_options); if ((bool) $this->application->settings->is_consistent_container_name_enabled) { - if (! $this->application->settings->custom_internal_name) { - $docker_compose['services'][$this->application->uuid] = $docker_compose['services'][$this->container_name]; - if (count($custom_compose) > 0) { - $ipv4 = data_get($custom_compose, 'ip.0'); - $ipv6 = data_get($custom_compose, 'ip6.0'); - data_forget($custom_compose, 'ip'); - data_forget($custom_compose, 'ip6'); - if ($ipv4 || $ipv6) { - data_forget($docker_compose['services'][$this->application->uuid], 'networks'); - } - if ($ipv4) { - $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; - } - if ($ipv6) { - $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; - } - $docker_compose['services'][$this->application->uuid] = array_merge_recursive($docker_compose['services'][$this->application->uuid], $custom_compose); + $docker_compose['services'][$this->application->uuid] = $docker_compose['services'][$this->container_name]; + if (count($custom_compose) > 0) { + $ipv4 = data_get($custom_compose, 'ip.0'); + $ipv6 = data_get($custom_compose, 'ip6.0'); + data_forget($custom_compose, 'ip'); + data_forget($custom_compose, 'ip6'); + if ($ipv4) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv4_address'] = $ipv4; } + if ($ipv6) { + $docker_compose['services'][$this->application->uuid]['networks'][$this->destination->network]['ipv6_address'] = $ipv6; + } + $docker_compose['services'][$this->application->uuid] = array_merge_recursive($docker_compose['services'][$this->application->uuid], $custom_compose); } } else { if (count($custom_compose) > 0) { diff --git a/tests/Unit/ApplicationDeploymentCustomDockerOptionsTest.php b/tests/Unit/ApplicationDeploymentCustomDockerOptionsTest.php new file mode 100644 index 000000000..9c671156d --- /dev/null +++ b/tests/Unit/ApplicationDeploymentCustomDockerOptionsTest.php @@ -0,0 +1,97 @@ + 'Custom Docker Options Team', + 'personal_team' => false, + 'show_boarding' => false, + ]); + $project = Project::create([ + 'name' => 'Custom Docker Options Project', + 'team_id' => $team->id, + ]); + $environment = Environment::where('project_id', $project->id)->firstOrFail(); + $server = Server::factory()->create(['team_id' => $team->id]); + $destination = $server->standaloneDockers()->firstOrFail(); + $application = Application::factory()->create([ + 'environment_id' => $environment->id, + 'destination_id' => $destination->id, + 'destination_type' => StandaloneDocker::class, + 'build_pack' => 'dockerimage', + 'custom_docker_run_options' => $customDockerOptions, + 'custom_network_aliases' => json_encode(['custom-alias'], JSON_THROW_ON_ERROR), + ]); + $application->settings()->update([ + 'is_consistent_container_name_enabled' => true, + 'custom_internal_name' => 'custom-internal-name', + ]); + + $queue = Mockery::mock(ApplicationDeploymentQueue::class)->makePartial(); + $queue->status = 'queued'; + $queue->shouldReceive('refresh')->once()->andReturnSelf(); + + $application = $application->fresh(); + $job = new TestableCustomDockerOptionsDeploymentJob; + $reflection = new ReflectionClass(ApplicationDeploymentJob::class); + + foreach ([ + 'application' => $application, + 'application_deployment_queue' => $queue, + 'destination' => $destination, + 'server' => $server, + 'mainServer' => $server, + 'pull_request_id' => 0, + 'container_name' => $application->uuid, + 'production_image_name' => 'example/app:latest', + 'deployment_uuid' => 'deployment-uuid', + 'workdir' => '/artifacts/custom-docker-options-app', + 'configuration_dir' => '/data/coolify/applications/test', + 'saved_outputs' => new Collection, + ] as $property => $value) { + $reflection->getProperty($property)->setValue($job, $value); + } + + $reflection->getMethod('generate_compose_file')->invoke($job); + $compose = Yaml::parse($reflection->getProperty('docker_compose')->getValue($job)); + + return $compose['services'][$application->uuid]; +} + +it('applies an entrypoint when consistent naming and a custom internal name are configured', function () { + expect(generateComposeServiceWithCustomDockerOptions('--entrypoint "/bin/echo hello world"')) + ->toHaveKey('entrypoint', '/bin/echo hello world'); +}); + +it('preserves custom network aliases when a static IP is configured', function () { + $service = generateComposeServiceWithCustomDockerOptions('--ip 10.0.0.25 --ip6 2001:db8::25'); + $network = $service['networks'][array_key_first($service['networks'])]; + + expect($network) + ->toHaveKey('ipv4_address', '10.0.0.25') + ->toHaveKey('ipv6_address', '2001:db8::25') + ->toHaveKey('aliases') + ->and($network['aliases'])->toContain('custom-alias'); +});