Skip to content

Simple guide to using Docker on Windows 10 and access from WSL 2

Introduction

For a heavy linux user like me, using windows also mean find a door to work with a linux distro. There are several options to gauge from the situation. I usually need both docker and wsl2 on the computer. And i've installed this many times.

docker-logo

So, this is a very straight forward tutorial on docker and wsl2 installation and configuration on windows10 or 11.

Prerequisites

  • Windows 10 Pro, Enterprise, or Education edition.
  • WSL 2 enabled on your Windows machine.
  • Docker Desktop for Windows installed.

Step-by-Step Guide

  1. Install Docker Desktop for Windows:

  2. Enable WSL 2:

    Enable docker for wsl2

  3. Set wsl default version to 2:

    • Open PowerShell as an administrator and run the following command:
    wsl --set-default-version 2
    
  1. Install a Linux Distribution:

    • Open the Microsoft Store app on your Windows machine.
    • Search for a Linux distribution (e.g., Ubuntu, Debian, or Alpine) and install it.
    wsl installer

  2. Configure WSL 2 as the Default WSL Version:

    • see the list of distro you have
    wsl.exe -l -v
    
    • Open PowerShell as an administrator and run the following command:
    wsl --set-version <distribution_name> 2
    

    Replace <distribution_name> with the name of the Linux distribution you installed.

  3. Start the Linux Distribution:

    • Launch the Linux distribution you installed from the Start menu or by running its executable.
    • Follow the initial setup instructions to create a user account and set a password.
  4. Add the Docker host configuration:

    • Scroll to the end of the file using the arrow keys.
    • Add the following line:
    echo "export DOCKER_HOST=unix:///var/run/docker.sock" >> ~/.bashrc
    
    • I didn't need this one but who knows:
    # open the bashrc
    nano ~/.bashrc
    # add this line in the file
    export DOCKER_HOST=tcp://localhost:2375
    
  5. Reload the updated .bashrc file:

    • Run the following command to apply the changes:
    source ~/.bashrc
    
  6. Verify Docker connectivity:

    • Run the following command to check if the Docker client in WSL can connect to Docker Desktop:
    docker version
    
  7. Enable WSL Integration in Docker Desktop:

    • Open Docker Desktop on your Windows machine.
    • Right-click on the Docker Desktop icon in the system tray (notification area) and select "Settings".
    • In the "Settings" window, navigate to the "Resources" section.
    • Click on "WSL Integration" in the left sidebar.
    • Toggle the switch next to your WSL distribution (e.g., Ubuntu) to enable integration.
    • Click "Apply & Restart" to save the changes and restart Docker Desktop.
  8. Verify Docker Connectivity in WSL:

    • Open the WSL terminal (Ubuntu).
    • Run the following command to check Docker connectivity:
    docker version
    

That's it! You should now have Docker successfully configured and running on Windows 10 with WSL 2. You can now use Docker commands within your Ubuntu WSL terminal to manage containers, images, and other Docker resources.

If you encounter any issues or have further questions, feel free to ask. Happy Dockerizing!

basic commands for working with wsl 2

  • install wsl: wsl --install
  • check list of distro (name and version): wsl.exe -l -v
  • change from v1 to v2: wsl.exe --set-version Ubuntu 2 1
  • unsinstall the wsl: wsl.exe --unregister Ubuntu-22.04 where Ubuntu-22.04 is my distro name i got
  • see images docker images