Docker Community Forums

Share and learn in the Docker community.

Create a hyperV machine INSIDE the container

My main concern is getting a semi-arbitrary hyperV instance inside the docker container. we have a fairly elaborate powershell process that builds a hyperV machine, then images that machine’s hard drive so the image can be deployed onto physical devices. Currently there’s a dependency for this process on the particular metal machine in my cube (and one other’s cube for redundancy), but we want to get away from that by containerizing the process to make it more dynamic and scalable, potentially even leverage it toward something approaching CI.
I have the following docker file (exact image isn’t important, but this is what I’m currently using):

FROM microsoft/powershell:nanoserver-1803 AS powershell
COPY ./mainContainer/ c:/app/

and need to execute powershell similar to that below inside the container

New-VM -Name $VmName -Generation 2 -Path "$TempDirectory\$VmName" -VHDPath $vhdPath -Switch $switchName
Set-VMMemory -VMName $VmName -StartupBytes 4096MB -DynamicMemoryEnabled $false 
Add-VMScsiController -VMName $VmName 
Add-VMDvdDrive -VMName $VmName -ControllerNumber 1 -ControllerLocation 0 -Path $WindowsIsoPath 
Set-VMFirmware -VMName $VmName -FirstBootDevice $dvdDrive 
Start-VM -VMName $VmName 
$vm = Get-Vm $VMName
# then there's little loop waiting for the machine to turn off before continuing
  • I currently get an error with these cmdlets because they can’t be found in a loaded module.
  • I try importing hyperv module and it doesn’t exist
  • I try installing the hyperv windows feature (Install-WindowsFeature) and get an error that this cmdlet doesn’t exist
  • I try Import-Module ServerManager and get an error that no such module can be found.
  • My next effort is to go look to see if I can manually bring in the servermanager module’s script module, but I’m not very optimistic at this point…

any advice?