---
title: Docker and other container environments: Install PHP agent
source: https://docs.newrelic.com/docs/apm/agents/php-agent/advanced-installation/docker-other-container-environments-install-php-agent
---

You can install the PHP agent on a Docker container or other container to monitor one or more of your PHP applications. This is supported for containers that meet the standard [PHP agent compatibility and requirements](https://docs.newrelic.com/docs/agents/php-agent/getting-started/php-agent-compatibility-requirements).

> #### ⚠️ IMPORTANT
>
> The PHP agent's daemon transmits data to New Relic periodically during the minute-long [harvest cycle](https://docs.newrelic.com/docs/using-new-relic/welcome-new-relic/getting-started/glossary#harvest-cycle). If you're starting up and tearing down containers often, ensure that you leave the daemon container running long enough to transmit any remaining data.

## Container options [#overview]

The PHP agent requires two components to work: the PHP agent (one for each application) and a [daemon](https://docs.newrelic.com/docs/agents/php-agent/getting-started/new-relic-daemon-processes), 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 on different containers.](#install-diff-containers) This may be useful if you have short-lived containers.
-   [Install agent and daemon on the same container.](#install-same-container) This is the recommended setup.

## Install agent and daemon in different containers [#install-diff-containers]

Installing the agent and daemon in different containers is supported starting with New Relic PHP agent release 9.2.0.

To see an example application, go to [New Relic's Support Forum](https://knowledge.newrelic.com/s/article/PHP-Agent-And-Daemon-Containers). If you're using short-lived application containers, we recommend you use a separate container for the PHP agent's daemon.

> #### ⚠️ CAUTION
>
> Data transmitted from the agent to the daemon is **not** encrypted. The only exception to this is the [SQL obfuscation](https://docs.newrelic.com/docs/agents/php-agent/configuration/php-agent-configuration#inivar-tt-sql) that happens before sending data to the daemon. If the agent and daemon are running on different hosts, we recommend that you use a private network connection between the agent and daemon.

### Set up the daemon container [#daemon-container]

If you use Docker, you can pull our daemon image from [Docker Hub](https://hub.docker.com/r/newrelic/php-daemon):

1.  Run this command: `docker pull newrelic/php-daemon`.
2.  To customize the image, follow the steps on [Docker Hub](https://hub.docker.com/r/newrelic/php-daemon).

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](https://docs.newrelic.com/docs/agents/php-agent/installation/php-agent-installation-tar-file), and run the `newrelic-install` script with the `install_daemon` argument.
2.  Start the daemon using `--address` and `--watchdog-foreground` arguments.

The `--address` argument sets a port for the daemon to accept connections. The `--watchdog-foreground` argument ensures that the daemon runs in the foreground.

### Set up the PHP agent container [#agent-container]

To set up the PHP agent container for Docker:

1.  Make sure a PHP installation is available in the container. For example, use a [Docker "Official Image"](https://github.com/docker-library/official-images#what-are-official-images) for [`php`](https://hub.docker.com/_/php/) like `php:fpm` or `php:cli`.
2.  To install the agent, download the PHP agent package from New Relic's [download site](https://download.newrelic.com/php_agent/release/), and run the `newrelic-install` script with the `install` argument.
3.  In the `newrelic.ini` file, set the application name and license key with the `newrelic.appname` and `newrelic.license` entries .
4.  Point the agent to the daemon by setting the `newrelic.daemon.address` option in the `newrelic.ini` file. Make sure the value for this option is `HOST:PORT`, where `HOST` is the name or IP address of the host where the daemon is running, and `PORT` is the port number where the daemon is listening

## Install agent and daemon in the same container [#install-same-container]

> #### ⚠️ CAUTION
>
> By default, the first transaction causes the agent to trigger a daemon start and an application connection initialization. For performance reasons, the agent does not wait for those operations to complete before it initializes the connection. This can result in the loss of the first few transactions after a container starts. To prevent this loss, set both `newrelic.daemon.start_timeout` and `newrelic.daemon.app_connect_timeout` entries in the `newrelic.ini` file to our [recommended values](https://support.newrelic.com/s/hubtopic/aAX8W0000008bSo/troubleshooting-frameworks) of 5s and 15s respectively.

To set up the PHP agent and daemon in the same Docker container:

1.  Make sure a PHP installation is available in the container. For example, use a [Docker "Official Image"](https://github.com/docker-library/official-images#what-are-official-images) for [`php`](https://hub.docker.com/_/php/) like `php:fpm` or `php:cli`.
2.  To install the agent, download the PHP agent package from New Relic's [download site](https://download.newrelic.com/php_agent/release/) 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**

````dockerfile
FROM php:fpm

# Install the latest New Relic PHP Agent
RUN \
    cd /tmp \
    # Discover the latest released version:
    && export NEW_RELIC_AGENT_VERSION=$(curl -s https://download.newrelic.com/php_agent/release/ | grep -o '[1-9][0-9]\?\(\.[0-9]\+\)\{3\}' | head -n1) \
    # Discover libc provider
    && export NR_INSTALL_PLATFORM=$(ldd --version 2>&1 | grep -q musl && echo "linux-musl" || echo "linux") \
    # Download the discovered version:
    && curl -o newrelic-php-agent.tar.gz https://download.newrelic.com/php_agent/release/newrelic-php5-${NEW_RELIC_AGENT_VERSION}-${NR_INSTALL_PLATFORM}.tar.gz \
    # Install the downloaded agent:
    && tar xzf newrelic-php-agent.tar.gz \
    && NR_INSTALL_USE_CP_NOT_LN=1 NR_INSTALL_SILENT=0 ./*/newrelic-install install \
    # Configure the agent to use license key from NEW_RELIC_LICENSE_KEY env var:
    && sed -ie 's/[ ;]*newrelic.license[[:space:]]=.*/newrelic.license=${NEW_RELIC_LICENSE_KEY}/' $(php-config --ini-dir)/newrelic.ini \
    # Configure the agent to use app name from NEW_RELIC_APP_NAME env var:
    && sed -ie 's/[ ;]*newrelic.appname[[:space:]]=.*/newrelic.appname=${NEW_RELIC_APP_NAME}/' $(php-config --ini-dir)/newrelic.ini \
    # Cleanup temporary files:
    && rm newrelic-php-agent.tar.gz && rm -rf newrelic-php5-*-linux
```

You must set two environment variables when starting the container from the image built using the above `Dockerfile`:

* `NEW_RELIC_LICENSE_KEY`: set this to your <InlinePopover type="licenseKey"/>.
* `NEW_RELIC_APP_NAME`: set this to your application name

````

> #### 💡 TIP
>
> Here are some Docker troubleshooting resources:
>
> -   [Monitoring containers that run a single PHP script](https://support.newrelic.com/s/hubtopic/aAX8W0000008aNC/relic-solution-single-php-script-docker-containers)
> -   [Troubleshooting tips for the PHP agent](https://support.newrelic.com/s/hubtopic/aAX8W0000008bSo/troubleshooting-frameworks)
