You can use the Python agent to instrument Python applications deployed in Docker containers. This document will show you how to build, configure, and deploy an instrumented version of your Dockerized Python application.
This Dockerfile provides a base for your Python Docker image by installing the Python agent. You can customize it by changing the
FROM instruction, for example to use another version of Python or a different Linux distribution.
To get started, copy the code block below and save it as a new Dockerfile in your directory.FROM python:3.6-alpineRUN pip install --no-cache-dir newrelicENTRYPOINT ["newrelic-admin", "run-program"]
Build the base image:$ docker build -t python_newrelic:latest .
You can use the new image as the base for your own Dockerfile, replacing your
FROM python_newrelic:latest. You'll need to put your app's command line in a
CMDinstruction, not in
ENTRYPOINT, so that the
newrelic-adminprogram can start it. For example:CMD ["gunicorn", "-b :5000", "myapp:app"]
To enable the agent, you'll need to configure it with environment variables. The
NEW_RELIC_APP_NAMEvariables are required. If you use
docker run, for example, you can add
$ docker run -e NEW_RELIC_LICENSE_KEY=YOUR_LICENSE_KEY \ -e NEW_RELIC_APP_NAME="YOUR APPLICATION NAME" \ INSTRUMENTED_IMAGE_NAME
If you're using
docker-compose, you can use secrets to manage your license key.
When configuring the agent, you have two options:
You can configure most agent settings with environment variables. You can do this at run time with the
-e parameter to
docker run, for example:
$ docker run -e NEW_RELIC_APP_NAME="A Different App" \ -e NEW_RELIC_LOG=stdout \ -e NEW_RELIC_DISTRIBUTED_TRACING_ENABLED=true \ -e NEW_RELIC_LICENSE_KEY=YOUR_LICENSE_KEY \ INSTRUMENTED_IMAGE_NAME
Or you can include environment variables in your Dockerfile:
ENV NEW_RELIC_LOG=stdout \NEW_RELIC_DISTRIBUTED_TRACING_ENABLED=true \NEW_RELIC_APP_NAME="My Application"# etc.
We strongly recommend not putting your license key in your Dockerfile or Docker image. Instead, set it at run time with the
When you can't or don't want to set options with environment variables, you can do this with a configuration file.
If you want to configure the Python agent with a configuration file, you can either add your
newrelic.ini file to your Docker image at build time or mount the file at run time. The file needs to be placed in your app's root directory.
If you choose to use file-based configuration, you must remove the
license_key setting in the file, and any other settings you want to set with environment variables. Otherwise, the default value in the file will override the environment variable. We strongly recommend not including your license key in the Docker image.
Generate or download the
newrelic.inifile described in the Python agent configuration docs.
newrelic.inifile to configure your settings. See the Python agent configuration documentation for more details.
Chose one of the following options:
newrelic.inifile to your Docker image at build time. Add this line to your Dockerfile, replacing the placeholder with the path to your app (either relative to your
WORKDIRor absolute) and build it you normally would.
ADD newrelic.ini /PATH/TO/YOUR/APP
Option 2: Mount the
newrelic.inifile at run time. Add the
-vswitch below to your
docker runcommand, replacing
/PATH/TO/YOUR/APPwith the absolute path to your app's base directory in the Docker image.
$ docker run -v /local/path/to/newrelic.ini:/PATH/TO/YOUR/APP/newrelic.ini \ -e NEW_RELIC_LICENSE_KEY=YOUR_LICENSE_KEY \ INSTRUMENTED_IMAGE_NAME