🚀 DevOps & SRE Certification Program 📅 Starting: 1st of Every Month 🤝 +91 8409492687 🔍 Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Deploying NewRelic Agent in Docker Image using Dockerfile for Tomcat and Java

It doesn’t actually matter where you put the New Relic agent. All that matters is that the agent and its support files (including newrelic.yml) are in the same folder, and that you set the -javaagent property of your JVM to point to the location of the newrelic.jar file.

A very important thing to know: your startup script (the one which contains the command that starts your application server) must include Java’s built-in argument called “-javaagent”, and the value of that argument must be the location of wherever you unzipped the newrelic agent. For example, “-javaagent:/usr/local/tomcat/newrelic/newrelic.jar”.

Let’s say that you were testing out deploying a single Java web application, using Docker and New Relic. These 2 files might be all you need:

$ cat Dockerfile

FROM tomcat
RUN mkdir -p /usr/local/tomcat/newrelic/logs
RUN useradd tomcat
RUN chown -R tomcat /usr/local/tomcat/newrelic/logs
ADD ./newrelic/newrelic.jar /usr/local/tomcat/newrelic/newrelic.jar
ADD ./newrelic/newrelic.yml /usr/local/tomcat/newrelic/newrelic.yml

$ cat docker-compose.yml (you’ll need to correct the indents on this one if you want to try it out yourself…)

version: ‘3’
services:
web:
build: .
ports:
- “8080:8080”
environment:
- TOMCAT_USERNAME=tomcat
- TOMCAT_PASSWORD=tomcat
- JAVA_OPTS=-javaagent:/usr/local/tomcat/newrelic/newrelic.jar

Tomcat with environment and Java system properties

FROM tomcat:9
# Add the newrelic.jar and -javaagent parameters
RUN mkdir -p /usr/local/tomcat/newrelic
ADD ./newrelic/newrelic.jar /usr/local/tomcat/newrelic/
ENV JAVA_OPTS="$JAVA_OPTS -javaagent:/usr/local/tomcat/newrelic/newrelic.jar"
# Add the configuration file
ADD ./newrelic/newrelic.yml /usr/local/tomcat/newrelic/
# An example of setting a system property config
ENV JAVA_OPTS="$JAVA_OPTS -Dnewrelic.config.app_name='My Application'"
# An example of setting an Environment variable config
ENV NEW_RELIC_LICENSE_KEY="license_key"
# Config to include the agent logs in Docker's stdout logging
ENV JAVA_OPTS="$JAVA_OPTS -Dnewrelic.config.log_file_name=STDOUT"
EXPOSE 8080
CMD ["catalina.sh", "run"]

How to start an application with the Java agent

FROM openjdk:8
ADD my-application.jar /app
ADD newrelic.jar  /app
ADD newrelic.yml  /app 
ENV NEW_RELIC_APP_NAME="My Application"
ENV NEW_RELIC_LICENSE_KEY="license_key"
ENV NEW_RELIC_LOG_FILE_NAME="STDOUT"
ENTRYPOINT ["java","-javaagent:/app/newrelic.jar","-jar","/app/my-application.jar"]

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.