perf: optimize Docker build with layer caching and BuildKit support

This commit is contained in:
2025-10-22 23:13:09 -04:00
parent 58bd57543b
commit c030f91ebe
11 changed files with 172 additions and 36 deletions
+10 -6
View File
@@ -4,17 +4,21 @@
FROM node:20-bullseye AS builder
WORKDIR /app
# Install dependencies
# Copy ONLY package files first (creates cacheable layer)
COPY package*.json ./
RUN npm ci
# Copy sources
# Install dependencies with cache mount for faster rebuilds
RUN --mount=type=cache,target=/root/.npm \
npm ci
# Copy source files AFTER npm install (only invalidates cache if code changes)
COPY . .
# Build Angular app (outputs to ./dist)
RUN npx ng build --configuration=production
# Build Angular app with cache mount for Angular's build cache
RUN --mount=type=cache,target=/app/.angular/cache \
npx ng build --configuration=production
# Prune dev dependencies to keep only production deps for runtime copy
# Prune dev dependencies to keep only production deps
RUN npm prune --omit=dev
# -------------------- Runtime stage --------------------
+59 -10
View File
@@ -2,7 +2,7 @@
.SYNOPSIS
Construit l'image Docker avec toutes les dépendances requises via PowerShell et WSL (Debian).
.DESCRIPTION
Ce script prépare les dépendances et construit l'image Docker sous WSL (Debian).
Ce script prépare les dépendances et construit l'image Docker sous WSL (Debian) avec BuildKit activé pour des builds plus rapides.
.PARAMETRES
-full : Si présent, effectue une construction complète de l'image Docker (équivalent à --no-cache).
.EXEMPLE
@@ -43,30 +43,79 @@ try {
exit 1
}
# Compose build command
# Check if .dockerignore exists, warn if not
$dockerignorePath = Join-Path $projectRoot ".dockerignore"
if (-not (Test-Path $dockerignorePath)) {
Write-Warning "Aucun fichier .dockerignore trouvé. Créez-en un pour optimiser le build."
Write-Host "Exemple de contenu :" -ForegroundColor Yellow
Write-Host @"
node_modules
dist
.git
.angular
.vscode
*.log
tmp
db/*.db
.env
"@ -ForegroundColor DarkGray
}
# Compose build command with BuildKit enabled
$noCache = if ($full) { '--no-cache' } else { '' }
$innerCmd = "cd '$wslProjectRoot' && docker build $noCache -t obsiviewer-angular:latest -f docker/Dockerfile ."
# Escape spaces in path for bash
$escapedPath = $wslProjectRoot -replace ' ','\\ '
# Build command parts
$cdCmd = "cd `"$wslProjectRoot`""
$dockerCmd = "docker build $noCache --progress=plain -t obsiviewer-angular:latest -f docker/Dockerfile ."
# Complete command with BuildKit
$innerCmd = "export DOCKER_BUILDKIT=1 && $cdCmd && $dockerCmd"
# Run build inside WSL Debian to use Linux Docker daemon
Write-Host "Construction de l'image Docker obsiviewer-angular:latest..." -ForegroundColor Cyan
Write-Host "BuildKit activé pour un build optimisé avec cache" -ForegroundColor Cyan
$buildResult = wsl -d Debian bash -lc $innerCmd 2>&1
# Display build output
$buildResult | ForEach-Object { Write-Host $_ }
if ($LASTEXITCODE -ne 0) {
Write-Error "Erreur lors de la construction de l'image Docker : $buildResult"
Write-Error "Erreur lors de la construction de l'image Docker (code: $LASTEXITCODE)"
exit 1
}
# Verify the image was built successfully
$verifyCmd = "docker image inspect obsiviewer-angular:latest"
Write-Host "Vérification de l'image construite..." -ForegroundColor Cyan
$verifyResult = wsl -d Debian bash -lc $verifyCmd 2>&1
$verifyCmd = "docker image inspect obsiviewer-angular:latest --format='{{.Size}}'"
Write-Host "`nVérification de l'image construite..." -ForegroundColor Cyan
$imageSize = wsl -d Debian bash -lc $verifyCmd 2>&1
if ($LASTEXITCODE -ne 0) {
Write-Error "L'image Docker n'a pas été construite correctement : $verifyResult"
Write-Error "L'image Docker n'a pas été construite correctement : $imageSize"
exit 1
}
Write-Host "Image Docker obsiviewer-angular:latest construite avec succès via WSL Debian." -ForegroundColor Green
# Convert size to MB
$sizeMB = [math]::Round([long]$imageSize / 1MB, 2)
Write-Host "`n================================================" -ForegroundColor Green
Write-Host "✓ Image Docker construite avec succès" -ForegroundColor Green
Write-Host " Nom: obsiviewer-angular:latest" -ForegroundColor Green
Write-Host " Taille: $sizeMB MB" -ForegroundColor Green
Write-Host "================================================" -ForegroundColor Green
# Display tips for faster rebuilds
if (-not $full) {
Write-Host "`nConseils pour les prochains builds :" -ForegroundColor Cyan
Write-Host " • Les dépendances npm sont mises en cache" -ForegroundColor Gray
Write-Host " • Le cache Angular est préservé entre les builds" -ForegroundColor Gray
Write-Host " • Seuls les fichiers modifiés déclencheront un rebuild" -ForegroundColor Gray
}
}
catch {
Write-Error "Erreur lors de la construction de l'image Docker : $_"
exit 1
}
}