VSCode Remote Container: SpringBoot Config (Part1)

Ragunath Rajasekaran
8 min readJan 19, 2022

--

In this blog, we look at how we can configure Docker along with VSCode Remote Container to provide a sophisticated SpringBoot development environment.

Photo by Brett Jordan on Unsplash

In my previous blog, we explored why we need VSCode Remote Container and how a developer can leverage it to make their lives easier.

In this blog, we can explore how to optimise the SpringBoot Development Environment with the help of Docker and VSCode Remote Container.

What we will cover in this tutorial?

  • What is the pre-requisite for VSCode remote containers?
  • Steps to configure VSCode Remote Container
  • Running SpringBoot app inside VSCode Remote Containers

What is the pre-requisite for VSCode remote containers?

  • Docker: All necessary tools and softwares will be configured in a docker container. If you are aware of the docker installation, feel free to follow the instructions and install it on the local machine. Those who are new can make use of this link to download and follow the instructions to install it on the local machine.
  • VSCode: VSCode IDE
  • VSCode Remote Container Extension: We are going to use Remote Containers to interact with the infrastructure created inside docker containers.
  • SpringBoot application example: Feel free to use any of the SpringBoot applications without any external dependencies (database, messaging protocols, etc.).

Steps to configure VSCode Remote Container

Steps Involves

  • Step 0: Install Docker, VSCode
  • Step 1: Checkout SpringBoot Source code
  • Step 2: .devcontainer folder
  • Step 3: Dockerfile Configuration
    3.1 Dockerfile
    3.2 Base Image
    3.3 What's inside Base Image
    3.4 vscode user accounts
    3.5 Add Gradle to Docker
  • Step 4: Build Docker Image
  • Step 5: devcontainer.json configuration
  • Step 6: Open In VSCode Container
  • Step 6: Open SpringBoot and run and verify

Step 0: Install Docker, VSCode

Install docker on your local machine and ensure that docker is running. VSCode should have the Remote Containers extension installed.

Step 1: Checkout SpringBoot source code

Please check out the em-api repo's data-jpa-begin branch for this tutorial. To know more about this project, please refer to my previous blog1 & blog2.

Git clone for data-jpa-begin branch from em-api repo

You can also use any of your spring-boot projects without a database or other dependencies for that demonstration.

Step 1.1: Open SpringBoot project in VSCode

Checkout and Open SpringBoot Project in VSCode

Step 2: .devcontainer folder

VSCode Remote Container locates .devcontainer folder inside the source root directory. This directory will contain the following two files (We will create them in this session)

  • Dockerfile: It is a typical dockerfile that contains infrastructure information
  • devcontainer.json: Contains docker build/run configurations & VSCode related settings.
mkdir .devcontainer

Step 3: Dockerfile Configuration

To run the SpringBoot app, we need Java and Gradle tools configured on a machine. Here we will configure these tools into docker. We shall write a Dockerfile and include configurations for Java and Gradle.

Those who are familiar with Docker can bring their own Dockerfile (Java & Gradle configured) and can jump right into step 4: Build Docker Image.

3.1 Dockerfile:

It is the manifest file that contains the instructions for configuring the infrastructure for our project in the docker. Please feel free to refer to this link for more detail.

Please create a Dockerfile inside .devcontainer folder

cd .devcontainer && touch Dockerfile

3.2 Base Image:

Typical dockerfile begins with a base image and most of the time it may contain the base operating system with some base tools installed.
We can start our dockerfile configuration with Ubuntu as a base image and install Java and gradle. But here, we are using Microsoft’s base image mcr.microsoft.com/vscode/devcontainers/java:0–8-buster for this demo.

3.3 What's inside Base Image:

Run the following command in the terminal to find the tools available inside the mcr.microsoft.com/vscode/devcontainers/java:0–8-buster container.

As shown in the below screenshot, the docker image contains the following important tools,

  • Java 1.8.0_292
  • JAVA_HOME is set
  • There isn't a gradle to build our SpringBoot app.
Default tools available in the mcr.microsoft.com/vscode/devcontainers/java:0–8-buster docker image

3.4 vscode user accounts

It is good practice to create a user in the docker and use that user account for its operation. So we do not rely on root user for all operations. Here we'll create vscode as a new user and start using it inside the docker container.

3.5 Add Gradle to Docker

Docker Base image is installed with the sdkman tool. We can install gradle through sdkman by using the following docker configuration.

Install gradle Inside docker container using docker-build-arguments through sdkman tool

We have used docker-build arguments to provide a gradle version while installing gradle inside the docker image. If we don’t provide any argument for GRADLE_VERSION, then the gradle latest version will be installed.

Gradle should be installed with proper permissions and we use umask to give correct permission when installing gradle. This setup must be performed with the vscode user account. Refer to the below command.

Install gradle inside docker container by setting proper permissions to the folder using umask

The following are the tools used in the above command to install the gradle. For your reference, We have included a short description and their official site link.

umask: The umask number controls the default permissions of newly created files

su: The su (Short for substitute or switch user) utility allows you to run commands with another user’s privileges, by default the root user.

docker build arguments: The ARG instruction defines a variable that users can pass at build-time to the builder with the docker build command using the --build-arg <varname>=<value> flag

The final version of the Dockerfile is shown below for our reference.

Final dockerfile configurations while installing gradle with the proper user permissions

Step 4: Build docker image

Here we are going to build the docker image and confirm if we have java and the gradle tools are available in the docker image.

Let’s navigate to the .devcontainer folder and run the following command to build a docker image with the latest gradle version. Remember that we have used the docker-build arguments to control gradle version. In the command below, given that we need the latest version, we passed no argument to GRADLE_VERSION.

Build docker Image with latest gradle version and buster variant

Run the following command to see the existence of the new docker image as described below,

grep the newly created docker image
Build & verify docker image of gradle tool

As shown in the image below, run the following command to verify that the newly built docker image has gradle & java.

Verify the existence of the gradle tool
Gradle version check inside the docker container

Now our Dockerfile configuration is ready and it can be used to build our spring boot application.

Step 5: devcontainer.json configuration

Now we have a Dockerfile with Java and Gradle configuration. As a VSCode remote container user, there is no need to run docker commands manually in the terminal. VSCode team has provided the very convenient option to capture docker-related configuration in a devcontainer.json file. So as an end-user we don’t need to run any of the docker commands to use VSCode Remote container. VSCode reads the devcontainer.json file to build the docker image and starts the container.

Sample devcontainer.json
  • name: Name of the configuration
  • build: Docker builds related configuration
  • dockerfile: Dockerfile path relative to .devcontainer folder.
  • args: Docker build-arguments
  • remoteUser: We can run our docker container configuration as a non-root user. Here we use vscode as a non-root user

Step 6: Open in VSCode Container

We are done with our configurations. Let's open our source code in VSCode. Open VSCode command palette and search and open for Remote container: Reopen in Container (Shown in below image)

Reopen in the container

VSCode will start building Docker Image. It will take some more time since it will pull images from the docker hub.

VSCode builds docker image
VSCode run inside the docker container
VSCode opened inside docker container

Step 7: Run SpringBoot App

We have configured docker and opened the source code inside vscode. Now we have all the necessary tools such as java & gradle to run SpringBoot app.

gradle bootRunis the command to run the SpringBoot application.

Run gradle bootRun to start the SpringBoot application. The application started on port 8080. Refer to the logs shown below image.

Run SpringBoot app inside docker container through VSCode Remote Container

This SpringBoot app is configured with a few REST endpoints. Let us consume any one of the REST API and validate whether the endpoint returns the correct response. Here I have used curl command to consume /health endpoint.

/health end point

Please check the response in the below screenshots.

curl health check endpoint inside the vscode remote container
health check log message

What we have done so far,

  1. Installed VSCode and VSCode Remote Extensions
  2. Installed Docker
  3. Check out sample SpringBoot
  4. Dockerfile & devcontainer.json configuration (inside .devcontainer folder)
  5. Opened the source code in the VSCode Remote Container
  6. Executed gradle bootRun command

Now we can run our SpringBoot app. But we are manually running the command in the VSCode Terminal.

We are still yet to explore and use the power of VSCode Extensions to run the SpringBoot app inside VSCode. In our next blog, we’ll configure few VSCode extensions inside remote containers and make the development environment more sophisticated for Spring Boot.

VSCode has provided some sophisticated ways to add Dockerfile and devcontainer.json and we will explore them in my upcoming blogs.

Please check out my previous blog to know why a developer needs VSCode Remote Container.

The source code for this blog can be found from em-api repo’s vscode-remote-dev branch.

has experience in developing full-stack applications using SpringBoot, Node, React and GraphQl technologies. He is also the AWS & Azure cloud specialist who uses the infrastructure as code (IAC) via Terraform. He worked with Spark & Hive Big Data technologies. He debuted his career as an iOS developer.
Let’s connect to
through LinkedIn, GitHub, Dev.to, Medium Email Subscription

--

--

Ragunath Rajasekaran

Sr. Tech Lead | Spring Boot | Big Data | AWS | Terraform | Spark | React | Docker