Skip to content

Commit

Permalink
Merge pull request #24 from devonfw-forge/feature/rename-binaries
Browse files Browse the repository at this point in the history
Added the renaming of binaries option
  • Loading branch information
jbellver99 authored May 2, 2022
2 parents 1c7ad3c + 2dce947 commit e01aeca
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
13 changes: 10 additions & 3 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The script `install.ps1` will install the Rancher Desktop solution along with so

* Support for enterprise VPNs (flag `-VPN`).
* Support (additional) for Windows Containers using Docker binary (flag `-WindowsContainers`). You do not need to choose anymore!
* Support for usual Docker commands in Powershell and Git Bash (flag `-Alias`).
* Support for usual Docker commands in Powershell and Bash (flag `-Alias`).

To install it, clone the repository and run `install.ps1` with the desired flags as administrator.

Expand Down Expand Up @@ -50,9 +50,16 @@ volumes:
# Rancher Desktop:
- /c/Users/Joey/Desktop/backend:/var/www/html
```
=== Behind the scenes

Rancher Desktop with containerd offers `nerdctl` as CLI. Although command syntax is the same, this requires a lot of changes on existing scripts and it takes time to get used to new command naming. Also, to run Docker Windows containers you need to specify the context, which is annoying. To solve these issues, when executing the installer with `-Alias` flag, the following alias are set on default PowerShell profile:
==== Fix Docker not working when called from other applications (i.e. not using a shell). Universal `docker` command support.

In some cases, Docker commands are executed by third-party applications that rely on binary paths. Also, there are complex scenarios where the alias set on the shell profiles do not apply and existing `docker` commands fail. To solve that, you can use `-RenameBinaries` flag (instead of `-Alias`) on installation, which provides universal `docker` command support by renaming binaries names.

Please note this has the side effect of requiring you to change existing `docker-compose` commands to `docker compose`.

=== Behind the scenes

Rancher Desktop with containerd offers `nerdctl` as CLI. Although command syntax is the same, this requires a lot of changes on existing scripts and it takes time to get used to new command naming. Also, to run Docker Windows containers you need to specify the context, which is annoying. To solve these issues, when executing the installer with `-Alias` flag, the following alias are set on default PowerShell and Bash profiles:

* `docker` and `docker-compose` to `nerdctl` and `nerdctl compose` for Linux containers.
* `dockerw` and `dockerw-compose` to `docker --context win` and `docker compose --context win` for Windows containers.
Expand Down
44 changes: 37 additions & 7 deletions install.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ param(
[switch]$Help = $false,
[switch]$VPN = $false,
[switch]$WindowsContainers = $false,
[switch]$Alias = $false
[switch]$Alias = $false,
[switch]$RenameBinaries = $false
)

$script:rancherDesktopExe = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\Rancher Desktop.exe"
$script:dockerFilesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin"
$script:windowsBinariesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\win32\bin"
$script:linuxBinariesPath = "C:\Users\$env:UserName\AppData\Local\Programs\Rancher Desktop\resources\resources\linux\bin"
$script:profilePath = "C:\Users\$env:UserName\Documents\WindowsPowerShell\old-profile.ps1"
$script:panicFilePath = "C:\ProgramData\docker\panic.log"
$script:dockerPackageUrl = "https://download.docker.com/win/static/stable/x86_64/docker-20.10.8.zip"
$script:rancherDesktopUrl = "https://github.com/rancher-sandbox/rancher-desktop/releases/download/v1.1.1/Rancher.Desktop.Setup.1.1.1.exe"
Expand All @@ -32,7 +35,10 @@ function Help
Write-Host "Flags:"
Write-Host " -VPN Enables support for enterprise VPNs."
Write-Host " -WindowsContainers Enables support for Windows Containers using Docker binary."
Write-Host " -Alias Creates alias for usual Docker commands in Powershell."
Write-Host " -Alias Creates alias for usual Docker commands in Powershell and Bash."
Write-Host ""
Write-Host "Advanced Flags:"
Write-Host " -RenameBinaries Renames binaries to provide universal docker command support in cases where shell profiles are of no use, but comes with some caveats (e.g. requires using docker compose instead of docker-compose). Incompatible with -Alias flag."
Write-Host ""
}

Expand Down Expand Up @@ -73,11 +79,11 @@ function DownloadDockerD
Write-Host "Installing dockerd..." -ForegroundColor Blue
Invoke-WebRequest $script:dockerPackageUrl -OutFile "docker.zip"
Expand-Archive docker.zip -DestinationPath "C:\"
Copy-Item "C:\docker\dockerd.exe" $script:dockerFilesPath -Recurse -Force
Copy-Item "C:\docker\dockerd.exe" $script:windowsBinariesPath -Recurse -Force
Remove-Item docker.zip
Remove-Item "C:\docker" -Recurse -Force

[Environment]::SetEnvironmentVariable("Path", "$($env:path);$script:dockerFilesPath", [System.EnvironmentVariableTarget]::Machine)
[Environment]::SetEnvironmentVariable("Path", "$($env:path);$script:windowsBinariesPath", [System.EnvironmentVariableTarget]::Machine)
$env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
dockerd --register-service

Expand Down Expand Up @@ -163,7 +169,7 @@ function RestartRequired

function CopyStartScript
{
Copy-Item "start.ps1" "$script:dockerFilesPath" -Force
Copy-Item "start.ps1" "$script:windowsBinariesPath" -Force
}

function IsDockerDesktopInstalled
Expand Down Expand Up @@ -230,6 +236,18 @@ function ChangeFilePermissions
}
}

function RenameBinariesFunction
{
Write-Host "Renaming the Rancher Desktop binaries..." -ForegroundColor Blue
Rename-Item -Path "$script:windowsBinariesPath\docker.exe" -NewName dockerw.exe
Rename-Item -Path "$script:windowsBinariesPath\docker-compose.exe" -NewName dockerw-compose.exe
Copy-Item "$script:windowsBinariesPath\nerdctl.exe" "$script:windowsBinariesPath\docker.exe" -Force

Rename-Item -Path "$script:linuxBinariesPath\docker" -NewName docker.old
Copy-Item "$script:linuxBinariesPath\nerdctl" "$script:linuxBinariesPath\docker" -Force
Write-Host "Renaming done." -ForegroundColor Green
}

function SetAppDataSettings
{
if(!(Test-Path -Path $script:appDataSettingsPath))
Expand All @@ -250,6 +268,13 @@ function SetAppDataSettings

#region main

if($Alias -and $RenameBinaries)
{
Write-Host "The flags -Alias and -RenameBinaries cannot be activated together." -ForegroundColor Red
Write-Host "Please choose only one of them." -ForegroundColor Red
exit 1
}

if($Help)
{
Help
Expand Down Expand Up @@ -285,12 +310,17 @@ if($WindowsContainers)
Add-AccountToDockerAccess "$env:UserDomain\$env:UserName"
}

if($Alias)
if($Alias -and -Not($RenameBinaries))
{
CreatePowershellProfile
UpdateGitBashProfile
}

if($RenameBinaries -and -Not($Alias))
{
RenameBinariesFunction
}

Write-Host "Installation finished." -ForegroundColor Green
Write-Host -NoNewLine "Press any key to continue..."
$null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
Expand Down

0 comments on commit e01aeca

Please sign in to comment.