Skip to content

An open-source, self-hostable PaaS alternative to Vercel, Heroku & Netlify that lets you easily deploy static sites, databases, full-stack applications and 360+ one-click services on your own servers.

fix(deployments): apply custom docker options when custom container name is set (#11479)

Omid Sayfun3d0caa236f9d
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 1b0ccb2184..327fea25e7 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -3427,24 +3427,19 @@ COPY ./nginx.conf /etc/nginx/conf.d/default.conf");
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 0000000000..9c671156d1
--- /dev/null
+++ b/tests/Unit/ApplicationDeploymentCustomDockerOptionsTest.php
@@ -0,0 +1,97 @@
+<?php
+
+use App\Jobs\ApplicationDeploymentJob;
+use App\Models\Application;
+use App\Models\ApplicationDeploymentQueue;
+use App\Models\Environment;
+use App\Models\Project;
+use App\Models\Server;
+use App\Models\StandaloneDocker;
+use App\Models\Team;
+use Illuminate\Foundation\Testing\RefreshDatabase;
+use Illuminate\Support\Collection;
+use Symfony\Component\Yaml\Yaml;
+use Tests\TestCase;
+
+uses(TestCase::class, RefreshDatabase::class);
+
+class TestableCustomDockerOptionsDeploymentJob extends ApplicationDeploymentJob
+{
+ public function __construct() {}
+
+ public function execute_remote_command(...$commands): void {}
+}
+
+function generateComposeServiceWithCustomDockerOptions(string $customDockerOptions): array
+{
+ $team = Team::create([
+ 'name' => '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');
+});