Mikey
September 13, 2019
In this blog, we will go through the steps required to configure nerd.vision in a docker image.
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.
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
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.
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
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:
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
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}"
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
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:
With nerd.vision running we can set a breakpoint within our nerd.vision workspace and start debugging.
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
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.
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.