Creating a development environment with WSL2, Docker and VS Code was a hot topic, so I tried it. Although I made it, the operation of the development environment was too heavy and I got stuck. The reason was that the development source was in the Windows folder instead of the lightweight Linux folder. I will leave the procedure as a commandment to myself.
Open PowerShell as an administrator and do the following:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
Open PowerShell as an administrator and do the following:
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Reboot the machine.
Open PowerShell and run the following command to set WSL 2 as the default version when installing a new Linux distribution.
wsl --set-default-version 2
sudo apt-get update
.You can access the WSL Linux file system from Windows by entering the following path in Explorer.
\\wsl$
For Ubuntu 20.04LTS:
\\wsl$\Ubuntu-20.04
When developing using WSL + Docker, place the source etc. under this.
Also, if you browse the Windows folder from WSL, you can see from the WSL terminal. It will be as follows.
/mnt/c/
WSL seems to reserve 80% of the host machine's physical memory by default,
Change the settings as it may cause the host to run out of memory.
Describe the settings in the following file (or create a new one if there is none).
%USERPROFILE%\.wslconfig
The description is as follows.
[wsl2]
memory=4GB
swap=0
processors=2
See WSL Command and Startup Configuration (https://docs.microsoft.com/ja-jp/windows/wsl/wsl-config#configuration-options) for more information.
Restart WSL for the settings to take effect.
Execute the following command from PowerShell and restart the WSL terminal to restart it.
wsl --shutdown
To check the WSL that is running, execute the following command in PowerShell.
wsl -l -v
Install Docker Desktop
Check Enable WSL 2 Windows Features
during installation
Launch Docker Desktop, right-click the Docker icon on the taskbar and launch setteings
--General has a check mark on Use the WSL 2 based engine
--In Resources, Enable integration with my default WSL distro
is checked.
wsl -l
command in PowerShell.If you set the VScode bin folder in the Windows environment variable PATH as shown below
You will be able to start VS Code with a command from WSL.
C:\app\VSCode\bin
Install Remote Development.
Remote Development includes multiple extensions, This time we will use Remote --Container in particular. Other extensions such as the following are also installed.
--Start Windows Terminal or Ubuntu Terminal
--Run the command cd ~
to move to your home folder
--Check the home folder path with the command pwd
On WSL it will be / home/username
.
On Windows it will be \\ wsl $ \ Ubuntu-20.04 \ home \ username
.
--Create and move any folder
mkdir dev_root
cd dev_root
--Launch VsCode
code .
Press ctrl + shift + p
and
Enter remote-containers: Open Folder in Container
The following prompts will be displayed, so make each selection.
From VsCode terminal Create a project template with spring boot initial izr
curl https://start.spring.io/starter.zip \
-d dependencies=web \
-d javaVersion=11 \
-o demo.zip
Unzip the project template
unzip demo.zip
Edit DemoApplication.java as follows
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/")
public String index(){
return "hello World";
}
}
Press the F5
key to debug and access http://localhost:8080/
When using Remote-Container
The .devcontainer
folder is created, and the following two files are created there.
devcontainer.json Here you can specify the Docker file or Docker Compose file to build, You can describe the VsCode settings of the container (java.home, etc.) and the extensions you want to install (spring-boot-extention-pack, etc.).
Dockerfile It will be a Docker file for building the container. This time I used the Docker image provided by the extension (MS), You can change it to your favorite Docker image uploaded to Docker Hub etc.
This article was written with reference to the following information.
-Installation Guide for Windows Subsystem for Linux for Windows 10 -Learn more about memory management in Windows Subsystem for Linux 2
Recommended Posts