Skip to content

Instantly share code, notes, and snippets.

@xirixiz
Forked from pavlovpetro/set_up_docker.ps1
Created November 30, 2021 07:55
Show Gist options
  • Save xirixiz/b11dab2c938adc0ff16e7d38638911ff to your computer and use it in GitHub Desktop.
Save xirixiz/b11dab2c938adc0ff16e7d38638911ff to your computer and use it in GitHub Desktop.
This script setting up a Docker for Windows - adds your Local Registry and Proxy, changes ProgramDir and the location of the MobyLinuxVM
#### Set up Docker Daemon
#### Check if this script is ran with admin rights
# if no - exit with message
function Test-Elevation {
$role = [Security.Principal.WindowsBuiltInRole]::Administrator
$principal = [Security.Principal.WindowsPrincipal]`
[Security.Principal.WindowsIdentity]::GetCurrent()
$principal.IsInRole($role)
}
if ( -not (Test-Elevation) ) {
Write-Error "The script must be run with admin's rights!`n" -Category PermissionDenied
exit
}
#### Windows
# Switch Deamon to Windows mode
write-host "`nSetting up a Windows Daemon`n" -ForegroundColor Green
write-host "Switching Docker to the Windows Daemon`n" -ForegroundColor Green
& 'C:\Program Files\Docker\Docker\DockerCli.exe' -SwitchWindowsEngine
# Get content of the conig file and convert it for work
$conf = Get-Content 'C:\ProgramData\Docker\config\daemon.json' -Raw | ConvertFrom-Json
# Ask user whether need to change a docker's system dir if no, use default dir
write-host "By default a Docker ProgramDir is 'C:\ProgramData\Docker\'. But you can point another disk just point a letter of the disk in the following format: 'D'." -ForegroundColor cyan
$get_disk = read-host "Enter a disk letter i.e. 'D' or just press 'Enter' to default"
if (($get_disk) -and ($get_disk -ne $env:HOMEDRIVE.Split(":")[0])) {
if ( -not ($conf.'data-root')){
$conf | Add-Member -Name 'data-root' -Value $($get_disk + ":\Docker") -MemberType NoteProperty
}
else {
$conf.'data-root'=$get_disk + ":\Docker"
}
write-host 'The ProgramDir of the Windows Daemon will be here:' $conf.'data-root' -ForegroundColor yellow
}
#elseif ($get_disk -eq $env:HOMEDRIVE.Split(":")[0]) {
# $conf.'data-root'=$null
# write-host "The disk has been changed to default.`n" -ForegroundColor cyan
#}
else {
write-host "The disk hasn't been changed.`n" -ForegroundColor cyan
}
$conf.'registry-mirrors'=@(
"http://your-docker-proxy:5000" # Add the registry address. Needed for work with proxy
)
$conf.'insecure-registries'=@(
"your-docker-proxy:5000", # Add proxy
"your-docker-registry:5000" # Add registry
)
$conf.debug=$false # Turn off debug
$conf.experimental=$false # Turn off experimental
## Add DNS
#if (-not ($conf.dns)){
# $conf | Add-Member -Name 'dns' -Value @("8.8.8.8","8.8.4.4") -MemberType NoteProperty
# }
#else {
# $conf.dns=@("8.8.8.8","8.8.4.4")
# }
# Convert back to JSON and edit the config file
$conf | ConvertTo-Json | Set-Content 'C:\ProgramData\Docker\config\daemon.json'
### Linux
# Switch Deamon to Linux mode
write-host "Setting up a Linux Daemon`n" -ForegroundColor Green
write-host "Switching Docker to the Linux Daemon...`n" -ForegroundColor Green
& 'C:\Program Files\Docker\Docker\DockerCli.exe' -SwitchLinuxEngine
write-host "In order to run Linux Containers on Windows host, the Linux Daemon uses the MobyLinuxVM located 'c:\Users\Public\Documents\Hyper-V\Virtual hard disks\' by default." -ForegroundColor cyan
write-host "But you can changed it to another disk. Just point a latter of the needed disk by the following format: 'D'." -ForegroundColor cyan
$get_disk = read-host "Enter a disk letter i.e. 'D' or just press 'Enter' to default"
if (($get_disk) -and ($get_disk -ne $env:HOMEDRIVE.Split(":")[0])) {
$confVM = Get-Content $env:APPDATA\Docker\settings.json -Raw | ConvertFrom-Json
$confVM.MobyVhdPathOverride=$get_disk + ":\DockerVM\MobyLinuxVM.vhdx"
$confVM | ConverTto-Json | Set-Content $env:APPDATA\Docker\settings.json
}
elseif ($get_disk -eq $env:HOMEDRIVE.Split(":")[0]) {
$confVM = Get-Content $env:APPDATA\Docker\settings.json -Raw | ConvertFrom-Json
$confVM.MobyVhdPathOverride=$null
$confVM | ConverTto-Json | Set-Content $env:APPDATA\Docker\settings.json
write-host "The path has been changed to default.`n" -ForegroundColor cyan
}
else {
write-host "The path hasn't been changed.`n" -ForegroundColor cyan
}
# Get content of the conig file and convert it for work
$confL = Get-Content $env:USERPROFILE\.docker\daemon.json -Raw | ConvertFrom-Json
# Add needed info
$confL.'registry-mirrors'=@(
"http://your-docker-proxy:5000"
)
$confL.'insecure-registries'=@(
"your-docker-proxy:5000",
"your-docker-registry:5000"
)
$confL.debug=$false # Turn off debug
$confL.experimental=$false # Turn off experimental
# Convert back to JSON and edit the config file
$confL | ConverTto-Json | Set-Content $env:USERPROFILE\.docker\daemon.json
write-host "Restarting Docker Services...`n" -ForegroundColor yellow
restart-service *docker*
sleep 2
get-service *docker*
write-host "`n"
write-host "Getting settings from Daemons...`n" -ForegroundColor Green
## Start any daemon after restarting the Docker Service
& 'C:\Program Files\Docker\Docker\DockerCli.exe' -SwitchLinuxEngine
## Display info
write-host "The settings for the Windows Daemon:`n" -ForegroundColor Green
write-host $(Get-Content 'C:\ProgramData\Docker\config\daemon.json' -Raw ) -ForegroundColor cyan
write-host "`n"
write-host "The settings for the Linux Daemon:`n" -ForegroundColor Green
write-host $(Get-Content $env:USERPROFILE\.docker\daemon.json -Raw ) -ForegroundColor cyan
# Check and show the current path to the MobyLinuxVM
$confVM = Get-Content $env:APPDATA\Docker\settings.json -Raw | ConvertFrom-Json
if ($confVM.MobyVhdPathOverride){
write-host "The location of the MobyLinuxVM:`n" -ForegroundColor Green
write-host $($confVM.MobyVhdPathOverride) -ForegroundColor cyan
}
sleep 2
write-host "`nDone!" -ForegroundColor Green
################################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment