Skip to content

Instantly share code, notes, and snippets.

@mtelvers
Last active June 27, 2024 16:05
Show Gist options
  • Save mtelvers/6519626150f8e4ed02acc3bc6afd583c to your computer and use it in GitHub Desktop.
Save mtelvers/6519626150f8e4ed02acc3bc6afd583c to your computer and use it in GitHub Desktop.
Using `runhcs` to create a container
$config = Invoke-RestMethod -uri https://raw.githubusercontent.com/Microsoft/hcsshim/d0b3bfc2ea9303a6a506da319f67fe827530b91e/test/functional/assets/defaultwindowsspec.json -Method Get
$config.process.args = @("cmd", "/c", "echo hello")
$config.windows.layerFolders = New-Object System.Collections.ArrayList
$ver = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion" -Name LCUVer).LCUVer
$image = "mcr.microsoft.com/windows/servercore:$ver"
docker image pull $image
$imageInspect = docker image inspect $image | ConvertFrom-JSON
$layer = $imageInspect.GraphDriver.Data.dir
while ($layer) {
$config.windows.layerFolders.Add($layer)
$layer = Get-Content "$layer\layerchain.json" | ConvertFrom-Json
}
mkdir c:\temp
mkdir c:\temp\scratch
mkdir c:\temp\bundle
$config.windows.layerFolders.Add("c:\temp\scratch")
$config | ConvertTo-Json | Set-Content c:\temp\bundle\config.json
runhcs create --pid-file c:\temp\pid --shim-log c:\temp\log.txt --vm-log c:\temp\vm.txt --bundle c:\temp\bundle mycontainer
runhcs list
runhcs start mycontainer
type c:\temp\log.txt
runhcs list
runhcs delete mycontainer
wclayer unmount c:\temp\scratch\
wclayer remove c:\temp\scratch\
Remove-Item -Recurse C:\temp\
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment