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: inject environment variables into custom Docker Compose build commands

Andras Bacsai1094ab7a4645
diff --git a/app/Jobs/ApplicationDeploymentJob.php b/app/Jobs/ApplicationDeploymentJob.php
index 5dced0599f..44e4899767 100644
--- a/app/Jobs/ApplicationDeploymentJob.php
+++ b/app/Jobs/ApplicationDeploymentJob.php
@@ -652,11 +652,32 @@ class ApplicationDeploymentJob implements ShouldBeEncrypted, ShouldQueue
$this->save_buildtime_environment_variables();
if ($this->docker_compose_custom_build_command) {
- // Prepend DOCKER_BUILDKIT=1 if BuildKit is supported
$build_command = $this->docker_compose_custom_build_command;
+
+ // Inject --env-file flag if not already present in custom command
+ // This ensures build-time environment variables are available during the build
+ if (! str_contains($build_command, '--env-file')) {
+ $build_command = str_replace(
+ 'docker compose',
+ 'docker compose --env-file /artifacts/build-time.env',
+ $build_command
+ );
+ }
+
+ // Prepend DOCKER_BUILDKIT=1 if BuildKit is supported
if ($this->dockerBuildkitSupported) {
$build_command = "DOCKER_BUILDKIT=1 {$build_command}";
}
+
+ // Append build arguments if not using build secrets (matching default behavior)
+ if (! $this->application->settings->use_build_secrets && $this->build_args instanceof \Illuminate\Support\Collection && $this->build_args->isNotEmpty()) {
+ $build_args_string = $this->build_args->implode(' ');
+ // Escape single quotes for bash -c context used by executeInDocker
+ $build_args_string = str_replace("'", "'\\''", $build_args_string);
+ $build_command .= " {$build_args_string}";
+ $this->application_deployment_queue->addLogEntry('Adding build arguments to custom Docker Compose build command.');
+ }
+
$this->execute_remote_command(
[executeInDocker($this->deployment_uuid, "cd {$this->basedir} && {$build_command}"), 'hidden' => true],
);
diff --git a/resources/views/livewire/project/application/general.blade.php b/resources/views/livewire/project/application/general.blade.php
index c95260efe0..415a1d3787 100644
--- a/resources/views/livewire/project/application/general.blade.php
+++ b/resources/views/livewire/project/application/general.blade.php
@@ -259,7 +259,7 @@
<div class="flex gap-2">
<x-forms.input x-bind:disabled="shouldDisable()"
placeholder="docker compose build" id="dockerComposeCustomBuildCommand"
- helper="If you use this, you need to specify paths relatively and should use the same compose file in the custom command, otherwise the automatically configured labels / etc won't work.<br><br>So in your case, use: <span class='dark:text-warning'>docker compose -f .{{ Str::start($application->base_directory . $application->docker_compose_location, '/') }} build</span>"
+ helper="If you use this, you need to specify paths relatively and should use the same compose file in the custom command, otherwise the automatically configured labels / etc won't work.<br><br>Environment variables are automatically injected via <span class='dark:text-warning'>--env-file</span> flag. If you need custom env handling, include your own <span class='dark:text-warning'>--env-file</span> flag in the command.<br><br>So in your case, use: <span class='dark:text-warning'>docker compose -f .{{ Str::start($application->base_directory . $application->docker_compose_location, '/') }} build</span>"
label="Custom Build Command" />
<x-forms.input x-bind:disabled="shouldDisable()"
placeholder="docker compose up -d" id="dockerComposeCustomStartCommand"
diff --git a/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php b/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php
new file mode 100644
index 0000000000..8d9d748ca0
--- /dev/null
+++ b/tests/Unit/ApplicationDeploymentCustomBuildCommandTest.php
@@ -0,0 +1,133 @@
+<?php
+
+/**
+ * Test to verify that custom Docker Compose build commands properly inject environment variables.
+ *
+ * This test suite verifies that when using a custom build command, the system automatically
+ * injects the --env-file flag to ensure build-time environment variables are available during
+ * the build process. This fixes the issue where environment variables were lost when using
+ * custom build commands.
+ *
+ * The fix ensures that:
+ * - --env-file /artifacts/build-time.env is automatically injected after 'docker compose'
+ * - Users can still provide their own --env-file flag to override the default behavior
+ * - Build arguments are appended when not using build secrets
+ */
+it('injects --env-file flag into custom build command', function () {
+ $customCommand = 'docker compose -f ./docker-compose.yaml build';
+
+ // Simulate the injection logic from ApplicationDeploymentJob
+ if (! str_contains($customCommand, '--env-file')) {
+ $customCommand = str_replace(
+ 'docker compose',
+ 'docker compose --env-file /artifacts/build-time.env',
+ $customCommand
+ );
+ }
+
+ expect($customCommand)->toBe('docker compose --env-file /artifacts/build-time.env -f ./docker-compose.yaml build');
+ expect($customCommand)->toContain('--env-file /artifacts/build-time.env');
+});
+
+it('does not duplicate --env-file flag when already present', function () {
+ $customCommand = 'docker compose --env-file /custom/.env -f ./docker-compose.yaml build';
+
+ // Simulate the injection logic from ApplicationDeploymentJob
+ if (! str_contains($customCommand, '--env-file')) {
+ $customCommand = str_replace(
+ 'docker compose',
+ 'docker compose --env-file /artifacts/build-time.env',
+ $customCommand
+ );
+ }
+
+ expect($customCommand)->toBe('docker compose --env-file /custom/.env -f ./docker-compose.yaml build');
+ expect(substr_count($customCommand, '--env-file'))->toBe(1);
+});
+
+it('preserves custom build command structure with env-file injection', function () {
+ $customCommand = 'docker compose -f ./custom/path/docker-compose.prod.yaml build --no-cache';
+
+ // Simulate the injection logic from ApplicationDeploymentJob
+ if (! str_contains($customCommand, '--env-file')) {
+ $customCommand = str_replace(
+ 'docker compose',
+ 'docker compose --env-file /artifacts/build-time.env',
+ $customCommand
+ );
+ }
+
+ expect($customCommand)->toBe('docker compose --env-file /artifacts/build-time.env -f ./custom/path/docker-compose.prod.yaml build --no-cache');
+ expect($customCommand)->toContain('--env-file /artifacts/build-time.env');
+ expect($customCommand)->toContain('-f ./custom/path/docker-compose.prod.yaml');
+ expect($customCommand)->toContain('build --no-cache');
+});
+
+it('handles multiple docker compose commands in custom build command', function () {
+ // Edge case: Only the first 'docker compose' should get the env-file flag
+ $customCommand = 'docker compose -f ./docker-compose.yaml build';
+
+ // Simulate the injection logic from ApplicationDeploymentJob
+ if (! str_contains($customCommand, '--env-file')) {
+ $customCommand = str_replace(
+ 'docker compose',
+ 'docker compose --env-file /artifacts/build-time.env',
+ $customCommand
+ );
+ }
+
+ // Note: str_replace replaces ALL occurrences, which is acceptable in this case
+ // since you typically only have one 'docker compose' command
+ expect($customCommand)->toContain('docker compose --env-file /artifacts/build-time.env');
+});
+
+it('verifies build args would be appended correctly', function () {
+ $customCommand = 'docker compose --env-file /artifacts/build-time.env -f ./docker-compose.yaml build';
+ $buildArgs = collect([
+ '--build-arg NODE_ENV=production',
+ '--build-arg API_URL=https://api.example.com',
+ ]);
+
+ // Simulate build args appending logic
+ $buildArgsString = $buildArgs->implode(' ');
+ $buildArgsString = str_replace("'", "'\\''", $buildArgsString);
+ $customCommand .= " {$buildArgsString}";
+
+ expect($customCommand)->toContain('--build-arg NODE_ENV=production');
+ expect($customCommand)->toContain('--build-arg API_URL=https://api.example.com');
+ expect($customCommand)->toBe(
+ 'docker compose --env-file /artifacts/build-time.env -f ./docker-compose.yaml build --build-arg NODE_ENV=production --build-arg API_URL=https://api.example.com'
+ );
+});
+
+it('properly escapes single quotes in build args', function () {
+ $buildArg = "--build-arg MESSAGE='Hello World'";
+
+ // Simulate the escaping logic from ApplicationDeploymentJob
+ $escapedBuildArg = str_replace("'", "'\\''", $buildArg);
+
+ expect($escapedBuildArg)->toBe("--build-arg MESSAGE='\\''Hello World'\\''");
+});
+
+it('handles DOCKER_BUILDKIT prefix with env-file injection', function () {
+ $customCommand = 'docker compose -f ./docker-compose.yaml build';
+
+ // Simulate the injection logic from ApplicationDeploymentJob
+ if (! str_contains($customCommand, '--env-file')) {
+ $customCommand = str_replace(
+ 'docker compose',
+ 'docker compose --env-file /artifacts/build-time.env',
+ $customCommand
+ );
+ }
+
+ // Simulate BuildKit support
+ $dockerBuildkitSupported = true;
+ if ($dockerBuildkitSupported) {
+ $customCommand = "DOCKER_BUILDKIT=1 {$customCommand}";
+ }
+
+ expect($customCommand)->toBe('DOCKER_BUILDKIT=1 docker compose --env-file /artifacts/build-time.env -f ./docker-compose.yaml build');
+ expect($customCommand)->toStartWith('DOCKER_BUILDKIT=1');
+ expect($customCommand)->toContain('--env-file /artifacts/build-time.env');
+});