Docker and other container environments: Install PHP agent

You can install the New Relic APM PHP agent on a Docker container (or other container) to monitor one or more of your PHP applications. This is supported only for containers that meet PHP agent requirements.

Overview of container options

The 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. For this reason, there are two options for enabling the PHP agent for container environments:

Install agent and daemon in different containers

Requirements:

Set up daemon container:

If you use Docker, you can pull our daemon image from Docker Hub:

  1. Run this command: docker pull newrelic/php-daemon
  2. To customize the image, follow the steps on Docker Hub.

You can also build your own daemon image:

  1. To install the daemon, download the PHP agent package from New Relic's tar file download site and run the newrelic-install script with the install_daemon argument.
  2. Start the daemon using --address and --watchdog-foreground arguments. The --address argument is used to set a port the daemon is accepting connections on. The --watchdog-foreground argument ensures that the daemon runs in the foreground.

Set up PHP agent container:

  1. Make sure a PHP installation is available in the container. For example: you might use a published Docker image like php:7.1.
  2. To install the agent, download the PHP agent package from New Relic's tar file download site and run the newrelic-install script with the install argument.
  3. Set the application name and license key via the newrelic.license and newrelic.appname entries in the newrelic.ini file.
  4. Point the agent to the daemon by setting the newrelic.daemon.address option in the newrelic.ini file. 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, PORT is the port number the daemon is listening on.

An example application can be found at New Relic's Explorers Hub.

Data transmitted from the agent to the daemon is not encrypted. The only exception to this is the SQL obfuscation that happens before sending data to the daemon. We recommend only using a private network connection between the agent and daemon (this only applies when the agent and daemon are running on different hosts).

Install agent and daemon in the same container

  1. Make sure a PHP installation is available in the container. For example: you might use a published Docker image like php:7.1.
  2. To install the agent, download the PHP agent package from New Relic's tar file download site and run the newrelic-install script with the install argument.
  3. Set the application name and license key via the newrelic.license and newrelic.appname entries in the newrelic.ini file.

Dockerfile example
FROM php:7.1

RUN \
  curl -L PHP_AGENT_URL | 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* && \
        sed -i -e 's/"REPLACE_WITH_REAL_KEY"/"YOUR_LICENSE_KEY"/' \
     -e 's/newrelic.appname = "PHP Application"/newrelic.appname = "YOUR_APPLICATION_NAME"/' \
         /usr/local/etc/php/conf.d/newrelic.ini

There are three parts that you must edit this example Dockerfile:

  • PHP_AGENT_URL: The download URL for the desired PHP agent version. Find the most recent version of the agent here: https://download.newrelic.com/php_agent/release/
  • YOUR_LICENSE_KEY: Replace this with your New Relic license key. Note that "REPLACE_WITH_REAL_KEY" is an actual string in the default newrelic.ini file for the PHP agent. Don't edit that string. The sed command replaces that default string with the actual 40-character key, in quotes.
  • YOUR_APPLICATION_NAME: Replace with the your application name, in quotes​.

For more help

Recommendations for learning more: