Skip to content

Commit d2d5bd4

Browse files
authored
feat: add Docker build cache preservation toggles (#7352)
2 parents 71bf8a3 + 43bb2f3 commit d2d5bd4

File tree

6 files changed

+282
-99
lines changed

6 files changed

+282
-99
lines changed

.ai/core/deployment-architecture.md

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,84 @@ Routes: [routes/api.php](mdc:routes/api.php)
270270
- **Build artifact** reuse
271271
- **Parallel build** processing
272272

273+
### Docker Build Cache Preservation
274+
275+
Coolify provides settings to preserve Docker build cache across deployments, addressing cache invalidation issues.
276+
277+
#### The Problem
278+
279+
By default, Coolify injects `ARG` statements into user Dockerfiles for build-time variables. This breaks Docker's cache mechanism because:
280+
1. **ARG declarations invalidate cache** - Any change in ARG values after the `ARG` instruction invalidates all subsequent layers
281+
2. **SOURCE_COMMIT changes every commit** - Causes full rebuilds even when code changes are minimal
282+
283+
#### Application Settings
284+
285+
Two toggles in **Advanced Settings** control this behavior:
286+
287+
| Setting | Default | Description |
288+
|---------|---------|-------------|
289+
| `inject_build_args_to_dockerfile` | `true` | Controls whether Coolify adds `ARG` statements to Dockerfile |
290+
| `include_source_commit_in_build` | `false` | Controls whether `SOURCE_COMMIT` is included in build context |
291+
292+
**Database columns:** `application_settings.inject_build_args_to_dockerfile`, `application_settings.include_source_commit_in_build`
293+
294+
#### Buildpack Coverage
295+
296+
| Build Pack | ARG Injection | Method |
297+
|------------|---------------|--------|
298+
| **Dockerfile** | ✅ Yes | `add_build_env_variables_to_dockerfile()` |
299+
| **Docker Compose** (with `build:`) | ✅ Yes | `modify_dockerfiles_for_compose()` |
300+
| **PR Deployments** (Dockerfile only) | ✅ Yes | `add_build_env_variables_to_dockerfile()` |
301+
| **Nixpacks** | ❌ No | Generates its own Dockerfile internally |
302+
| **Static** | ❌ No | Uses internal Dockerfile |
303+
| **Docker Image** | ❌ No | No build phase |
304+
305+
#### How It Works
306+
307+
**When `inject_build_args_to_dockerfile` is enabled (default):**
308+
```dockerfile
309+
# Coolify modifies your Dockerfile to add:
310+
FROM node:20
311+
ARG MY_VAR=value
312+
ARG COOLIFY_URL=...
313+
ARG SOURCE_COMMIT=abc123 # (if include_source_commit_in_build is true)
314+
# ... rest of your Dockerfile
315+
```
316+
317+
**When `inject_build_args_to_dockerfile` is disabled:**
318+
- Coolify does NOT modify the Dockerfile
319+
- `--build-arg` flags are still passed (harmless without matching `ARG` in Dockerfile)
320+
- User must manually add `ARG` statements for any build-time variables they need
321+
322+
**When `include_source_commit_in_build` is disabled (default):**
323+
- `SOURCE_COMMIT` is NOT included in build-time variables
324+
- `SOURCE_COMMIT` is still available at **runtime** (in container environment)
325+
- Docker cache preserved across different commits
326+
327+
#### Recommended Configuration
328+
329+
| Use Case | inject_build_args | include_source_commit | Cache Behavior |
330+
|----------|-------------------|----------------------|----------------|
331+
| Maximum cache preservation | `false` | `false` | Best cache retention |
332+
| Need build-time vars, no commit | `true` | `false` | Cache breaks on var changes |
333+
| Need commit at build-time | `true` | `true` | Cache breaks every commit |
334+
| Manual ARG management | `false` | `true` | Cache preserved (no ARG in Dockerfile) |
335+
336+
#### Implementation Details
337+
338+
**Files:**
339+
- `app/Jobs/ApplicationDeploymentJob.php`:
340+
- `set_coolify_variables()` - Conditionally adds SOURCE_COMMIT to Docker build context based on `include_source_commit_in_build` setting
341+
- `generate_coolify_env_variables(bool $forBuildTime)` - Distinguishes build-time vs. runtime variables; excludes cache-busting variables like SOURCE_COMMIT from build context unless explicitly enabled
342+
- `generate_env_variables()` - Populates `$this->env_args` with build-time ARG values, respecting `include_source_commit_in_build` toggle
343+
- `add_build_env_variables_to_dockerfile()` - Injects ARG statements into Dockerfiles after FROM instructions; skips injection if `inject_build_args_to_dockerfile` is disabled
344+
- `modify_dockerfiles_for_compose()` - Applies ARG injection to Docker Compose service Dockerfiles; respects `inject_build_args_to_dockerfile` toggle
345+
- `app/Models/ApplicationSetting.php` - Defines `inject_build_args_to_dockerfile` and `include_source_commit_in_build` boolean properties
346+
- `app/Livewire/Project/Application/Advanced.php` - Livewire component providing UI bindings for cache preservation toggles
347+
- `resources/views/livewire/project/application/advanced.blade.php` - Checkbox UI elements for user-facing toggles
348+
349+
**Note:** Docker Compose services without a `build:` section (image-only) are automatically skipped.
350+
273351
### Runtime Optimization
274352
- **Container resource** limits
275353
- **Auto-scaling** based on metrics
@@ -428,7 +506,7 @@ services:
428506
- `templates/compose/chaskiq.yaml` - Entrypoint script
429507

430508
**Implementation:**
431-
- Parsed: `bootstrap/helpers/parsers.php` (line 717)
509+
- Parsed: `bootstrap/helpers/parsers.php` in `parseCompose()` function (handles `content` field extraction)
432510
- Storage: `app/Models/LocalFileVolume.php`
433511
- Validation: `tests/Unit/StripCoolifyCustomFieldsTest.php`
434512

@@ -481,7 +559,7 @@ services:
481559
- Pre-creating mount points before container starts
482560

483561
**Implementation:**
484-
- Parsed: `bootstrap/helpers/parsers.php` (line 718)
562+
- Parsed: `bootstrap/helpers/parsers.php` in `parseCompose()` function (handles `is_directory`/`isDirectory` field extraction)
485563
- Storage: `app/Models/LocalFileVolume.php` (`is_directory` column)
486564
- Validation: `tests/Unit/StripCoolifyCustomFieldsTest.php`
487565

0 commit comments

Comments
 (0)