[CmdletBinding()] param( [string]$Path = ".", [string]$Tag = "app:local", [string]$Dockerfile = "Dockerfile", [switch]$NoCache, [switch]$Pull, [switch]$Apply ) $ErrorActionPreference = 'Stop' if (-not (Get-Command docker -ErrorAction SilentlyContinue)) { throw "docker not found in PATH" } $context = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path if (-not (Test-Path -LiteralPath $context -PathType Container)) { throw "Build context is not a folder: $context" } if ([System.IO.Path]::IsPathRooted($Dockerfile)) { $dockerfilePath = $Dockerfile } else { $dockerfilePath = Join-Path $context $Dockerfile } if (-not (Test-Path -LiteralPath $dockerfilePath -PathType Leaf)) { throw "Dockerfile not found: $dockerfilePath" } $buildArgs = @('build', '-t', $Tag, '-f', $dockerfilePath) if ($NoCache) { $buildArgs += '--no-cache' } if ($Pull) { $buildArgs += '--pull' } $buildArgs += $context Write-Host "Context: $context" Write-Host "Dockerfile: $dockerfilePath" Write-Host "Tag: $Tag" Write-Host "Command: docker $($buildArgs -join ' ')" if (-not $Apply) { Write-Host "Preview only. Add -Apply to run the build." exit 0 } & docker @buildArgs if ($LASTEXITCODE -ne 0) { throw "docker build failed with code $LASTEXITCODE" } $sizeText = ((& docker image inspect --format '{{.Size}}' $Tag 2>$null) -join "`n").Trim() if ($LASTEXITCODE -eq 0 -and $sizeText) { $sizeMb = [math]::Round([double]$sizeText / 1MB, 1) Write-Host "Image size: $sizeMb MB" } Write-Host "Build finished. Image was not pushed."