Build Java development environment with WSL2 Docker VS Code

Build Java development environment with WSL2 Docker VS Code

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.

Installation of WSL2

Enable the Windows subsystem for Linux

Open PowerShell as an administrator and do the following:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

Enable virtual machine functionality

Open PowerShell as an administrator and do the following:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

Reboot the machine.

Download the Linux kernel update package

  1. Download and install the latest package. WSL2 Linux Kernel Update Package for x64 Machines (https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi)

Set WSL 2 as the default version

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

Install the selected Linux distribution

  1. Open the Microsoft Store (https://aka.ms/wslstore), search for and select the desired Linux distribution.
  2. On the distribution page, select Get. Get Ubuntu 20.04 LTS here.
  3. Start Ubuntu 20.04LTS and enter your user name and password.
  4. Update Ubuntu with the command sudo apt-get update.

Check the WSL folder

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/

Limit WSL resources

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

Docker installation / configuration

Installation

Install Docker Desktop Check Enable WSL 2 Windows Features during installation

Configuration

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.

VS Code settings

Setting environment variables

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

Extension installation

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.

Launch VS Code

--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 .

Creating a Remote container

Press ctrl + shift + p and Enter remote-containers: Open Folder in Container

The following prompts will be displayed, so make each selection.

  1. Select a folder
  2. Select Docker image → select java
  3. Select Java version → Select 11
  4. Select build tool → select maven gradle node as you like

Creating a Java project

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/

Supplement about Remote-Container

When using Remote-Container The .devcontainer folder is created, and the following two files are created there.

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

Build Java development environment with WSL2 Docker VS Code
Build a Java development environment with VS Code
Build Java development environment with VS Code on Mac
How to build Java development environment with VS Code
[Environment construction] Build a Java development environment with VS Code!
Prepare Java development environment with VS Code
Build docker environment with WSL
Build VS Code + WSL + Java + Gradle environment from scratch
Java build with mac vs code
Java development environment (Mac, VS Code)
Build Java program development environment with Visual Studio Code
Java web application development environment construction with VS Code (struts2)
Build WebAPP development environment with Java + Spring with Visual Studio Code
Build a Wordpress development environment with Docker
Until you build a Nuxt.js development environment with Docker and touch it with VS Code
Build Go development environment with WSL2 + Docker Desktop + VSCode (Remote --Containers)
Build a WordPress development environment quickly with Docker
Using Gradle with VS Code, build Java → run
Build a development environment for Docker, java, vscode
[First team development ②] Build an environment with Docker
Docker management with VS Code
Introduction to Java development environment & Spring Boot application created with VS Code
[Mac] VS Code development environment construction (Java, Gradle, Node.js)
Build ruby debug environment with VS Code of Windows 10
Try to build a Java development environment using Docker
Create Chisel development environment with Windows10 + WSL2 + VScode + Docker
Prepare Java development environment with VS Code
Build Couchbase local environment with Docker
Build a Node.js environment with Docker
Java Spring environment in vs Code
Prepare Java development environment with Atom
Build PlantUML environment with VSCode + Docker
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build Java development environment (for Mac)
Build jooby development environment with Eclipse
Getting Started with Docker with VS Code
Build Unity development environment on docker
Build docker + laravel environment with laradock
How to build Rails, Postgres, ElasticSearch development environment with Docker
[Note] A story about changing Java build tools with VS Code
Create Spring Boot-gradle-mysql development environment with Docker
Java development environment
Build a Java development environment on Mac
Let me do VS Code Remote Development + Java development in Proxy environment
Build Java 8 development environment on AWS Cloud9
Spring Boot + Docker Java development environment construction
Building a haskell environment with Docker + VS Code on Windows 10 Home
Build Redmine code reading environment on Docker
Build a development environment for Django + MySQL + nginx with Docker Compose
[Docker] Build Jupyter Lab execution environment with Docker
Build an environment with Docker on AWS
Build an Ultra96v2 development environment on Docker 1
Lightweight PHP 7.4 development environment created with Docker
Build TensorFlow operation check environment with Docker
How to build Rails 6 environment with Docker
Build a simple Docker + Django development environment
[Be careful about changing the version of Xdebug! ] Create a development environment with Xdebug3 + docker + VS Code
Build debug environment on container --Build local development environment for Rails tutorial with Docker-
A record of setting up a Java development environment with Visual Studio Code
Build an environment of "API development + API verification using Swagger UI" with Docker