Configuring to automatically run in Docker


September 13, 2019

Configuring to automatically run in Docker

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


For this example, we are using a very simple Java application that prints “Congratulations, you are running 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 are available in GitHub here:

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
CMD java -jar /opt/nvdocker.jar

Adding to our docker application

To configure 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 /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 installed

Here is the complete docker file that will run our application with 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
ADD /opt/nerdvision.jar
ENV JAVA_OPTS="-javaagent:/opt/nerdvision.jar=api.key={Your API Key}"
CMD java $JAVA_OPTS -jar /opt/nvdocker.jar

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

Using with our Docker Application

With running we can set a breakpoint within our 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 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 and a snapshot will be available.

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



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