Skip to content

Instantly share code, notes, and snippets.

@au-phiware
Last active September 8, 2021 20:55
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save au-phiware/25213e72c80040f398ba to your computer and use it in GitHub Desktop.
Save au-phiware/25213e72c80040f398ba to your computer and use it in GitHub Desktop.
Powershell scripts for Docker Toolbox for Windows

Powershell scripts for Docker Toolbox for Windows

This collection of scripts is intended to be installed in the Docker Toolbox installation directory (i.e. C:\Program Files\Docker Toolbox), with exception of Docker Quickstart Terminal.lnk which is intended to replace the shortcut that Docker Toolbox places on your desktop.

There are some gotchas when working with docker in powershell:

  • The output encoding of docker commands differ to the powershell default.

    For example, when working with docker cp you will typically need to specify the encoding to Out-File:

      docker cp demo:/demo - | Out-File demo.tar oem
    

    (I have tried to set $OutputEncoding = [System.Text.Encoding]::GetEncoding("IBM437") without success.)

start.ps1

The start script is the powershell equivalent of the bash start script (start.sh), with some additional features.

  • Any shared folders added to the VM are automatically mounted.

    This allows you to expose folders that are outside of your user's home directories on your Windows host machine to the docker host machine. The name of the shared folder will be used as the moint point (relative to /).

docker-compose.ps1

The compose script allows you to execute docker-compose inside a container from your Windows host as if it were a native command.

  • The docker-compose image is automatically downloaded the first time the command is executed.
  • The current folder is mounted and set as the working directory.
  • By setting the $Env:DOCKER_COMPOSE_VERSION variable you can choose to build the image from any branch of the [docker/compose] (https://github.com/docker/compose) repository. (Default: release.)
if ($Env:DOCKER_COMPOSE_VERSION -eq $null -or $Env:DOCKER_COMPOSE_VERSION.Length -eq 0) {
$Env:DOCKER_COMPOSE_VERSION = "release"
}
if (-not $Env:DOCKER_HOST) {
docker-machine env --shell=powershell default | Invoke-Expression
if (-not $?) { exit $LastExitCode }
}
docker inspect --type=image --format='{{.Comment}}' "docker-compose-$Env:DOCKER_COMPOSE_VERSION"
if (-not $?) {
echo "Building docker-compose image..."
docker build -t "docker-compose-$Env:DOCKER_COMPOSE_VERSION" "https://github.com/docker/compose.git#$Env:DOCKER_COMPOSE_VERSION"
if (-not $?) { exit $LastExitCode }
}
$local="/$($PWD -replace '^(.):(.*)$', '"$1".ToLower()+"$2".Replace("\","/")' | Invoke-Expression)"
docker run --rm -ti -v /var/run/docker.sock:/var/run/docker.sock -v "${local}:$local" -w "$local" "docker-compose-$Env:DOCKER_COMPOSE_VERSION" $args
exit $LastExitCode
This work, by Corin Lawson, is free of known copyright restrictions.
param([string]$DOCKER_MACHINE_NAME="default")
$VirtualBox=Get-ItemProperty -Path Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Oracle\VirtualBox
$DockerMachine="docker-machine.exe"
$VirtualBoxManage=$VirtualBox.InstallDir + "VBoxManage.exe"
& $VirtualBoxManage showvminfo $DOCKER_MACHINE_NAME | Out-Null
if ($?) {
echo "Machine $DOCKER_MACHINE_NAME already exists in VirtualBox."
} else {
echo "Creating Machine $DOCKER_MACHINE_NAME in VirtualBox..."
& $DockerMachine rm -f $DOCKER_MACHINE_NAME | Out-Null
& $DockerMachine create -d virtualbox --virtualbox-memory 2048 $DOCKER_MACHINE_NAME
}
echo "Starting machine $DOCKER_MACHINE_NAME..."
& $DockerMachine start $DOCKER_MACHINE_NAME
$MountOptions = "defaults,iocharset=utf8"
$DockerPasswd = & $DockerMachine ssh $DOCKER_MACHINE_NAME "grep '^docker:' /etc/passwd"
if ($DockerPasswd.StartsWith('docker:')) {
$MountOptions = "$MountOptions,$($DockerPasswd -replace '^docker:[^:]*:(\d+):(\d+):.*$', 'uid=$1,gid=$2')"
}
$VirtualBoxMounts = & $DockerMachine ssh $DOCKER_MACHINE_NAME mount | ForEach-Object {
if ($_ -match 'on (.*) type vboxsf ') {
$Matches[1]
}
}
& $DockerMachine ssh $DOCKER_MACHINE_NAME "sudo VBoxControl sharedfolder list -automount" | ForEach-Object {
if ($_ -match '^[0-9]+ - (?<ShareName>((?<DriveLetter>[A-Za-z]):)?(?<FolderName>.*))$') {
$MountPoint = "$($Matches['DriveLetter'])$($Matches['FolderName'])"
if (-not ($MountPoint -match '^/')) {
$MountPoint = "/$MountPoint"
}
if (-not ($VirtualBoxMounts -ccontains $MountPoint)) {
echo "Mounting $($Matches['ShareName']) to $MountPoint..."
& $DockerMachine ssh $DOCKER_MACHINE_NAME "sudo mkdir -p $MountPoint && sudo mount -t vboxsf -o $MountOptions $($Matches['ShareName']) $MountPoint"
}
}
}
Remove-Variable MountOptions, DockerPasswd, VirtualBoxMounts, MountPoint
echo "Setting environment variables for machine $DOCKER_MACHINE_NAME..."
& $DockerMachine env --shell=powershell $DOCKER_MACHINE_NAME | Invoke-Expression
echo '
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
'
echo "docker is configured to use the $DOCKER_MACHINE_NAME machine at $Env:DOCKER_HOST"
@romulo1984
Copy link

You can do something to run on Git-Shell instead of powershell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment