TrafeX / docker-php-nginx

Docker image with PHP-FPM 8.3 & Nginx 1.24 on Alpine Linux
https://hub.docker.com/r/trafex/php-nginx
MIT License
1.33k stars 720 forks source link

Newrelic APM support #171

Open gaudinjeremy opened 4 months ago

gaudinjeremy commented 4 months ago

Hello,

I tried to add the newrelic APM for php but I get this error:

 => ERROR [ 4/12] RUN   curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-linux.tar.gz | tar -C /tmp -zx &&   export NR_INSTALL_USE_CP_NOT_LN=1 &&   export NR_INSTALL_SILE  0.8s
------                                                                                                                                                                                                          
 > [ 4/12] RUN   curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-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* &&   sed -i       -e 's/"REPLACE_WITH_REAL_KEY"/"MY_LICENSE_KEY"/'       -e 's/newrelic.appname = "PHP Application"/newrelic.appname = "PHP APP"/'       -e 's/;newrelic.daemon.app_connect_timeout =.*/newrelic.daemon.app_connect_timeout=15s/'       -e 's/;newrelic.daemon.start_timeout =.*/newrelic.daemon.start_timeout=5s/'       /usr/local/etc/php82/conf.d/newrelic.ini:
0.100   % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
0.100                                  Dload  Upload   Total   Spent    Left  Speed
100 24.9M  100 24.9M    0     0  38.6M      0 --:--:-- --:--:-- --:--:-- 38.6M
0.760 sed: /usr/local/etc/php82/conf.d/newrelic.ini: No such file or directory
------
Dockerfile:34
--------------------
  33 |     
  34 | >>> RUN \
  35 | >>>   curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-linux.tar.gz | tar -C /tmp -zx && \
  36 | >>>   export NR_INSTALL_USE_CP_NOT_LN=1 && \
  37 | >>>   export NR_INSTALL_SILENT=1 && \
  38 | >>>   /tmp/newrelic-php5-*/newrelic-install install && \
  39 | >>>   rm -rf /tmp/newrelic-php5-* /tmp/nrinstall* && \
  40 | >>>   sed -i \
  41 | >>>       -e 's/"REPLACE_WITH_REAL_KEY"/"MY_LICENSE_KEY"/' \
  42 | >>>       -e 's/newrelic.appname = "PHP Application"/newrelic.appname = "PHP APP"/' \
  43 | >>>       -e 's/;newrelic.daemon.app_connect_timeout =.*/newrelic.daemon.app_connect_timeout=15s/' \
  44 | >>>       -e 's/;newrelic.daemon.start_timeout =.*/newrelic.daemon.start_timeout=5s/' \
  45 | >>>       /usr/local/etc/php82/conf.d/newrelic.ini
  46 |     
--------------------
ERROR: failed to solve: process "/bin/sh -c curl -L https://download.newrelic.com/php_agent/release/newrelic-php5-10.16.0.5-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* &&   sed -i       -e 's/\"REPLACE_WITH_REAL_KEY\"/\"MY_LICENSE_KEY\"/'       -e 's/newrelic.appname = \"PHP Application\"/newrelic.appname = \"PHP APP\"/'       -e 's/;newrelic.daemon.app_connect_timeout =.*/newrelic.daemon.app_connect_timeout=15s/'       -e 's/;newrelic.daemon.start_timeout =.*/newrelic.daemon.start_timeout=5s/'       /usr/local/etc/php82/conf.d/newrelic.ini" did not complete successfully: exit code: 1

It seems to be a file path problem linked to this line "0.760 sed: /usr/local/etc/php82/conf.d/newrelic.ini: No such file or directory" but even by changing the directories I haven't managed to finalize the installation.

I followed the newrelic doc to install (https://docs.newrelic.com/docs/apm/agents/php-agent/advanced-installation/docker-other-container-environments-install-php-agent/#install-diff-containers)

Thanks

TrafeX commented 4 months ago

Hi @gaudinjeremy,

Try this, this works for me:

    # New Relic settings
    ARG NEW_RELIC_LICENSE_KEY="your-license-key"
    ARG NEW_RELIC_APP_NAME="your-app-name"

    # Install New Relic
    RUN mkdir -p /var/log/newrelic /var/run/newrelic && \
        touch /var/log/newrelic/php_agent.log /var/log/newrelic/newrelic-daemon.log && \
        chmod -R g+ws /tmp /var/log/newrelic/ /var/run/newrelic/ && \
        chown -R 1001:0 /tmp /var/log/newrelic/ /var/run/newrelic/ && \
        # Download and install Newrelic binary
        export NEWRELIC_VERSION=$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.*>\(.*linux\).tar.gz<.*/\1/p') && \
        cd /tmp && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - && \
        cd "${NEWRELIC_VERSION}" && \
        NR_INSTALL_SILENT=true ./newrelic-install install && \
        rm -f /var/run/newrelic-daemon.pid && \
        rm -f /tmp/.newrelic.sock && \
        sed -i \
        -e "s/newrelic.license =.*/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/" \
        -e "s/newrelic.appname =.*/newrelic.appname = ${NEW_RELIC_APP_NAME}/" \
        $PHP_INI_DIR/conf.d/newrelic.ini
gaudinjeremy commented 4 months ago

Thanks @TrafeX,

The installation has been completed, but I haven't received any data from Newrelic.

I modified the code by adding this line to activate newrelic : "-e "s/;newrelic.enabled =.*/newrelic.enabled = true/" \"

but this had no effect.

Here's my complete dockerfile

`ARG ALPINE_VERSION=3.18 FROM --platform=linux/amd64 alpine:${ALPINE_VERSION} LABEL Maintainer="Tim de Pater code@trafex.nl" LABEL Description="Lightweight container with Nginx 1.24 & PHP 8.2 based on Alpine Linux."

Setup document root

WORKDIR /var/www/html

Install packages and remove default server definition

RUN apk add --no-cache \ curl \ nginx \ php82 \ php82-ctype \ php82-curl \ php82-dom \ php82-fileinfo \ php82-fpm \ php82-gd \ php82-intl \ php82-mbstring \ php82-mysqli \ php82-opcache \ php82-openssl \ php82-phar \ php82-session \ php82-tokenizer \ php82-xml \ php82-xmlreader \ php82-xmlwriter \ php82-redis \ supervisor

Configure nginx - http

COPY config/nginx.conf /etc/nginx/nginx.conf

Configure nginx - default server

COPY config/conf.d /etc/nginx/conf.d/

Configure PHP-FPM

ENV PHP_INI_DIR /etc/php82 COPY config/fpm-pool.conf ${PHP_INI_DIR}/php-fpm.d/www.conf COPY config/php.ini ${PHP_INI_DIR}/conf.d/custom.ini

Configure supervisord

COPY config/supervisord.conf /etc/supervisor/conf.d/supervisord.conf

Make sure files/folders needed by the processes are accessable when they run under the nobody user

RUN chown -R nobody.nobody /var/www/html /run /var/lib/nginx /var/log/nginx

Create symlink for php

RUN ln -s /usr/bin/php82 /usr/bin/php

ARG NEW_RELIC_LICENSE_KEY="xxxxxxxxxx" ARG NEW_RELIC_APP_NAME="app_name"

Install New Relic

RUN mkdir -p /var/log/newrelic /var/run/newrelic && \ touch /var/log/newrelic/php_agent.log /var/log/newrelic/newrelic-daemon.log && \ chmod -R g+ws /tmp /var/log/newrelic/ /var/run/newrelic/ && \ chown -R 1001:0 /tmp /var/log/newrelic/ /var/run/newrelic/ && \

Download and install Newrelic binary

export NEWRELIC_VERSION=$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.*>\(.*linux\).tar.gz<.*/\1/p') && \
cd /tmp && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - && \
cd "${NEWRELIC_VERSION}" && \
NR_INSTALL_SILENT=true ./newrelic-install install && \
rm -f /var/run/newrelic-daemon.pid && \
rm -f /tmp/.newrelic.sock && \
sed -i \
-e "s/newrelic.license =.*/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/" \
-e "s/newrelic.appname =.*/newrelic.appname = ${NEW_RELIC_APP_NAME}/" \
-e "s/;newrelic.enabled =.*/newrelic.enabled = true/" \
$PHP_INI_DIR/conf.d/newrelic.ini

Switch to use a non-root user from here on

USER nobody

Add application

COPY --chown=nobody src/ /var/www/html/

Expose the port nginx is reachable on

EXPOSE 8080

Let supervisord start nginx & php-fpm

CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

Configure a healthcheck to validate that everything is up&running

HEALTHCHECK --timeout=10s CMD curl --silent --fail http://127.0.0.1:8080/fpm-ping `

Thanks

enricodeleo commented 2 months ago

@gaudinjeremy this is the relevant code you need to add to your Dockerfile (discarding your previously implementation of APM). It is tested working on trafex/php-nginx:3.4.0.

Of course you need to build the image with the right --args and make sure the NEW_RELIC_DAEMON_ADDRESS is correct and reachable from the container once running (e.g. being on the same docker network or both being bound to host).


# Configure New Relic
ARG NEW_RELIC_LICENSE_KEY
ARG NEW_RELIC_APP_NAME
ARG NEW_RELIC_DAEMON_ADDRESS

ENV NEW_RELIC_LICENSE_KEY ${NEW_RELIC_LICENSE_KEY}
ENV NEW_RELIC_APP_NAME ${NEW_RELIC_APP_NAME}
ENV NEW_RELIC_DAEMON_ADDRESS ${NEW_RELIC_DAEMON_ADDRESS}

# Install New Relic PHP Agent BEFORE SWITCHING USER to nobody
RUN mkdir -p /var/log/newrelic /var/run/newrelic && \
    touch /var/log/newrelic/php_agent.log /var/log/newrelic/newrelic-daemon.log && \
    chmod -R g+ws /tmp /var/log/newrelic/ /var/run/newrelic/ && \
    chown -R 1001:0 /tmp /var/log/newrelic/ /var/run/newrelic/ && \
    # Download and install Newrelic binary
    export NEWRELIC_VERSION=$(curl -sS https://download.newrelic.com/php_agent/release/ | sed -n 's/.*>\(.*linux\).tar.gz<.*/\1/p') && \
    cd /tmp && curl -sS "https://download.newrelic.com/php_agent/release/${NEWRELIC_VERSION}.tar.gz" | gzip -dc | tar xf - && \
    cd "${NEWRELIC_VERSION}" && \
    NR_INSTALL_SILENT=true ./newrelic-install install && \
    rm -f /var/run/newrelic-daemon.pid && \
    rm -f /tmp/.newrelic.sock && \
    sed -i \
    -e "s/newrelic.license =.*/newrelic.license = ${NEW_RELIC_LICENSE_KEY}/" \
    -e "s/newrelic.appname =.*/newrelic.appname = ${NEW_RELIC_APP_NAME}/" \
    -e "s/;newrelic.enabled = true/newrelic.enabled = true/" \
    -e "s/;newrelic.daemon.address =.*/newrelic.daemon.address = \"${NEW_RELIC_DAEMON_ADDRESS}:31339\"/" \
    $PHP_INI_DIR/conf.d/newrelic.ini
gaudinjeremy commented 2 months ago

@enricodeleo

Thanks for the reply, in the meantime I'd found a solution by putting the deamon in another container, which works perfectly.

Best