
# WSL and Docker under Windows
If you haven't previously done it already, uninstall `Docker Desktop on Windows` from your Windows machine. This was previously being used to provide docker daemon to subsystem containers, but since the licensing issues, it's no longer allowed to be used for free.
## Install Ubuntu subsystem for Windows
* Enable Windows features for virtualization and sub-system:

* Download and install windows kernel update for WSL 2
https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
* Open up Microsoft Store, and search for Ubuntu. Navigate to `Ubuntu 20.04`, and install it.

After opening the application enter Unix username and password by choice.
(do not forget it :D). afterwards, type `exit` and close the window.
* Additional app we are going to download from Microsoft Store is `Windows Terminal`

## Setting UP WSL (Subsystem for Linux)
* Open up Windows Terminal. This should be already set by default, but just in case type command `wsl --set-default-version 2`. Running the command `wsl --list --verbose` , you should see that `Ubuntu-20.04` is set to run under version 2. In case it's showing version 1, you need to migrate it, running the following command: `wsl --set-version Ubuntu-20.04 2`
* Now we will login into the subsystem, by typing only the `wsl` command from Windows Shell. Type command `sudo apt-get update` to test sudo command and also update the package lists (something linux related).
### Setting up SSH keys
We need to create SSH keys for the WSL machine. We can do it by generating new keys under wsl/linux or copying it from a Windows machine. Run command `ssh-keygen` and keep pressing enter for all prompts. To print out generated key, type `cat ~/.ssh/id_rsa.pub`
To use existing Windows keyes, we will override them from a Windows machine. Assuming you are still in the initial folder (something like `wslusername@hostusername:/mnt/c/Users/hostusername`), run the following commands:
`sudo cp .ssh/id_rsa /home/{wslusername}/.ssh/`
`sudo cp .ssh/id_rsa.pub /home/{wslusername}/.ssh/`
Now we will navigate to WSL home directory:
`cd ~`. Crate projects folder with `mkdir projects`. then `cd projects`.
we can now test SSH key with command `git clone git@git.vegaitsourcing.rs:petar.petkovic/lambda-food.git`
### Installing Docker and Docker Compose
Inside WSL, run following commands:
```
sudo apt-get install \
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 install docker-ce docker-ce-cli containerd.io`
`sudo usermod -aG docker $USER`
Now, the command `docker run hello-world
` will fail since we don't have docker daemon running.
The fastest workaround is to run command `sudo dockerd`, after daemon has completed initialization, just close that terminal. the process will keep running in background and will keep docker daemon running. We need to run this command every time inside WSL after the system is rebooted or WSL restarted. Running `docker run hello-world` again inside WSL, should work as expected.
Also, it's a bit annoying inside wsl to enter sudo password every time we run sudo command, so what you could do is run `sudo visudo`, and append `{wslusername} ALL=(ALL) NOPASSWD: ALL` inside the file. CTRL+X and select Yes to save.
Install docker compose with following commands:
```
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```
`sudo chmod +x /usr/local/bin/docker-compose`
## Working with docker
Its recommended to keep all the projects in /home/{user}/projects folder, since things will work much faster if folders are not mounted from Windows file system
in VSCODE, install following extensions: Remote - WSL PHP Debug and Docker.
With Docker, we can do all sorts of automatization steps using Makefile. Below are examples for VSCODE
Open Windows Terminal and type `wsl`, then `cd ~/projects/lambda-food/`.
Type `code .` to open lambda-food project with VSCODE under WSL.Go through all extensions and install then in WSL, clicking on green icon `Install in WSL:Ubuntu...`
Now, open up terminal in VSCODE.

Clicking on bottom left WSl icon, and then show log, will open up the WSL log. Copy and save ip address outlined above, it will be important latter when we are setting up XDEBUG
Create `.vscode` folder, then create `launch.json` file inside.
```
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"pathMappings": {
"/var/www": "${workspaceRoot}",
},
"port": 9000,
"log": true,
"externalConsole": false,
"hostname": "{WSLIP}"
}
]
}
```
type `code .docker/local/php/xdebug.ini`
replace xdebug.client_host ip with the same `WSLIP`
This file is not inside gitignore, so try not to commit this file when pushing. this is a fast workaround, working on finding more elegant solution to dynamically set this IP.
Inside WSL we also need to install Make package:
`sudo apt-get install make`
Now, you can type `make dockerize`, and you should be able to start the project.
## Optimization Steps
In windows under C:\Users\\{windowsuser} create file `.wslconfig` and paste following parameters:
```
[wsl2]
memory=6GB
swap=0
localhostForwarding=true
```
## Troubleshooting
### Restarting WSL
Run `wsl.exe --shutdown` inside Windows Terminal. then start it again with the `wsl` command.