How to Install New Relic on Your Dockerized System

Hanisha Arora
3 min readNov 4, 2024

--

Setting up New Relic on Docker can feel like wrestling a jellyfish — it’s slippery, it’s tricky, and you’re not sure if you’re winning. But don’t worry; with this guide, you’ll have New Relic set up in no time, monitoring your app so smoothly you’ll think it’s magic.

By the end, you’ll have New Relic doing all the hard work while you sit back and take all the credit.

Pre-requistic

  • Create an account on new relic and generate a license key.

1. Setting Up the New Relic Infrastructure Agent

The New Relic Infrastructure Agent collects and sends data on your container’s system-level metrics, making it easier to monitor CPU, memory, and disk usage.

To add the Infrastructure Agent, open up your docker-compose.yml file and add the following service:

newrelic-infraagent:
container_name: newrelic-infraagent
image: newrelic/infrastructure-bundle:3.2.53
environment:
- NRIA_LICENSE_KEY=${NRIA_LICENSE_KEY}
- NRIA_LOGLEVEL=debug
cap_add:
- SYS_PTRACE
network_mode: host
pid: host
privileged: true
volumes:
- "/:/host:ro"
- "/var/run/docker.sock:/var/run/docker.sock"
  • NRIA_LICENSE_KEY: This is your New Relic license key. It’s crucial because, without it, data won’t be sent to New Relic.
  • Network Mode: Setting network_mode to host lets the agent read host metrics directly, which is essential for accurate monitoring.
  • Volumes: Mounting the root filesystem and Docker socket ensures that the agent can read all container data

For more information on the configuration options, see New Relic’s Docker documentation.

2. Installing Application Performance Monitoring (APM)

New Relic captures data like transaction times, error rates, and user interactions and application’s performance metrics. It offers APM solutions for multiple languages. Here‘s sample for python and PHP.

APM for Python

pip install newrelic

Next, set up New Relic within your Python app by creating a newrelic_integration.py file. Here’s a basic setup:

import newrelic.agent
import os

class NewRelicIntegration:

def __init__(self):
new_releic_settings = newrelic.agent.global_settings()
new_releic_settings.license_key = os.environ.get('NRIA_LICENSE_KEY')
newrelic.agent.initialize(config_file='/newrelic.ini', environment=None, ignore_errors=None, log_file=None, log_level=None)

Import this class into your main app file and initialize it. This file tells New Relic how to collect data and where to send it.

from .newrelic import NewRelicIntegration
NewRelicIntegration()

Incase you’re using PHP

RUN sed -i -e s/newrelic.license[[:space:]]=[[:space:]].\*/newrelic.license="your-license-key"/ \
-e s/newrelic.appname[[:space:]]=[[:space:]].\*/newrelic.appname="your-application-name"/ \
-e s/\;newrelic.daemon.port[[:space:]]=[[:space:]].\*/newrelic.daemon.port="@newrelic-daemon"/ \
-e s/\;newrelic.application_logging.enabled[[:space:]]=[[:space:]].\*/newrelic.application_logging.enabled=true/ \
-e s/\;newrelic.application_logging.metrics.enabled[[:space:]]=[[:space:]].\*/newrelic.application_logging.metrics.enabled=true/ \
-e s/\;newrelic.application_logging.forwarding.enabled[[:space:]]=[[:space:]].\*/newrelic.application_logging.forwarding.enabled=true/ \
/usr/local/etc/php/conf.d/newrelic.ini

3. Monitor your other system — Integrating Third-Party Services

New Relic can also monitor services like PostgreSQL and RabbitMQ. This helps you see how these critical services are performing and spot any potential issues before they become problems.

To integrate PostgreSQL, first enable the PostgreSQL plugin in New Relic’s Integrations Manager. Next, set up a configuration file for PostgreSQL monitoring by adding the following to your docker-compose.yml file:

services:
newrelic-infraagent:
environment:
- POSTGRES_DB_NAME=${POSTGRES_DB_NAME}
- POSTGRES_USERNAME=${POSTGRES_USERNAME}
- POSTGRES_PORT=${POSTGRES_PORT}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
volumes:
- ./postgres-config.yml:/etc/newrelic-infra/integrations.d/postgres-config.yml- ./postgres-config.yml:/etc/newrelic-infra/integrations.d/postgres-config.yml

Now, create a postgres-config.yml file to define the PostgreSQL settings. Here’s an example configuration:

integrations:
- name: nri-postgresql
env:
USERNAME: {{POSTGRES_USERNAME}}
PASSWORD: {{POSTGRES_PASSWORD}}
HOSTNAME: localhost
PORT: {{POSTGRES_PORT}}
DATABASE: {{POSTGRES_DB_NAME}}
COLLECT_DB_LOCK_METRICS: false
COLLECTION_LIST: ALL
TIMEOUT: 10
PGBOUNCER: true
interval: 15s
labels:
env: production
role: postgresql
inventory_source: config/postgresql

With this configuration, New Relic will monitor PostgreSQL metrics like query performance, transactions, and more.

4. Log Forwarding with Fluentd

Fluentd is a flexible tool that collects logs from your containers and sends them to New Relic. This is essential for tracking any errors or performance issues that show up in your logs.

Add Fluentd as a service in your docker-compose.yml file:

newrelic-fluentd:
container_name: newrelic-fluentd
image: newrelic/newrelic-fluentd-docker:latest
environment:
- API_KEY=${NRIA_LICENSE_KEY}
- LOG_LEVEL=info
user: "root"
volumes:
- ./fluent.conf:/fluentd/etc/fluent.conf
- ./fluentd-pos:/var/log/fluentd-pos
- ./logs:/var/log/containers:ro
restart: unless-stopped

and here’s a sample for how to modify logs and send them.

Set Up Fluentd Configuration

To make Fluentd work with New Relic, create a fluent.conf file. This file defines where Fluentd should find logs and how it should send them to New Relic.

Here’s a basic example.

Just do docker-compose up and refresh your New Relic’s interface. You’ll see tamed server telling you things.

Now you’ll know about every hiccup, crash, and random server mood swing before it spirals out of control. If you need more ideas to expand your monitoring setup, New Relic’s documentation have plenty of tips and tricks. With New Relic on the job, you’re free to get back to building the next big thing — or at least look busy doing it. Hope you have a well-behaved application now!

Here’s the github repo, incase you need these sample files.

--

--

Hanisha Arora
Hanisha Arora

Written by Hanisha Arora

Making developers work on real problems by killing their tech biases.

No responses yet