Docker WSL 2 Windows
Docker is an open-source application container engine that packages an app with its dependencies into a portable container that runs anywhere supported. Containers are sandboxed and isolated from each other with very low overhead. This note runs Docker on Windows on top of WSL 2, from WSL setup through Docker basics and advanced operations.
Installing WSL
WSL (Windows Subsystem for Linux) runs a Linux environment on Windows and is the best foundation for Docker on Windows.The modern
wsl --install automatically enables the required Windows features and installs the default distro, so manual feature toggling is usually unnecessary. The manual step 1 below is for fine-grained control or when auto-install fails. WSL 2 only needs “Virtual Machine Platform” and “Windows Subsystem for Linux”; Hyper-V and Windows Hypervisor Platform are not required (and Hyper-V is absent on Windows Home).Enable features (optional, if auto-install fails)
Control Panel → Programs → Turn Windows features on or off, check:
- Virtual Machine Platform (required)
- Windows Subsystem for Linux (required)
- Hyper-V, Windows Hypervisor Platform (optional, not required for WSL 2)
Install WSL
Open PowerShell or Command Prompt as administrator and run:This enables the required features and installs the default Linux distro (Ubuntu). Restart afterwards.
Set up the Linux user
On first launch the new distro opens a console, waits for files to extract (faster afterwards), and prompts you to create a username and password.
Start as root (if no user was set in step 3)
If
wsl --install did not prompt for a username/password, enter as root first, then create an account manually:Tune WSL 2 resources (optional)
Edit Apply with
C:\Users\<your-username>\.wslconfig to cap WSL 2 memory and processors. The [wsl2] section header is mandatory, or the settings are ignored:wsl --shutdown to restart WSL.Installing Docker
Use Docker Desktop with the WSL 2 engine on Windows.Install Docker Desktop
Download the installer from Docker and follow the prompts; mind the system requirements.
Enable the WSL 2 engine and integration
Settings (top-right gear) → General, check “Use the WSL 2 based engine”; Resources → WSL Integration, select the distros to enable Docker integration for, then Apply & Restart.
Docker basics
Core concepts
- Image
- Container
- Dockerfile
A read-only template with the code, runtime, libraries, and config needed to run an app. Think of it as an install disc.
docker run options: container name (--name), port mapping (-p host:container), volume mount (-v host-dir:container-dir, for persistence), environment variables (-e, e.g. connection info, API keys).
Common commands
| Command | Purpose | Example |
|---|---|---|
docker run | Start a container | docker run -it --name my_ubuntu ubuntu bash |
docker build | Build an image | docker build -t my-image . |
docker ps | List running containers (-a includes stopped) | docker ps -a |
docker images | List all images | docker images |
docker stop | Stop a container | docker stop my_container |
docker rm | Remove a container | docker rm my_container |
docker rmi | Remove an image | docker rmi my-image |
docker exec | Run a command in a running container | docker exec -it my_container bash |
docker restart | Restart a container | docker restart my_container |
docker update --restart=always | Auto-restart on boot | docker update --restart=always my_container |
docker <COMMAND> --help | Show help for a command | docker run --help |
Build an image and run a container
Advanced techniques
- Custom images: use a Dockerfile to define the base image, copy files, and install specific software versions and dependencies.
- Persist data with Volumes: Docker Volumes persist container data to the host or external storage, so data survives container deletion.
- Networking: Docker offers bridge / host / overlay network modes to control inter-container communication and outbound connectivity.
- Docker Hub: Docker’s cloud image registry; pull others’ images and push your own to share.
Links
- Docker docs: docs.docker.com
- WSL docs: learn.microsoft.com/windows/wsl