Run a MicroProfile Microservice on OpenLiberty in a Remote development container in Visual Studio Code

In this blog post I want to show, how to setup a local remote Java development container for Eclipse MicroProfile with OpenLiberty in Visual Studio Code.

I did that for the Authors microservice from the Cloud Native Starter project with MicroProfile 3.2, OpenJDK Java 11, and the latest OpenLiberty version.

That blog post is structured in:

  • Setup and configuration of Visual Studio Code
  • Run the Authors microservice in the remote development container
  • Debug the Authors microservice in the remote development container

The documentation of remote development with Video Studio Code is here and Java Dev Environments with Containers is a great blog post, which does also cover the remote development with container.


1. Setup and configuration

Ensure you have installed Docker Desktop on your local machine.

Step 1: Install the following extensions in Visual Studio Code

Step 2: Open in your Visual Studio Code  workspace the Remote containers extension window

vsc-remote-java-dev-00

Step 3: Click the option “Add Development Container Configuration Files”

vsc-remote-java-dev-00.1

Step 4: Search for Java 11 an select “openjdk-11-jdk” Dockerfile

Step 5: Verify the newly created folders and files

vsc-remote-java-dev-01

Step 6: Configure the devcontainer.json file. The gif shows the relevant folder and files for the remote container extension.

vsc-remote-java-dev-01

For the Authors microservice I need to configure a port forward configuration “forwardPorts”: [8080,3000,7777]

  • Port 8080: as the default port for the development container
  • Port 3000: is the port we use for the microservice on the OpenLiberty server
  • Port 7777: is the remote debugging port for the OpenLiberty server
{

"name": "Java 11",
"dockerFile": "Dockerfile",

// Set *default* container specific settings.json values on container create.
"settings": {
"terminal.integrated.shell.linux": "/bin/bash",
"java.home": "/docker-java-home"
},
...
// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8080,3000,7777],
....

}

Step 7: Configure the created Dockerfile to enable Maven in the container, therefore uncomment the section “install the Maven CLI Tools”

Step 8: Now open the local folder of the workspace in the remote development container. That will map the folder as volume to the development container and code changes will be saved on your local file system.

vsc-remote-java-dev-02
Step 9: Verify that you can access the files. The images displays the verification.
vsc-remote-java-dev-03

2. Run the Authors microservice in the development container

The pom.xml file for the Authors microservice contains the settings to use the OpenLiberty mavenplugin.
<plugins>
  <!-- Enable liberty-maven plugin -->
  <!-- tag::libertyMavenPlugin[] -->
  <plugin>
   <groupId>io.openliberty.tools</groupId>
   <artifactId>liberty-maven-plugin</artifactId>
   <version>3.1</version>
   <!-- tag::libertyMavenConfiguration[] -->
   <configuration>
     <serverName>authorsDevServerOnContainer</serverName>
     <configFile>liberty/server-container.xml</configFile>
    </configuration>
   <!-- end::libertyMavenConfiguration[] -->
  </plugin>
  <!-- end::libertyMavenPlugin[] -->
  <!-- Enable liberty-maven-plugin -->
</plugins>
Step 1: Clean the Java project with mvn package clean 
Step 2: Start the OpenLiberty server with mvn liberty:run. The gifs shows the start of the server.
vsc-remote-java-dev-02
Step 3: Open the Authors OpenAPI explorer in the browser http://localhost:3000/openapi/ui and try out the api.

3. Debug the Authors microservice in the development container

Verify following settings in the .vscode/launch.json file. That file contains the settings to connect the OpenLiberty debugging port 7777 of the development server. The image displays the file.
vsc-remote-java-dev-04

Step 1: Create a break point in one of the classes of the Authors microservice

Step 2: Start the server with mvn liberty:dev

Step 3: Open the Authors mircoservice in a browser http://localhost:3000/openapi/ui

Step 4: Start the debugging with the configuration Debug (Attach) – Docker Remote 

The two gifs are showing the 4 steps

  • Start OpenLiberty in the development mode

gif (1)

  • Debug the Authors microservice

gif (2)

I hope this was useful for you and let’s see what’s next?

Greetings,

Thomas

PS:  My GitHub project with the source code is here.

#Docker, #Container, #Java, #VisualStudioCode, #RemoteDebug, #OpenLiberty, #RemoteDevelopment

 

One thought on “Run a MicroProfile Microservice on OpenLiberty in a Remote development container in Visual Studio Code

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: