scripts/GetContainerLayerContents.ps1

52 lines
1.4 KiB
PowerShell

param(
[Parameter(Mandatory=$true)]
[string]$imageName
)
# Save the Docker image as a tarball
$imageTar = "$imageName.tar"
docker save -o $imageTar $imageName
# Create a temporary directory to extract layers
$tempDir = [System.IO.Path]::GetTempPath() + [System.Guid]::NewGuid().ToString()
New-Item -ItemType Directory -Force -Path $tempDir
# Extract the image tarball using 'tar' command
tar -xf $imageTar -C $tempDir
# Function to list files in a layer
function ListLayerFiles {
param (
[string]$layerPath
)
$layerTar = Get-ChildItem -Path $layerPath -Filter "*.tar" -File
$layerDir = [System.IO.Path]::Combine($tempDir, [System.IO.Path]::GetRandomFileName())
New-Item -ItemType Directory -Force -Path $layerDir
# Extract the layer tarball using 'tar'
tar -xf $layerTar.FullName -C $layerDir
# List files in the layer
Write-Host "Files in layer:"
Get-ChildItem -Path $layerDir -Recurse
}
# List all layers and ask user to choose one
$layerDirs = Get-ChildItem -Path $tempDir -Directory
$layerDirs | ForEach-Object -Begin { $i = 0 } -Process {
Write-Host "[$i]: $($_.Name)"
$i++
}
$userChoice = Read-Host "Enter the index of the layer to list"
if ($userChoice -lt $layerDirs.Count -and $userChoice -ge 0) {
ListLayerFiles -layerPath $layerDirs[$userChoice].FullName
} else {
Write-Host "Invalid index selected."
}
# Cleanup
Remove-Item -Path $imageTar
Remove-Item -Path $tempDir -Recurse