Cómo instalar y configurar Dockerd en Windows con WSL 2 | Guía Completa

La instalación y configuración de Dockerd en Windows puede resultar compleja si no se utilizan los métodos adecuados. En esta guía, te muestro cómo instalar Dockerd fácilmente en Windows utilizando WSL 2 mediante un script de PowerShell que automatiza todo el proceso.

🚀 Requisitos previos

  • Tener Windows 10/11 con soporte para WSL 2.
  • Permisos administrativos en Windows.

⚙️ Script de Instalación de Dockerd en Windows

Utiliza este script de PowerShell para instalar automáticamente Dockerd en Windows, configurar WSL 2 y registrar Dockerd como un servicio:

# Variables
$dockerFolder = "./docker"  # Binaries location
$installPath = "$Env:ProgramFiles\Docker"  # Installation directory
$dockerdPath = "$installPath\dockerd.exe"
$dockerServiceName = "docker"

# 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..."
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."

✅ Pasos para ejecutar el script

  1. Guarda el script en un archivo con extensión .ps1, por ejemplo, install-docker.ps1.
  2. Asegúrate de que la carpeta ./docker contenga los archivos docker.exe y dockerd.exe.
  3. Ejecuta el script en PowerShell como administrador:
Set-ExecutionPolicy Bypass -Scope Process -Force
.\install-docker.ps1

📌 Verificación final

Después de ejecutar el script, verifica la instalación con el siguiente comando en PowerShell:

docker version

De esta manera, tendrás Docker funcionando correctamente en Windows con WSL 2 configurado.