From 523f959ad326157cfac07a04f91a925402450d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Kucharczyk?= Date: Fri, 29 Dec 2023 11:25:20 +0100 Subject: [PATCH] GetContainerLayerContents: add --- GetContainerLayerContents.ps1 | 51 +++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 GetContainerLayerContents.ps1 diff --git a/GetContainerLayerContents.ps1 b/GetContainerLayerContents.ps1 new file mode 100644 index 0000000..dbf0458 --- /dev/null +++ b/GetContainerLayerContents.ps1 @@ -0,0 +1,51 @@ +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