Skip to content

Instantly share code, notes, and snippets.

@AviDuda
Created October 13, 2016 15:56
Show Gist options
  • Save AviDuda/6d56c6ea3fa569c39958e45f00145510 to your computer and use it in GitHub Desktop.
Save AviDuda/6d56c6ea3fa569c39958e45f00145510 to your computer and use it in GitHub Desktop.
Docker Toolbox startup helper for Windows
# This script will hopefully make using Docker Toolbox on Windows a lot easier.
# There's Docker for Windows, but you need to live with Docker Toolbox if you have Windows 10 Home or an older version of Windows.
#
# The script checks if your shared folders are set up correctly in VirtualBox, starts the Docker VM,
# mounts the shared folders, and sets the environment in your current shell.
#
# This script assumes you have VBoxManage.exe in your PATH.
#
# License: MIT
$Config = @{
# Which Docker machine should this script use?
Machine = "default";
# Shared folders config
SharedFolders = @(
@{
Name = "web";
HostPath = "C:\web";
GuestDirectory = "/var/www";
ReadOnly = $False;
AutoMount = $True;
Permanent = $True;
}
);
}
function Log {
Write-Host "[docker-start] " -ForegroundColor Yellow -NoNewline
Write-Host $args
}
function Bailout {
$Command = $args -join " "
Write-Host $Command
Invoke-Expression $Command
if ($LastExitCode -ne 0) {
Log "Last command returned an error code $LastExitCode. Interrupting."
exit
}
}
# Find out if the shared folder is already mapped to the VM
foreach ($Folder in $Config.SharedFolders) {
$FolderPattern = 'SharedFolderNameMachineMapping\d+="'
$FolderPattern += $Folder.Name
$FolderPattern += '"'
$FolderMatch = VBoxManage showvminfo $Config.Machine --machinereadable | Select-String -pattern $FolderPattern
if ($FolderMatch -eq $Null) {
Log Stopping machine $Config.Machine
docker-machine stop $Config.Machine
Log Creating shared folder $Folder.Name
$Params = New-Object System.Collections.ArrayList
if ($Folder.ReadOnly -eq $True) { [void] $Params.Add("--readonly") }
if ($Folder.AutoMount -eq $True) { [void] $Params.Add("--automount") }
if ($Folder.Permanent -eq $False) { [void] $Params.Add("--transient") }
$OutParams = $Params -join " "
Bailout VBoxManage sharedfolder add $Config.Machine --name $Folder.Name --hostpath $Folder.HostPath $outparams
}
}
Log Starting the machine $Config.Machine...
Bailout docker-machine restart $Config.Machine
# Docker is sometimes complaining about certs, if it happens to you, regenerate them with this.
# Log Regenerating certs...
# Bailout docker-machine regenerate-certs --force $Config.Machine
foreach ($Folder in $Config.SharedFolders) {
Log Adding shared folder $Folder.Name ($Folder.HostPath) to $Folder.GuestDirectory
docker-machine ssh $Config.Machine sudo mkdir -p $Folder.GuestDirectory
docker-machine ssh $Config.Machine sudo mount -t vboxsf -o defaults,uid=``id -u docker``,gid=``id -g docker`` $Folder.Name $Folder.GuestDirectory
}
docker-machine env | Invoke-Expression
Log Docker is ready!
$IP = docker-machine ip $Config.Machine
Log IP address: $IP
Log Environment is set in this shell. If you want to invoke Docker from anywhere else, type this command:
Log "docker-machine env | Invoke-Expression"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment