Created
July 2, 2025 02:13
-
-
Save nanox/c82affa1c89a7380c1000fe2690c04aa to your computer and use it in GitHub Desktop.
How to Setup Docker on Windows Server
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
# Variables | |
$dockerFolder = ".\docker" # Binaries location | |
$installPath = "$env:ProgramFiles\Docker" # Installation directory | |
$dockerdPath = "$installPath\dockerd.exe" | |
$dockerServiceName = "docker" | |
$paths = $env:psmodulePath.Split(';') | |
$modulePath = Join-Path $paths[0] "DockerMsftProvider" | |
if (!(Test-Path $modulePath)) { | |
New-Item -Path $modulePath -ItemType Directory | |
} | |
$outfile = Join-Path $modulePath 'DockerMsftProvider.psm1' | |
Invoke-WebRequest -UseBasicParsing -OutFile $outfile -Uri https://raw.githubusercontent.com/ajkauffmann/MicrosoftDockerProvider/master/DockerMsftProvider.psm1 | |
$outfile = Join-Path $modulePath 'DockerMsftProvider.psd1' | |
Invoke-WebRequest -UseBasicParsing -OutFile $outfile https://raw.githubusercontent.com/ajkauffmann/MicrosoftDockerProvider/master/DockerMsftProvider.psd1 | |
Install-Package Docker -ProviderName DockerMsftProvider -Force | |
# Check if WSL 2 is enabled | |
try { | |
$wslVersion = wsl --list --verbose 2>&1 | |
if ($wslVersion -match "Windows Subsystem for Linux has no installed distributions.") { | |
Write-Host "WSL is not installed. Installing WSL 2..." | |
# Install WSL and set default version to 2 | |
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart | |
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart | |
wsl --set-default-version 2 | |
# Install Ubuntu from Microsoft Store using WSL command | |
Write-Host "Installing Ubuntu from Microsoft Store..." | |
wsl --install -d Ubuntu-20.04 | |
Write-Host "Ubuntu installed. Please configure it and try again." | |
exit 1 | |
} else { | |
Write-Host "WSL 2 is already installed and set as the default version." | |
} | |
} catch { | |
Write-Error "Failed to check WSL version. Please ensure that WSL is installed and try again." | |
exit 1 | |
} | |
# Verify if the docker binaries exist | |
if (-Not (Test-Path "$dockerFolder\docker.exe") -or -Not (Test-Path "$dockerFolder\dockerd.exe")) { | |
Write-Error "docker.exe or dockerd.exe not found in the '$dockerFolder' folder." | |
exit 1 | |
} | |
# Create installation directory if it doesn't exist | |
if (-Not (Test-Path $installPath)) { | |
New-Item -Path $installPath -ItemType Directory -Force | |
} | |
# Copy binaries to the installation directory | |
Write-Host "Copying binaries to $installPath..." | |
Copy-Item -Path "$dockerFolder\*" -Destination $installPath -Force | |
# Register Docker as a service | |
Write-Host "Registering Docker as a service..." | |
& $dockerdPath --register-service | |
# Set the service to start automatically | |
Write-Host "Configuring the service to start automatically..." | |
Set-Service -Name $dockerServiceName -StartupType Automatic | |
# Try to start the Docker service | |
Write-Host "Starting Docker service..." | |
if ($restartNeeded) { | |
Write-Host 'A restart is needed to finish the installation' -ForegroundColor Green | |
If ((Read-Host 'Do you want to restart now? [Y/N]') -eq 'Y') { | |
Restart-Computer | |
} | |
} else { | |
Start-Service -Name $dockerServiceName | |
} | |
# Check if Docker started successfully | |
$serviceStatus = Get-Service -Name $dockerServiceName | |
if ($serviceStatus.Status -eq 'Running') { | |
Write-Host "Docker service started successfully." | |
} else { | |
Write-Error "Failed to start the Docker service." | |
} | |
# Verify Docker installation by running 'docker version' | |
Write-Host "Verifying the installation..." | |
& "$installPath\docker.exe" version | |
Write-Host "Docker installed and configured successfully." | |
& "$installPath\docker.exe" info |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment