Configuring nerd.vision to automatically run in Docker

Mikey

September 13, 2019

Configuring nerd.vision to automatically run in Docker

In this blog, we will go through the steps required to configure nerd.vision in a docker image. 


Introduction

For this example, we are using a very simple Java application that prints “Congratulations, you are running nerd.vision in Docker!” to the console, then every 5 seconds prints a random number to the console.


Our Docker Appliction


All files including the jar file, the Java class and the Dockerfiles with and without nerd.vision are available in GitHub here: https://github.com/mflewittIntergral/NerdVisionDockerSample


NVDocker.java

The code we are running a single Java class with a while loop that will run forever and print a random number every 5 seconds.



Dockerfile

The Dockerfile is running an OpenJDK 12 base image and executing the nvdocker jar file. The commands for creating the executable jar can be found in the readme file in GitHub.


Running the docker image will display the console for the Java application in the terminal, where a new random number between 0 and 49 is printed ever 5 seconds.

FROM openjdk:12
COPY nvdocker.jar /opt
WORKDIR /opt
CMD java -jar /opt/nvdocker.jar

Adding nerd.vision to our docker application

To configure nerd.vision within our docker environment we need to edit the Dockerfile used to build your image, the modifications required are as follows:

  1. Adding the nerdvision.jar file
  2. Setting the JVM arguments for the running JVM
  3. Adding the JVM arguments to the Java run command

Adding the nerdvision.jar file

To fetch the nerdvision.jar file you can use the Docker ADD command, in our case the jar file is placed in the /opt directory but this is file can be placed in any existing directory on the docker container.


ADD https://get.nerd.vision/java/latest/nerdvision.jar /opt/nerdvision.jar

Setting the JVM arguments

It is possible to set the JVM arguments directly in the run command for the docker image, however I find this can make finding the cause of any issues that do occur less clear. The arguments are set within an environment property and then the argument is passed into the Java run command.


ENV JAVA_OPTS="-javaagent:/opt/nerdvision.jar=api.key={Your API KEY}"

Adding the JVM arguments to the Java run command

Our JVM arguments have been set within the JAVA_OPTS environment variable, we now need to place the JAVA_OPTS values into the Java run command.


CMD java $JAVA_OPTS -jar /opt/nvdocker.jar


The complete Dockerfile with nerd.vision installed

Here is the complete docker file that will run our application with nerd.vision. As you can see we have added two lined to download the jar file and configure the JVM options, then passed the options into the run command.

FROM openjdk:12
COPY nvdocker.jar /opt
WORKDIR /opt
ADD https://get.nerd.vision/java/latest/nerdvision.jar /opt/nerdvision.jar
ENV JAVA_OPTS="-javaagent:/opt/nerdvision.jar=api.key={Your API Key}"
CMD java $JAVA_OPTS -jar /opt/nvdocker.jar

If nerd.vision is installed on the JVM, when running the docker image you will see a nerd.vision block at the beginning of the console output for the application:


Using nerd.vision with our Docker Application

With nerd.vision running we can set a breakpoint within our nerd.vision workspace and start debugging.

Configuring a breakpoint

With this simple application, there is no issue to debug but we can set a breakpoint on line 20 of the NVDocker.java file to capture the application state at this time. Here I have configured a breakpoint to only fire if the integer “i” is greater than 35


Viewing our snapshot

When our variable i is above 35 a breakpoint will be fired in nerd.vision and a snapshot will be available.

In our snapshot, we see that “i”  was equal to 48.


Mikey

Mikey

Technical Support Engineer and Developer advocate for nerd.vision and other software projects. Background in object-oriented software development and data analysis. Personal hobbies include simulation gaming and watch collecting.