Your data. Anywhere you go.

New Relic for iOS or Android


Download on the App Store    Android App on Google play


New Relic Insights App for iOS


Download on the App Store


Learn more

Close icon

Need help Using new relic with Spring Boot + Docker

docker
springboot
javaagent

#1

I get the following error when trying to use docker with new relic:

docker run -p 9080:9080 -e spring.profiles.active=local -t pqnt:build

Error opening zip file or JAR manifest missing : BOOT-INF/classes/newrelic/newrelic.jar
Error occurred during initialization of VM
agent library failed to init: instrument

I am using:

newrelic.jar, v3.25.0,
Docker:
Client:
Version: 17.09.0-ce
API version: 1.32
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:40:09 2017
OS/Arch: darwin/amd64

Server:
Version: 17.09.0-ce
API version: 1.32 (minimum version 1.12)
Go version: go1.8.3
Git commit: afdb6d4
Built: Tue Sep 26 22:45:38 2017
OS/Arch: linux/amd64
Experimental: true

My Dockerfile:
FROM java:8u92-jdk-alpine
MAINTAINER #sports-data-internal
ARG NR_ENV

Copy repo to docker working dir

WORKDIR /opt/application
COPY ./target/*.jar ./application.jar

RUN sh -c ‘touch ./application.jar’

Java RT cmd

ENV JAVA_OPTS="-Xmx2G -Xms2G -XX:+UseG1GC -javaagent:BOOT-INF/classes/newrelic/newrelic.jar -Dnewrelic.config.file=BOOT-INF/classes/newrelic.yml -Dnewrelic.environment=${NR_ENV}"
ENTRYPOINT [ “sh”, “-c”, “java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar ./application.jar” ]

I am able to get the app to run outside of Docker, as follows:
java -javaagent:./target/classes/newrelic/newrelic.jar -Dnewrelic.config.file=/Users/paul.lester/code/pqnt/target/classes/newrelic.yml -Dnewrelic.environment=dev_pqnt -jar target/pqnt-0.0.1-SNAPSHOT-rcb64e.jar

However, when I attempt to run it within the docker container, I can’t find a proper path to get it to run. I’ve tried several different paths in the Dockerfile for the config file and new relic jar, but none of them work. I get the same error.

My Spring Boot fat jar looks like this, with regards to new relic:
0 Tue Dec 19 09:52:52 EST 2017 BOOT-INF/classes/newrelic/
122 Mon Jan 11 23:27:28 EST 2016 BOOT-INF/classes/newrelic/CHANGELOG
5185 Fri Jan 15 09:43:00 EST 2016 BOOT-INF/classes/newrelic/LICENSE
15240 Fri Jan 15 09:41:02 EST 2016 BOOT-INF/classes/newrelic/newrelic-api.jar
83581 Mon Jan 11 23:27:28 EST 2016 BOOT-INF/classes/newrelic/nrcerts
16564 Mon Jan 11 23:27:28 EST 2016 BOOT-INF/classes/newrelic/extension.xsd
8825624 Fri Jan 15 09:45:26 EST 2016 BOOT-INF/classes/newrelic/newrelic.jar
85200 Fri Jan 15 09:53:12 EST 2016 BOOT-INF/classes/newrelic/newrelic-api-javadoc.jar
11761 Tue Dec 19 09:52:42 EST 2017 BOOT-INF/classes/newrelic.yml
4488 Mon Jan 11 23:27:28 EST 2016 BOOT-INF/classes/newrelic/extension-example.xml
17938 Fri Jan 15 09:53:12 EST 2016 BOOT-INF/classes/newrelic/newrelic-api-sources.jar
11666 Mon Jan 11 23:27:28 EST 2016 BOOT-INF/classes/newrelic/newrelic.yml
3456 Mon Jan 11 23:27:28 EST 2016 BOOT-INF/classes/newrelic/README.txt
8825624 Fri Jan 15 09:45:20 EST 2016 BOOT-INF/lib/newrelic-agent-3.25.0.jar
8001725 Fri Jan 15 09:45:26 EST 2016 BOOT-INF/lib/newrelic-java-3.25.0.zip

Thanks, in advance, for your help!


#2

Solved:

The issue was in finding the newrelic.jar and newrelic.yml file. Both were, previously, inside the application jar. I simply had copy them to the root directory of the Docker image and everything works:

FROM java:8u92-jdk-alpine
MAINTAINER #sports-data-internal
ARG NR_ENV

Copy repo to docker working dir

WORKDIR /opt/application
COPY ./target/*.jar ./application.jar

Add the newrelic pieces to the root directory of the docker image

COPY ./target/newrelic/newrelic.jar ./newrelic.jar
COPY ./target/classes/newrelic.yml ./newrelic.yml

RUN sh -c ‘touch ./application.jar’

Java RT cmd

ENV JAVA_OPTS="-Xmx2G -Xms2G -XX:+UseG1GC -javaagent:./newrelic.jar -Dnewrelic.config.file=./newrelic.yml -Dnewrelic.environment=${NR_ENV}"
ENTRYPOINT [ “sh”, “-c”, “java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar ./application.jar” ]


#3

Excellent news, @paul.lester and thank you so much for updating us all with the solution—your thread will help other community members in the future who are having this same issue. :blush:


#4

For info, heres how I implemented it…

I’m using Spring-Boot on AWS ECS using a self contained java executable compiled using gradle.

I needed to monitor each of the docker containers but did not want to modify my deployment source code in order to achieve this.

In order to achieve this I created a duplicate Dockerfile called Dockerfile-newrelic

FROM openjdk:8-jdk-alpine
VOLUME /tmp
RUN mkdir -p /var/log

setup default args

ARG NRA_VERSION=4.1.0
ARG NRA_LICENCE=abcd123456789

add curl and zip

RUN apk --update add curl unzip

download newrelic

RUN curl -SL http://central.maven.org/maven2/com/newrelic/agent/java/newrelic-java/$NRA_VERSION/newrelic-java-$NRA_VERSION.zip -o /tmp/nr.zip &&
unzip /tmp/nr.zip -d /opt/ &&
rm /tmp/nr.zip

make sure the logs directory is defined

RUN mkdir -p /opt/newrelic/logs
RUN chmod 777 /opt/newrelic/logs

stream edit the newrelic.yml and replace the default

RUN cat /opt/newrelic/newrelic.yml | sed -e “s/<%= license_key %>/$NRA_LICENCE/” -e “s/app_name:.*/app_name: MY APPLICATION NAME/” > /opt/newrelic/newrelic.yml.new
RUN mv /opt/newrelic/newrelic.yml /opt/newrelic/newrelic.yml.default
RUN mv /opt/newrelic/newrelic.yml.new /opt/newrelic/newrelic.yml

ADD build/libs/myapp.jar myapp.jar
EXPOSE 8080
ENTRYPOINT java -javaagent:/opt/newrelic/newrelic.jar -jar myapp.jar --spring.profiles.active="${APP_ENV}" --server.port=8080

build the image using the new Docker file and give it a newrelic tag (not latest)

docker build --file Dockerfile-newrelic -t xxxxxxxxxxxxx.eu-west-1.amazonaws.com/myapp-uat:newrelic .

push it into the repository and you can now update your tasks to use the image with the new relic tag

docker push xxxxxxxxxxxxx.eu-west-1.amazonaws.com/myapp-uat:newrelic


#5

Awesome share, @rob.whent! That is an awesome comprehensive example you shared with all of us—this thread is becoming increasingly valuable. Thanks again for sharing your solution with us. :blush:


#6

Just implemented this for a java1.8 Spring boot application on Docker

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE

ENV NRA_VERSION 4.4.0
ENV NRA_LICENCE xxxxxxxxxxxxxxxxxxxxxxxxxxxx

RUN apk --update add curl unzip
RUN mkdir /opt
RUN curl -SL http://central.maven.org/maven2/com/newrelic/agent/java/newrelic-java/$NRA_VERSION/newrelic-java-$NRA_VERSION.zip -o /tmp/nr.zip && unzip /tmp/nr.zip -d /opt/ && rm /tmp/nr.zip
RUN mkdir -p /opt/newrelic/logs
RUN chmod 777 /opt/newrelic/logs
RUN cat /opt/newrelic/newrelic.yml | sed -e “s/<%= license_key %>/$NRA_LICENCE/” -e “s/app_name:.*/app_name: THIS_IS_MY_APP/” > /opt/newrelic/newrelic.yml.new
RUN mv /opt/newrelic/newrelic.yml /opt/newrelic/newrelic.yml.default
RUN mv /opt/newrelic/newrelic.yml.new /opt/newrelic/newrelic.yml

COPY /target/randomAppName-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT java -Djava.security.egd=file:/dev/./urandom -javaagent:/opt/newrelic/newrelic.jar -jar /app.jar


#7

Thanks for sharing your solution here @LloydD :smiley: