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

Relic Solution: PHP Agent And Daemon Containers


#1

The New Relic APM PHP Agent requires two components to work: the PHP agent (one for each application) and a daemon, which aggregates data sent from one or more agents and sends it to New Relic. Previous versions of the PHP Agent required the agent and the daemon to run on the same host or in the same container. With release 9.2.0.247 of the PHP Agent, agent and daemon communication is now possible over an IPv4 or IPv6 TCP socket. This simple application below demonstrates how the agent and daemon can be set up on separate containers.

Agent Container Configuration

The agent Dockerfile below shows how to configure an agent container:

  • PHP agent is downloaded and installed using the agent version set in NEW_RELIC_AGENT_VERSION. Must be version 9.2.0.247 or later for daemon and agent to run on separate hosts.
  • NEW_RELIC_APPNAME and NEW_RELIC_LICENSE_KEY are used to set the newrelic.appname and newrelic.license entries in the newrelic.ini file.
  • The containerized approach is accomplished with newrelic.daemon.address, which is used to point the agent to the daemon. The value for this option needs to be HOST:PORT, where HOST is the name or IP address of the host the daemon is running on, and PORT is the port number the daemon is listening on.

You can verify agent <–> daemon connection by examining the logs at /var/log/newrelic/php_agent.log in the agent container.

agent/Dockerfile:

FROM php:7.2

ARG NEW_RELIC_AGENT_VERSION
ARG NEW_RELIC_LICENSE_KEY
ARG NEW_RELIC_APPNAME
ARG NEW_RELIC_DAEMON_ADDRESS

RUN curl -L "https://download.newrelic.com/php_agent/archive/${NEW_RELIC_AGENT_VERSION}/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-linux.tar.gz" | tar -C /tmp -zx \
 && export NR_INSTALL_USE_CP_NOT_LN=1 \
 && export NR_INSTALL_SILENT=1 \
 && /tmp/newrelic-php5-*/newrelic-install install \
 && rm -rf /tmp/newrelic-php5-* /tmp/nrinstall*

RUN sed -i -e s/\"REPLACE_WITH_REAL_KEY\"/${NEW_RELIC_LICENSE_KEY}/ \    
 -e s/newrelic.appname[[:space:]]=[[:space:]].\*/newrelic.appname="${NEW_RELIC_APPNAME}"/ \
 -e s/\;newrelic.daemon.address[[:space:]]=[[:space:]].\*/newrelic.daemon.address="${NEW_RELIC_DAEMON_ADDRESS}"/ \
    /usr/local/etc/php/conf.d/newrelic.ini

CMD while : ; do php -r "phpinfo();" ; sleep 1 ; done

Daemon Container Configuration

Pull the image from Docker Hub

Update: We have released a Docker image, of the PHP daemon, on Docker Hub. To use it, pull the image with the docker pull newrelic/php-daemon command and run it with the docker run newrelic/php-daemon. The daemon in this image listens on port 31339. For more information on this image, visit Docker Hub or our repo.

Create your own

The daemon Dockerfile below shows how to configure a daemon container:

  • PHP daemon is downloaded and installed using the agent version set in NEW_RELIC_AGENT_VERSION. Must be version 9.2.0.247 or later for daemon and agent to run on separate hosts.
  • The daemon is started using the --address and --watchdog-foreground
    • --address argument is used to set a port the daemon is accepting connections on. --address and newrelic.daemon.address (in the agent newrelic.ini) must match.
    • --watchdog-foreground argument ensures that the daemon runs in the foreground.

daemon/Dockerfile:

FROM alpine:latest

ARG NEW_RELIC_AGENT_VERSION

RUN apk add curl

RUN curl -L "https://download.newrelic.com/php_agent/archive/${NEW_RELIC_AGENT_VERSION}/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-linux-musl.tar.gz" | tar -C /tmp -zx \
 && export NR_INSTALL_SILENT=1 \
 && tmp/newrelic-php5-*/newrelic-install install_daemon \
 && rm -rf /tmp/newrelic-php5-* /tmp/nrinstall* 

CMD /usr/bin/newrelic-daemon --address=$(hostname):31339  --watchdog-foreground

You can verify agent <–> daemon connection by examining the logs at /var/log/newrelic/newrelic-daemon.log in the daemon container.

Application Docker Compose

The docker_compose.yml file below displays the container structure of the example application. The args used in agent/Dockerfile are set here (using a valid license key env variable). The port the daemon is listening on expose is set here. This must match the --address (daemon startup argument) and newrelic.daemon.address (in the agent newrelic.ini).

version: '2.4'

services:
  agent:
    depends_on:
      - "daemon"
    build:
      context: agent 
      args:
        NEW_RELIC_AGENT_VERSION: "9.2.0.247"
        NEW_RELIC_LICENSE_KEY: ${NEW_RELIC_LICENSE_KEY}
        NEW_RELIC_APPNAME: "New Relic PHP Container"
        NEW_RELIC_DAEMON_ADDRESS: "daemon:31339"
  daemon:
    image: newrelic/php-daemon

Feature Idea: Add config "newrelic.daemon.host" to allow non-localhost connections
New relic APM daemon in separate docker container
How to configure multiple docker containers to use one daemon