Last active
September 14, 2018 21:29
-
-
Save Iristyle/a4d58cd77afeda8830523e4be15d23de to your computer and use it in GitHub Desktop.
Docker / LCOW helpers for Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-DockerConfigFilePath | |
{ | |
$path = "${ENV:ALLUSERSPROFILE}\docker\config\daemon.json" | |
# retrieve the path to daemon.json from docker service config, if present | |
sc.exe qc docker | | |
? { $_ -match 'BINARY_PATH_NAME\s*:\s*(.*)' } | | |
? { $_ -match '--config-file\s*(.*daemon\.json)' } | |
# found BINARY_PATH_NAME which contains --config-file *AND* file exists | |
if (($Matches.Count -gt 0) -and ((Test-Path -Path $Matches[1]) -eq $True)) | |
{ | |
$path = $Matches[1] | |
} | |
Write-Host "Config file path: $path" | |
return $path | |
} | |
function Get-DockerConfig | |
{ | |
$json = '{}' | |
$configFilePath = Get-DockerConfigFilePath | |
if (Test-Path $configFilePath) | |
{ | |
$json = Get-Content $configFilePath | |
Write-Host "Existing config:`n`n$json`n`n" | |
} | |
return ($json | ConvertFrom-Json) | |
} | |
function Set-DockerConfig($hash) | |
{ | |
$configFilePath = Get-DockerConfigFilePath | |
$utf8NoBom = New-Object System.Text.UTF8Encoding $False | |
# making sure to write a UTF-8 file with NO BOM | |
[System.IO.File]::WriteAllLines($configFilePath, ($hash | ConvertTo-Json), $utf8NoBom) | |
Write-Host "Updated config file:`n`n$(Get-Content $configFilePath)`n`n" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Enable-DockerExperimental | |
{ | |
$config = Get-DockerConfig | |
if (-not $config.PSObject.Properties.Match('experimental')) | |
{ | |
$config.experimental = $True | |
} | |
else | |
{ | |
$config | Add-Member -MemberType NoteProperty -Name 'experimental' -Value $True | |
} | |
Set-DockerConfig $config | |
} | |
Push-Location $Env:Temp | |
# upgrade the docker engine with experimental LCOW support | |
Invoke-WebRequest -OutFile docker-master.zip https://master.dockerproject.com/windows/x86_64/docker.zip | |
# Appveyor has the engine installed to C:\Program Files\Docker\Docker\Resources\dockerd.exe | |
Expand-Archive -Path docker-master.zip -DestinationPath . -Force | |
$dockerHome = "$Env:ProgramFiles\Docker\" | |
Stop-Service docker | |
Copy-Item .\docker\* $dockerHome | |
Enable-DockerExperimental | |
Start-Service docker | |
docker version | |
docker info | |
# run alpine to make sure Linux containers are working | |
docker run alpine | |
# acquire and build linuxkit from fork | |
go get -v -u github.com/Iristyle/linuxkit/src/cmd/linuxkit | |
# validate it was built and in path | |
& linuxkit.exe version | |
# clone the LCOW source and build the image with linuxkit | |
git clone https://github.com/linuxkit/lcow | |
Push-Location lcow | |
& linuxkit.exe build lcow.yml | |
# move the LCOW VM into the expected location | |
$containerHome = "$Env:ProgramFiles\Linux Containers" | |
New-Item $containerHome -Type Directory -Force | |
Copy-Item .\lcow-initrd.img "$containerHome\initrd.img" | |
Copy-Item .\lcow-kernel "$containerHome\kernel" | |
Pop-Location | |
# server doesn't require registration as experimental as its already setup that way | |
# re-register the service | |
# & $dockerHome\dockerd.exe --register-service --experimental | |
Pop-Location | |
Start-Service docker |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# runs a registry on | |
function Start-DockerRegistry | |
{ | |
docker pull registry:2 | |
docker run -d -p 5000:5000 --restart=always --name registry registry:2 | |
} | |
# runs a registry on top of Windows Nano Server | |
# requires LCOW on to test puppetserver and run this Windows container | |
function Start-DockerRegistryWindows | |
{ | |
# Run the registry container | |
# https://hub.docker.com/r/stefanscherer/registry-windows/ | |
New-Item -Type Directory c:\registry | |
docker pull stefanscherer/registry-windows:2.6.2 | |
docker run -d -p 5000:5000 --restart=always --name registry -v C:\registry:C:\registry stefanscherer/registry-windows:2.6.2 | |
} | |
function Add-UntrustedDockerRegistry($Address) | |
{ | |
$config = Get-DockerConfig | |
# Reconfigure the docker daemon to use the registry untrusted | |
# add the tcp listener on loopback for docker-api gem | |
# $config.hosts += 'tcp://127.0.0.1:2375' | |
if (-not $config.ContainsKey('insecure-registries')) | |
{ | |
$config.'insecure-registries' = @() | |
} | |
$config.'insecure-registries' += @("127.0.0.1:5000") | |
Set-DockerConfig $config | |
} | |
Start-DockerRegistry | |
Add-UntrustedDockerRegistry -Address '127.0.0.1:5000' | |
# Restart service for configuration to take effect | |
Restart-Service docker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment