What you will learn

  • How to dockerize your Next.js application with OpenTelemetry via infrastack.ai

Prerequisites

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:

1

Configure next.config.mjs

In your next.config.mjs file, :

const nextConfig = {
  output: 'standalone', // For web services/Docker
  experimental: {
    instrumentationHook: true, // To enable instrumentation
  },
};

export default nextConfig;

This will allow the Next.js application to be run as a web service.

2

Create a Dockerfile

Create a Dockerfile in the root of your project with the following content:

Dockerfile
COPY package.json package-lock.json ./
RUN npm ci

FROM base as builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm run build

FROM base as runner
WORKDIR /app

ENV NODE_ENV production

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

RUN mkdir .next

RUN chown nextjs:nodejs .next

COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

ENV PORT 3000

CMD HOSTNAME="0.0.0.0" node server.js

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.

You can change the port if you will not be using the default port of 3000.

3

Create a dockerignore file

Create a .dockerignore file in the root of your project with the following content:

.dockerignore
Dockerfile
.dockerignore
node_modules
.next
README.md
npm-debug.log
!.next/static
!.next/standalone
.git

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.

4

Build the Docker image

Build the Docker image with the following command:

docker image build -t nextjs-example:latest .        
You can change the name nextjs-example:latest to your desired name and tag.
5

Run the Docker container

Run the Docker container with the following command:

docker container run -p 3000:3000 -e INFRASTACK_API_KEY=<your-infrastack-api-key> nextjs-example:latest
6

Visit your application

You can verify that the application is working by navigating to http://localhost:3000 in your web browser.

Now, you can check your data in the infrastack.ai dashboard.

7

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.