User Tools

Site Tools


docker:install-docker-for-windows-wsl

Install Docker in WSL and use it on Windows

First, install WSL for windows : search from start menu for “Turn Windows features on or off” and select “Windows Subsystem for linux

Next, install ubuntu:

wsl --set-default-version 2
wsl --install -d Ubuntu

Go in Ubuntu:

wsl -l -v
wsl -d Ubuntu

From there, install docker:

sudo apt-get update
 
sudo apt-get -y install apt-transport-https ca-certificates curl gnupg lsb-release
 
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg 
 
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 
sudo apt-get update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io
sudo groupadd docker
sudo usermod -aG docker $USER

To start:

sudo service docker start

To automatically start at startup, add at the end of the file:

vi ~/.bashrc
if (! pgrep dockerd > /dev/null); then
    sudo service docker start
fi

And also in order to not require a password for the above command, add to the end:

sudo visudo
your-username ALL=(ALL) NOPASSWD: /usr/sbin/service docker start

Now you can run wsl docker commands.
To get rid of the wsl prefix save this as docker.bat in C:\Windows:

docker.bat
@echo off
wsl docker %*

Automatically Start Ubuntu WSL at Startup without Showing CMD

Open Notepad and create a batch script with the following command:

start_wsl.bat
@echo off
wsl -d Ubuntu

Save the file as `start_wsl.bat` in the `C:\Windows` directory.

Create a shortcut to the batch file. Right-click on the `start_wsl.bat` file, and choose Create Shortcut.
Next, set the shortcut to run minimized:

  1. Right-click the newly created shortcut, and select Properties.
  2. In the Shortcut tab, set Run to Minimized.
  3. Click OK to save the settings.

Then, move the shortcut to the Windows Startup folder:

  1. Press *Win + R*, and type `shell:startup`, then hit Enter. This opens the Startup folder.
  2. Move the shortcut you created into this folder.

Now, WSL (Ubuntu) will automatically start whenever Windows boots up, without showing the CMD window.

docker/install-docker-for-windows-wsl.txt · Last modified: 2024/09/07 01:44 by odefta