Dockerize Play2 Web API with New Relic Integration
As developers, we know the importance of smooth deployment and robust monitoring in delivering applications. With Docker, we can simplify deployment by packaging our application and its dependencies into portable containers. And by incorporating New Relic, we can gain invaluable insights into our application’s performance, ensuring it runs seamlessly.
Whether you’re a seasoned developer looking to streamline your workflow or someone new to containerization and monitoring, this guide is created for you. Let’s embark on this journey together and optimize your Play2Framework projects for success!
Application Key
To begin, let’s prioritize creating an application key — an essential element in our setup. This key holds significant importance, as its integrity directly impacts the security of our sessions. Generating this key is straightforward; we can achieve it through this command.
Once generated, we insert this key into our application.conf file, specifically under the key “play.http.secret.key.” In production environments.
It’s crucial to handle this key securely. Therefore, we advocate retrieving it from environment variables, ensuring its confidentiality. To implement this, we include
within our application.conf file. Do not forget to give this value as environment variable.
Executable
this command serves a pivotal role in our workflow by compiling a binary version of our application. This resulting executable is instrumental for our Docker setup. Upon execution of this command, a zip file is generated at the path {appRoot}/target/universal. Extracting this zip file reveals a folder housing a “bin” directory containing an executable named after your application. This executable can be executed alongside the secret you generated.
Additionally, it offers flexibility by supporting execution with environment variables. If you have an environment variable named PORT
you can give it to the application.
New Relic
When considering monitoring options for your application, New Relic provides two agents. The first focuses on infra-level monitoring, which we’ll bypass in this blog post to concentrate on the second option. This agent directly monitors the application itself.
To begin setup, navigate to the Add Data
tab within New Relic, then select Java
followed by Docker
.
Here, you’ll name your application and save your configuration. Choose Running in Linux
and proceed to download the provided zip folder.
Upon unzipping, you’ll find two essential files: newrelic.jar
and newrelic.yml
. Open newrelic.yml
and input the configuration details from the New Relic interface. Save your changes and proceed to the next step, Update your Dockerfile
.
Copy the Add New Relic Artifacts
section provided. We’ll utilize this in the following chapter titled Dockerization
. Then click save.
Skip the next section because we will not do infra level monitoring.
There will be Test the connection
section after that. However, we will test our connection after creation docker container.
Dockerize
Start by composing a Dockerfile, utilizing Java as the base image.
Establish a fresh directory named ‘new-relic’ within Docker. Integrate your New Relic artifacts into this newly created folder. Append the Java agent to the Java options.
Transfer the directory containing the executable, generated by the ‘sbt dist’ command, into Docker.
Ensure to expose the port where your application operates.
Finally, execute your application with its corresponding secret.
Final version of your dockerfile will be like below.
And an example for you.
Create docker image by running docker run command at the directory of the Dockerfile. I want to create a container which’s platform islinux/x86_64. Yours can change.
Run Your Image
Compose a docker-compose.yml file. Under the services section, specify your application’s name and assign a container name. Specify the image name created previously with the Dockerfile. Set the platform to linux/x86_64. Define the port you wish to use for the application. Remember that Docker does not automatically expose the port, so you need to link your machine’s port with the Docker port manually. Within the environment section, you have the ability to configure environment variables for your application.
Run your image with below command.
You should be able to see the logs inside the running container. It should contain logs for new-relic and of course listening Http on
logs.
Test New Relic Connection
Go to new-relic interface where we left .
Test your connection and confirm the status of the integration.
Send some request to your application. In a few minutes you can see the results in APM & Services
.
In Conclusion
We’ve covered the essential steps to build and deploy a high-performance Play2Framework web API with Docker and New Relic. By leveraging Docker for deployment and New Relic for advanced monitoring, you’re equipped to ensure the success of your application in production.
Remember, the security of your application key is crucial. Handle it with care to safeguard your sessions against unauthorized access.
As you implement these steps, keep in mind the principles of security, scalability, and maintainability. With the powerful combination of Play2Framework, Docker, and New Relic, you’re poised for success in delivering robust and efficient web APIs.
Please be sure to take a look at our other blog posts as well.
Do not forget to check out our corporate website.