Dockerize your Next.js application with OpenTelemetry via infrastack.ai
Learn how to dockerize your Next.js application with OpenTelemetry via infrastack.ai
What you will learn
- How to dockerize your Next.js application with OpenTelemetry via infrastack.ai
Prerequisites
- Docker installed on your machine
- An infrastack.ai account
- A Next.js application configured with the infrastack.ai SDK, see Integrate OpenTelemetry for Next.js with infrastack.ai
The application should be on the nodejs
runtime. The edge
runtime is not supported at this time.
Overview
Docker is a platform for building, running, and shipping applications with ease. With the power of infrastack.ai and OpenTelemetry, you can get comprehensive observability for your Next.js application.
After you have configured your application with the infrastack.ai SDK by following the Integrate OpenTelemetry for Next.js with infrastack.ai guide, you can dockerize your application by following these steps:
Configure next.config.mjs
In your next.config.mjs
file, :
This will allow the Next.js application to be run as a web service.
Create a Dockerfile
Create a Dockerfile
in the root of your project with the following content:
In this optimized dockerfile, we are using the base
image as the base image for the builder and runner stages. The builder
stage is used to build the application and the runner
stage is used to run the application.
Create a dockerignore file
Create a .dockerignore
file in the root of your project with the following content:
This will prevent unnecessary files from being included in the Docker image. You can add more files to the ignore list if you see fit.
Build the Docker image
Build the Docker image with the following command:
nextjs-example:latest
to your desired name and tag.Run the Docker container
Run the Docker container with the following command:
Visit your application
You can verify that the application is working by navigating to http://localhost:3000
in your web browser.
Optional: Deploy to a cloud provider
You can push the Docker image to a registry such as Docker Hub or GitHub Container Registry and then deploy it to a cloud provider of your choice.