Run a linux container on a windows server using Hyper-V using powershell.
Hyper-V
Make sure that the host has all the requirements to for hyper-v.
If running on a virtual machine, make sure that it has hyper-v nested virtualization support – it is the exception, not the rule. On azure, for example, you need a v3 instance.
Install
Using powershell, logged in as an administrator:
Install-WindowsFeature Hyper-V –IncludeManagementTools –Restart
Most likely the system will restart to complete the installation.
Docker machine
You can get the docker machine by installing Docker EE or you can just download the docker machine.
Docker EE
On a version of Windows server capable of:
Install-Module "DockerMsftProvider" -Force
Update-Module "DockerMsftProvider"
Install-Package Docker -ProviderName "DockerMsftProvider" -Update -Force
Install-WindowsFeature Containers -Restart
Docker machine
In powershell:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -OutFile docker-machine.exe https://github.com/docker/machine/re
leases/download/v0.16.1/docker-machine-Windows-x86_64.exe
Create Virtual Switch
In powershell, as an admin:
$ethernet = Get-NetAdapter -Name ethernet
New-VMSwitch -Name External -NetAdapterName $ethernet.Name -AllowManagementOS $true
Create Virtual Machine
Example:
.\docker-machine.exe create --driver hyperv `
--hyperv-cpu-count 2 `
--hyperv-memory 4096 `
--hyperv-virtual-switch External `
docker
In this example the virtual machine will be named “docker” and assumes you have the docker-machine executable on the current folder.
You can customize the number of cpus and amount of memory used by the virtual machine by changing the parameters.
HINT: If you name the virtual machine “default”, you will save a lot of typing later on. Docker machine will assume “default” is the name whenever you don’t provide one.
This is a different example using rancherOS instead of the deprecated boot2docker:
.\docker-machine.exe create -d hyperv `
--hyperv-memory 2048 `
--hyperv-boot2docker-url `
https://releases.rancher.com/os/latest/hyperv/rancheros.iso `
--hyperv-virtual-switch External `
default
You should see something like this:
Prepare the environment
Again, in powershell as an administrator:
docker-machine.exe env | Invoke-Expression
This will set the environment variables so you can use docker on the host (assuming that you have installed Docker EE) to interact with containers on the virtual machine.
If you created the virtual machine using any name other than “default”, you will need to provide the name as argument on any calls to docker-machine.
For example, you can run a busybox container with:
PS C:\WINDOWS\system32> docker run -it busybox sh
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
697743189b6d: Pull complete
Digest: sha256:061ca9704a714ee3e8b80523ec720c64f6209ad3f97c0ff7cb9ec7d19f15149f
Status: Downloaded newer image for busybox:latest
/ # exit
or list the running containers simply with:
PS C:\WINDOWS\system32> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
References
- https://blogs.technet.microsoft.com/heyscriptingguy/2013/10/09/use-powershell-to-create-virtual-switches/
- https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/quick-start/try-hyper-v-powershell
- https://docs.docker.com/machine/drivers/hyper-v/
- https://docs.microsoft.com/en-us/azure/virtual-machines/windows/nested-virtualization#set-up-internet-connectivity-for-the-guest-virtual-machine
- https://aws.amazon.com/blogs/compute/running-hyper-v-on-amazon-ec2-bare-metal-instances/
- https://docs.microsoft.com/en-us/virtualization/hyper-v-on-windows/user-guide/nested-virtualization-azure-virtual-network
- https://rancher.com/docs/os/v1.x/en/installation/running-rancheros/workstation/docker-machine/
0 Comments