Automated PowerShell script to set up Docker in WSL2 without Docker Desktop.
This script provides a complete automated installation of Docker running natively in WSL2 (Ubuntu), allowing you to use Docker from both Windows PowerShell and the Linux environment without requiring Docker Desktop.
- Installs and configures WSL2 with mirrored networking mode
- Installs Ubuntu distribution
- Installs Docker Engine in Ubuntu
- Configures Docker daemon with systemd
- Installs Docker CLI tools on Windows
- Automatically detects WSL IP and configures DOCKER_HOST
- Sets up Task Scheduler to auto-start Ubuntu at user logon
- Enables Docker commands from both Windows and WSL
- Windows 10 version 2004 or higher (Build 19041 and higher) or Windows 11
- Administrator privileges
- Internet connection
-
Open PowerShell as Administrator
- Right-click PowerShell and select "Run as Administrator"
-
Run the script
.\docker-wsl.ps1 -
Follow the prompts
- The script will pause for you to complete Ubuntu setup (username/password)
- After creating your Ubuntu username and password, type
exitto return to PowerShell - Press any key to continue the installation
-
Restart your computer
- Required for all WSL changes to take effect
-
Test Docker
docker run hello-world
The installation process includes 8 steps:
- Install WSL - Enables Windows Subsystem for Linux
- Enable Virtual Machine Platform - Required for WSL2
- Update WSL - Gets the latest WSL version
- Set WSL 2 as default and configure networking - Ensures new distributions use WSL2 and sets up mirrored networking mode
- Install Ubuntu - Installs Ubuntu distribution from Windows Store
- Update Ubuntu and Install Docker - Updates Ubuntu packages and installs Docker Engine, CLI, containerd, and Docker Compose plugin
- Install Windows Docker tools - Installs Docker CLI, Compose, and Buildx on Windows, and configures DOCKER_HOST with WSL IP
- Create Task Scheduler job - Sets up automatic Ubuntu startup at user logon to ensure the WSL distribution stays running (required for Docker to remain accessible from Windows)
- WSL networking is configured in mirrored mode for better network compatibility
- Docker Engine runs inside WSL Ubuntu as a systemd service
- Docker CLI on Windows connects to the WSL Docker daemon via localhost (127.0.0.1:2375) in mirrored networking mode
- Task Scheduler automatically starts Ubuntu at user logon and keeps it running in the background
- WSL distributions automatically shut down when idle to save resources
- The scheduled task runs a background process (
sleep infinity) to prevent Ubuntu from stopping - This ensures Docker remains accessible from Windows PowerShell at all times
- Without this task, you would need to manually start WSL before using Docker commands
- You can use
dockercommands from both PowerShell and Ubuntu terminal - The
DOCKER_HOSTenvironment variable is automatically set totcp://127.0.0.1:2375for mirrored networking mode
After installation, you can use Docker commands from any PowerShell or terminal window:
# Pull and run containers
docker pull nginx
docker run -d -p 8080:80 nginx
# Use Docker Compose
docker compose up -d
# Check Docker status
docker ps
docker imagesFrom within Ubuntu WSL:
# Same Docker commands work
docker ps
sudo systemctl status dockerFor a web-based Docker management interface, we recommend using Portainer. Portainer provides an intuitive UI to manage containers, images, volumes, networks, and more.
# Create a volume for Portainer data
docker volume create portainer_data
# Run Portainer container
docker run -d `
--name portainer `
-p 9000:9000 `
-p 9443:9443 `
--restart always `
-v /var/run/docker.sock:/var/run/docker.sock `
-v portainer_data:/data `
portainer/portainer-ce:latest- Open your browser and navigate to:
http://localhost:9000orhttps://localhost:9443 - Create your admin account on first launch
- Select "Docker" as the environment to manage
- Start managing your Docker environment through the web interface
- Visual container management (start, stop, restart, remove)
- Image management and registry connections
- Volume and network management
- Docker Compose stack deployment
- Container logs and stats viewing
- Terminal access to containers
# Check if Ubuntu is running
wsl -l -v
# Check if the scheduled task is running
Get-ScheduledTask -TaskName "WSL-Ubuntu-Startup" | Get-ScheduledTaskInfo
# If the task is not running, start it
Start-ScheduledTask -TaskName "WSL-Ubuntu-Startup"
# Alternative: If Ubuntu is not running (State: Stopped),
# you can start it manually instead of using the scheduled task
wsl -d Ubuntuwsl -d Ubuntu sudo systemctl start dockerwsl -d Ubuntu sudo systemctl status dockerwsl -d Ubuntu sudo systemctl restart dockerecho $env:DOCKER_HOST
# Should output: tcp://127.0.0.1:2375If Docker commands fail to connect, try these alternatives:
For mirrored networking mode (default in this script):
# Try IPv4 localhost
[Environment]::SetEnvironmentVariable("DOCKER_HOST", "tcp://127.0.0.1:2375", "User")
# Try IPv6 localhost
[Environment]::SetEnvironmentVariable("DOCKER_HOST", "tcp://[::1]:2375", "User")For NAT networking mode (if you changed from mirrored):
# Get the WSL IP address and set DOCKER_HOST
$wslIp = wsl -d Ubuntu hostname -I | ForEach-Object { $_.Trim().Split()[0] }
[Environment]::SetEnvironmentVariable("DOCKER_HOST", "tcp://${wslIp}:2375", "User")After changing DOCKER_HOST, restart your PowerShell session for the changes to take effect.
Test without changing environment variable:
You can test different host addresses without modifying DOCKER_HOST using the -H option:
# Test with IPv4 localhost
docker -H tcp://127.0.0.1:2375 ps
# Test with IPv6 localhost
docker -H tcp://[::1]:2375 ps
# Test with WSL IP (for NAT mode)
$wslIp = wsl -d Ubuntu hostname -I | ForEach-Object { $_.Trim().Split()[0] }
docker -H tcp://${wslIp}:2375 ps# Test if Docker API is responding (mirrored networking uses localhost)
curl http://localhost:2375/versionwsl --shutdown
wsltcp://0.0.0.0:2375 without TLS encryption. This is suitable for local development but exposes Docker to your local network.
For production use, consider:
- Using Unix socket only
- Enabling TLS authentication
- Restricting network access with firewall rules
To remove Docker:
# Remove Docker CLI tools from Windows (run in PowerShell as Administrator)
winget uninstall Docker.DockerCLI
winget uninstall Docker.DockerCompose
winget uninstall Docker.Buildx
# Remove DOCKER_HOST environment variable
[Environment]::SetEnvironmentVariable("DOCKER_HOST", $null, "User")
# Remove Task Scheduler job
Unregister-ScheduledTask -TaskName "WSL-Ubuntu-Startup" -Confirm:$false
# Optionally remove Ubuntu distribution
wsl --unregister Ubuntu
# Optionally remove .wslconfig file
Remove-Item "$env:USERPROFILE\.wslconfig" -ForceMIT License - See LICENSE file for details
Contributions are welcome! Please feel free to submit issues or pull requests.